简体   繁体   English

通过套接字接收和回复数据

[英]Receiving and replying with data over a socket

So, right now, the only thing I've managed to do, is, on connect, pw.printin("Hi") . 因此,现在,我唯一要做的就是在连接上pw.printin("Hi") Now the receiving side of the client may reply with "Hello", and if it does, I want to answer with "Hi" , and then have it reply with "Hello" again, in an endless loop that only gets interrupted in another if statement. 现在,客户端的接收方可能会回答“ Hello”,如果是,我想回答“ Hi”,然后让它再次回答“ Hello”,这是一个无休止的循环,只会在另一个循环中被中断声明。

try {
            ServerSocket serverSocket = new ServerSocket(90);
            while(true) {
                String str= "Hello";                
                Socket socket = serverSocket.accept();
                OutputStream os = socket.getOutputStream();
                PrintWriter pw = new PrintWriter(os, true);
                BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                pw.println("Hi");
                if(br.readLine().equals(str)){
                    pw.println("Hi");
                }
                System.out.println(str);
            }
        } catch (IOException e) {
                try {
                    if(serverSocket!= null){
                    serverSocket.close();
                    }
                }
                 catch (IOException e1){

                    e1.printStackTrace(System.err);
                }
            }
        return 500;
    }
}

Your problem is, that there is no actual loop for repeatedly saying hi to the client, you have to make it. 您的问题是,没有反复向客户hi实际循环,您必须这样做。 The Socket socket = serverSocket.accept(); Socket socket = serverSocket.accept(); part is for dealing with one client, so the while(true) loop just keeps the server alive for multiple clients to connect. 部分是用于处理一个客户端,因此while(true)循环仅使服务器保持活动状态,以便多个客户端进行连接。 Thus, you have to make the inner loop (preferably the other thread/thread pool for client in more serious scenario) to make per-client logic. 因此,您必须制作内部循环(在更严重的情况下,最好是客户端的其他线程/线程池)来制作每个客户端的逻辑。

The sample of such loop: 此类循环的示例:

try {
    String str = "Hello";
    String endingStr = "Bye";
    while (true) {
        Socket socket = serverSocket.accept();
        OutputStream os = socket.getOutputStream();
        PrintWriter pw = new PrintWriter(os, true);
        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        pw.println("Hi");
        while ((input = br.readLine()) != null) { //Here is the loop part
            input = input.replaceAll("[^A-Za-z0-9]", "");
            if(input.equals(endingStr)){
                break;
            }

            if (input.equals(str)) {
                pw.println("Hi");
            }
        }
        socket.close();
    }
} catch (IOException ex) {
    try {
        if (serverSocket != null) {
            serverSocket.close();
        }
    } catch (IOException e1) {

        e1.printStackTrace(System.err);
    }
}

EDIT: changed code to prevent NPE as suggested by EJP 编辑:更改了代码以防止EJP建议使用NPE

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

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