简体   繁体   English

TCP连接 - 服务器仅在关闭套接字后发送消息

[英]TCP connection - server only sends message after closing socket

I am trying to make a client send a request to a server and receive a response whilst keeping to connection up. 我试图让客户端向服务器发送请求并在保持连接的同时收到响应。

If i close the socket: 如果我关闭套接字:

//server side
outToClient.writeBytes("Message to send");
connectionSocket.close();

//client side
serverResponse = inFromServer.readLine(); 
System.out.println("FROM SERVER: " + serverResponse);

Output on client side 客户端输出

FROM SERVER: Message to send FROM SERVER:要发送的消息

And after that the connection is lost, obviously. 之后,显然连接丢失了。

If i do not close the socket: 如果我不关闭套接字:

//server side
outToClient.writeBytes("Message to send");

//client side
serverResponse = inFromServer.readLine(); 
System.out.println("FROM SERVER: " + serverResponse);

No output on client side. 客户端没有输出。 The server never sends the message or the client never receives it. 服务器从不发送消息,或者客户端从未接收消息。

Anyone knows a possible reason for this to be happening? 有人知道这可能发生的原因吗? The client uses a thread to receive messages and a thread to send messages. 客户端使用线程来接收消息,使用线程来发送消息。 The client socket is created in the main client thread so receiver and sender threads use the current socket to communicate. 客户端套接字在主客户端线程中创建,因此接收方和发送方线程使用当前套接字进行通信。

Thanks in advance. 提前致谢。

If the client expects to read a line, the server should write a line. 如果客户端希望读取一行,则服务器应该写一行。 A line is terminated by a \\n character. 一行由\\n字符终止。

It may also be necessary to flush the stream you are using to send data to the client: 可能还需要刷新您用于将数据发送到客户端的流:

outToClient.writeBytes("Message to send\n");
outToClient.flush();

Without seeing the code it's hard to say if the flush is required or not. 没有看到代码,很难说是否需要刷新。

Never, ever write any code on top of TCP without first specifying the protocol. 在没有先指定协议的情况下,永远不要在TCP上编写任何代码。 Otherwise, when you have a problem like this, it's impossible to determine which end is at fault. 否则,当您遇到这样的问题时,无法确定哪一端有问题。

One side believes it's sending a "message". 一方认为它正在发送“消息”。 The other side does not believe the data it receives is a "message". 另一方不相信它收到的数据是“消息”。 Which is right? 哪个是对的? Well, if we had a protocol specification, we'd look at its definition of a "message" and see. 好吧,如果我们有一个协议规范,我们会看看它对“消息”的定义,然后看看。

But we don't have one, so we can't say. 但我们没有,所以我们不能说。 This makes it impossible to construct a correct fix. 这使得无法构建正确的修复。 If you change the sender to send a line, the receiver will still be broken in requiring a line. 如果您将发送方更改为发送一条线路,则接收方仍然需要一条线路。 Or won't it? 或者不是吗? And you change the receiver to process messages that aren't lines, is that fixing it or breaking it? 并且您更改接收器以处理非线路的消息,修复它还是破坏它?

It seems in this case that both sides are wrong. 在这种情况下,似乎双方都是错的。 The most likely intent is that a message consists of a line of data terminated with a newline. 最可能的意图是消息由以换行符终止的一行数据组成。 The sender is not sending a newline and the recipient is not insisting on one either (because it accepts the data as a "message" when the connection closes). 发件人不发送换行符,收件人也不会坚持使用换行符(因为它在连接关闭时接受数据作为“消息”)。 So unless the design intent really was that a "message" is a chunk of data not including a line ending terminated by either a line ending or the close of a connection, both sides are wrong. 因此,除非设计意图确实是“消息”是一大块数据,不包括由行结束或连接关闭而终止的行结束,否则双方都是错误的。

Document the protocol. 记录协议。 It's an essential step. 这是必不可少的一步。

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

相关问题 Java服务器-TCP套接字检测EOF而不关闭套接字连接 - Java Server - TCP Socket detect EOF without closing the socket connection TCP客户端/服务器通信只发送第一条消息? - TCP Client/Server communication only sends first message? 套接字仅发送一次消息 - Socket sends message only once Java程序,该程序通过套接字接口与邮件服务器建立TCP连接并发送电子邮件 - Java program that establishes a TCP connection with a mail server through socket interface and sends an email Java套接字仅发送一条消息 - Java socket sends only one message 在一次性使用中关闭套接字连接= false spring集成TCP Server - closing socket connection in single-use=false spring integration TCP Server 服务器的Tcp连接关闭启动是否可行? - Tcp connection closing initiation by server is fevorable? 在从服务器接收数据时,在关闭套接字后显示/接收数据。 服务器发送后如何立即接收。 - On receiving a data from the server it is displayed/received after closing the socket. How to receive it immediately once the server sends.? 使用DataOutputStream向Server Socket写消息到Client Socket的消息仅在关闭Client Socket后才发送,为什么? - Writing messages to client socket using DataOutputStream to Server Socket only sent after closing the Client Socket why? Amazon EC2服务器TCP套接字连接 - Amazon EC2 server TCP Socket Connection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM