简体   繁体   English

尝试套接字连接后已在使用的地址

[英]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. 嗨,我是Java套接字的新手,我一直在尝试创建服务器套接字。 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 ). 此错误消息表示某些应用程序已经在您指定的端口( 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: 在Linux中,可以使用netstat命令执行此操作:

# netstat -tulpn | grep :3456

For more details on netstat command, see this article . 有关netstat命令的更多详细信息,请参阅本文

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 . 假设该进程的PID1234 Then we gonna need this command: 然后我们将需要以下命令:

# kill -9 1234

You may need root privileges to do that. 您可能需要root权限才能执行此操作。

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. 它可以是selinux ,也可以是网络防火墙设置-也可以考虑进行检查。

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. 这与某个先前的套接字(该程序的先前运行的套接字)保持活动状态有关,以完成关闭机制上的双重ACK。 You can bypass that by indicating a specific option to the socket: 您可以通过为套接字指定一个特定的选项来绕开它:

socket.setReuseAddress(true);

(where socket is your server socket). (其中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. 程序的上一个实例退出后,连接端口仍处于TIME_WAIT状态。 Either wait two minutes, or create your ServerSocket as follows: 等待两分钟,或按如下所示创建ServerSocket:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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