简体   繁体   中英

Address already in use after attempting socket connection

Hi I'm new to java socket and I've been trying to create a server socket. I know this question may have been asked but i would like for someone to look at my code please. Can someone please tell me where I'm going wrong because I'm getting an error saying "Address already in use" Help please? I have also realised that with some research that I may have run two servers simultaneously. Can anyone please explain please?Here's the server class and the client class.

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.ServerSocket;
import java.net.Socket;


public class MessageServer {
public static void main(String[] args) throws IOException{

    //int port = 25000;

    //int port = Integer.parseInt(args[0]); 

    ServerSocket serverr = new ServerSocket(3456);

    while(true){

        System.out.println("Waiting for client...");
        //ServerSocket serverSocket = new ServerSocket(53705);
        //System.out.println("listening on port " + serverSocket.getLocalPort());
        Socket client = serverr.accept();

        System.out.println("Client from " + client.getInetAddress() + " connected.");
        BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
        String inputLine = in.readLine();
        System.out.println("Client said: '"+inputLine+"'");
        Writer count = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
        byte c [] = count.toString().getBytes();
        count.flush();
        //server.close();

    } 

}
}





import java.net.*;
import java.io.*;

public class MessageSendClient {

public static void man(String args[]) throws IOException{
    Socket client = new Socket ("localhost", 3456);
    System.out.println("Connected to " + client.getInetAddress());
    InputStream in = client.getInputStream();

    byte c[] = new byte[100];
    int num = in.read(c);
    String count = new String(c);

    System.out.println("Server said: " + count);
    client.close();
    }
}

Exception stack trace:

Exception in thread "main" java.net.BindException: Address already in use at
    java.net.PlainSocketImpl.socketBind(Native Method) at     
    java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:376) at 
    java.net.ServerSocket.bind(ServerSocket.java:376) at 
    java.net.ServerSocket.<init>(ServerSocket.java:237) at
    java.net.ServerSocket.<init>(ServerSocket.java:128) at 
    MessageServer.main(MessageServer.java:18)

This error message means some application has already created a socket on the port you specified ( 3456 ).

To resolve the problem you need either change this port to some other, or find out which program occupies it and terminate this program. In Linux, you can do this with netstat command:

# netstat -tulpn | grep :3456

For more details on netstat command, see this article .

When you have identified the process using technique I wrote above, you may want to kill it. Let's assume the PID of the process is 1234 . Then we gonna need this command:

# kill -9 1234

You may need root privileges to do that.

If none of this helps, it is also possible that some external software is blocking your connections. It can be selinux , it can be network firewall settings - consider checking that too.

This has to do with some previous socket (of a previous run of the program) staying alive some time to complete the double ACK on close mechanism. You can bypass that by indicating a specific option to the socket:

socket.setReuseAddress(true);

(where socket is your server socket).

Either:

  1. A previous instance of your code is still running: terminate it.
  2. Something else is listening at the port: bad luck.
  3. A connection port is still in the TIME_WAIT state after the previous instance of your program had exited. Either wait two minutes, or create your ServerSocket as follows:

     ServerSocket ss = new ServerSocket(); // create an unbound socket ss.setReuseAddress(true); ss.bind(...); // argument(s) left as an exercise for the reader 

问题在于,当您对代码进行两次测试时,您并未完全杀死之前的过程。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM