Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ServerSocket.Accept()有问题_Java_Sockets_Network Programming_Serversocket - Fatal编程技术网

Java ServerSocket.Accept()有问题

Java ServerSocket.Accept()有问题,java,sockets,network-programming,serversocket,Java,Sockets,Network Programming,Serversocket,我在使用服务器套接字时遇到问题 我有这两种方法 public boolean sendRequest(String IP, int port) { try { System.out.println("Inside network send"); client = new Socket(IP, port); DataOutputStream outToClient = new DataOutputStream(

我在使用服务器套接字时遇到问题 我有这两种方法

public boolean sendRequest(String IP, int port) {
    try {
        System.out.println("Inside network send");
        client = new Socket(IP, port);
        DataOutputStream outToClient = new DataOutputStream(
                client.getOutputStream());
        outToClient.writeBytes("Requesting Game\n");
        System.out.println("sent");
        BufferedReader inFromClient = new BufferedReader(
                new InputStreamReader(client.getInputStream()));
        String s = null;
        long start = new Date().getTime();
        while (true) {
            s = inFromClient.readLine();
            if (s != null) {
                if (s.equals("YES"))
                    return true;
                else if (s.equals("NO"))
                    return false;
                else return false;
            }
            long curr = new Date().getTime();
            if((curr-start) > (60*1000))
            {
                System.out.println("TIMEOUT IN receiving request response");
                return false;
            }
            //time out 1 minute
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return false;
}

使用这个类构造函数,如下所示

public Communicator() {
    try {
        // initialize a server on this pc
        // connection to other pcs are done on another method
        server = new ServerSocket(1234);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
现在,每当我尝试在我的机器上运行这个程序时,使用这个服务器的不同端口号,它都能完美地工作

但当我尝试在不同的机器上播放它,并正确连接到它们的IP和端口号时,有时会失败 它获取receiveConnection方法在server.accept()上卡住,另一个方法在创建新套接字(IP,端口)时卡住

我需要在这个问题上得到帮助,因为沟通必须是可靠的
谢谢

您要做的是将accept()放入infinate循环中,启动一个新线程来处理接受的套接字。否则,服务器一次只能处理一个请求。这可能是你的问题的原因(即使不是,也会引起问题)你还有另一个问题。如果
readLine()
返回true,或者您得到任何其他EOS指示,则必须关闭套接字并退出读取循环。@EJP问题是它有时无法通过服务器的.accept()代码行。我也不知道为什么有时候有效,有时候无效
public Communicator() {
    try {
        // initialize a server on this pc
        // connection to other pcs are done on another method
        server = new ServerSocket(1234);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}