Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.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

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 为什么这只工作一次?_Java_Sockets_Client Server - Fatal编程技术网

Java 为什么这只工作一次?

Java 为什么这只工作一次?,java,sockets,client-server,Java,Sockets,Client Server,} 我从客户A收到的第一条消息是可以的,但当他试图向我发送另一条消息时,我从未被接受。卡在socket=serverSocket.accept()处; 我做错了什么?请帮帮我:D **我需要保持会议的活力。我收到的第一个字符串是like和echo或aknowlage,第二个是实际请求。我认为您应该只接受同一个客户端的连接一次。您已经从中读取了所需的套接字。当您从另一个客户端获得新连接时,请使用accept。您的代码在每个已接受的连接中只读取一个请求。您需要一个内部循环,或者最好在一个循环中启动一

}

我从客户A收到的第一条消息是可以的,但当他试图向我发送另一条消息时,我从未被接受。卡在socket=serverSocket.accept()处; 我做错了什么?请帮帮我:D


**我需要保持会议的活力。我收到的第一个字符串是like和echo或aknowlage,第二个是实际请求。

我认为您应该只接受同一个客户端的连接一次。您已经从中读取了所需的套接字。当您从另一个客户端获得新连接时,请使用accept。

您的代码在每个已接受的连接中只读取一个请求。您需要一个内部循环,或者最好在一个循环中启动一个单独的线程来处理每个已接受的连接。

尝试类似的方法

public class Main {

private static Socket socket;

public static void main(String[] args) {
    try {
        int port = 13579;
        ServerSocket serverSocket = new ServerSocket(port);
        System.out.println("Servidor Iniciado escuchando al puerto " + port);
        while (true) {
            socket = serverSocket.accept();
            DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
            String request = dataInputStream.readUTF();
            String returnMessage = "";
            if (request.startsWith("ISO0060000500800")) {
                returnMessage = "ISO006000050081082200000020000000400000000000000" + request.substring(48, 64) + "00" + request.substring(64);
            } else {
                System.out.println("Received: " + request);
                try {
                    InterpretePostilionEvertec interpretePostilionEvertec = new InterpretePostilionEvertec();
                    Transaccion transaccion = interpretePostilionEvertec.interpretarRequest(request);
                    GestorTransacciones gestorTransacciones = new GestorTransacciones();
                    try {
                        returnMessage = interpretePostilionEvertec.interpretarResponse(gestorTransacciones.abono(transaccion), request);
                    } catch (Exception e) {
                    }
                } catch (Exception e) {
                    returnMessage = "Error: " + e.getMessage();
                }
            }
            DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
            dataOutputStream.writeUTF(returnMessage);
            dataOutputStream.flush();
            System.out.println("Return: " + returnMessage);
        }
    } catch (Exception e) {
        System.err.println(e.getMessage());
    }
}

其中SomeObject实现Runnable

在socket=serverSocket.accept()之后添加另一个循环以读取多条消息;这个答案没有道理。也许你需要更清楚地表达出来。我可以在那里看到正确答案的萌芽,但不清楚你的意思是什么:-|
/**
 * Continuously listen on port 8888. accept() will block
 * until a new connection is requested. When a new connection 
 * is received, spawn a new thread to service it, 
 * then listen for more.
 */
public static void main(String[] args) throws Exception {
    ServerSocket listener = new ServerSocket(8888);
    ExecutorService es = Executors.newCachedThreadPool();
    try {
        while (true) {
            es.submit(new SomeObject(listener.accept()));
        }
    } finally {
        listener.close();
        es.shutdownNow();
    }
}