gpt4 book ai didi

java - 在 Java 中没有通过 TCP 接收所有消息?

转载 作者:可可西里 更新时间:2023-11-01 02:49:54 26 4
gpt4 key购买 nike

我有一个 tcp 套接字发送这样的三行

        out2.println("message1\n");
out2.println("message2\n");
out2.println("message3\n");

另一个 tco 套接字接收并显示这些消息,就像这样

        System.out.println(in.readLine());
System.out.println(in.readLine());
System.out.println(in.readLine());

但只有第一条消息会被接收和显示,之后我发送的任何消息都不会。

编辑:这是代码

    private void buttonActionPerformed(java.awt.event.ActionEvent evt) {                                       
try {
// TODO add your handling code here:
String ipAddress = ipTextArea.getText();
sourceSocket = new Socket(ipAddress,32323);
out = new PrintWriter(sourceSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(sourceSocket.getInputStream()));
System.out.println(in.readLine());
System.out.println(in.readLine());
System.out.println(in.readLine());
} catch (UnknownHostException ex) {
Logger.getLogger(DESWashView.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(DESWashView.class.getName()).log(Level.SEVERE, null, ex);
}


}

cWashStations 也是从按钮事件中调用的:

public void cWashStations(){
Thread washThread = new Thread(){
@Override
public void run(){
try {
sSocket2 = new ServerSocket(32323);
Thread stationThread = new Thread(){
@Override
public void run(){
try {
washSocket = sSocket2.accept();
out2 = new PrintWriter(washSocket.getOutputStream(), true);
in2 = new BufferedReader(new InputStreamReader(washSocket.getInputStream()));
out2.println("hello from attendant3423\n\n");
out2.flush();
out2.println("hello from attendant3423\n\n");
out2.println("1");
while(running){
}
} catch (IOException ex) {
Logger.getLogger(DESAttendantView.class.getName()).log(Level.SEVERE, null, ex);
}
}
};
stationThread.start();

} catch (IOException ex) {
Logger.getLogger(DESAttendantView.class.getName()).log(Level.SEVERE, null, ex);
}

}
};
washThread.start();
}

最佳答案

如果您使用 println(),请不要在字符串末尾添加 \n

关于java - 在 Java 中没有通过 TCP 接收所有消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2555348/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com