Winsock 仅在程序关闭时发送数据
我有一个 C++/Windows 程序,它通过 WM_COPYDATA 消息从另一个 C++ 程序接收数据。然后应该使用 Sockets/winsock 将此消息发送到用 Java 编写的服务器。客户端与服务器的连接正常,但似乎无法及时发送消息。然而,一旦客户端关闭,它应该发送的所有消息都会被一大堆发送。下面是 Java 服务器终端输出的示例:
Server Starting up.
Client Accepted.
hi from clienttesttesttesttesttesttesttesttesttesttesttesttesttesttest
前两行是这些事件发生时 Java 服务器输出的。最后一行是来自客户端的消息。客户端在 Winsock 初始化后立即发送“hi from client”,然后在程序中的各个点进行“测试”,因为它通过 WM_COPYDATA 消息从其他 C++ 程序接收数据。
这是 Java 服务器代码:
BufferedReader in = new BufferedReader(new InputStreamReader(
clientSocket.getInputStream()));
String incomingLine;
while((incomingLine = in.readLine()) != null)
System.out.println(incomingLine);
这是发送消息的 C++ 函数:
void sendDataWinsock(char* text){
int result = send(ConnectSocket,text,(int)strlen(text),0);
}
这是处理 WM_COPYDATA 消息的 WndProc 部分:
case WM_COPYDATA:
sendDataWinsock("test");
break;
有谁知道为什么要这样做?就好像客户端程序正在将所有这些消息添加到它应该发送的消息队列中,但由于太忙而无法立即发送它们,因此仅在程序关闭时不再需要处理时才发送它们Windows 消息。或者,我想,错误实际上可能出现在 Java 代码中——我对此还很陌生。
I have a c++/windows program that receives data from another c++ program via a WM_COPYDATA message. It is then supposed to use Sockets/winsock to send this message on to a server written in Java. The client connects to the server fine, but it doesn't seem to be able to send messages in a timely fashion. However, once the client is closed down, all the messages it should have been sending get sent in one big lump. Here is an example of the terminal output of the Java server:
Server Starting up.
Client Accepted.
hi from clienttesttesttesttesttesttesttesttesttesttesttesttesttesttest
the first two lines are output by the Java server when those events happen. The last line is messages from the client. The client sends "hi from client" right after winsock is initialized, and then "test" at various points later in the program as it receives data from the other c++ program via WM_COPYDATA messages.
Here is the Java server code:
BufferedReader in = new BufferedReader(new InputStreamReader(
clientSocket.getInputStream()));
String incomingLine;
while((incomingLine = in.readLine()) != null)
System.out.println(incomingLine);
Here is the c++ function where the messages are sent:
void sendDataWinsock(char* text){
int result = send(ConnectSocket,text,(int)strlen(text),0);
}
And here is a section of WndProc where the WM_COPYDATA messages are processed:
case WM_COPYDATA:
sendDataWinsock("test");
break;
Does anyone know why it is doing this? It is as if the client program is adding all these messages to a queue of things it should be sending, but is too busy to send them immediately, and so only sends them as the program is closing down, when it no longer has to process Windows messages. Or, I suppose, the error could actually be in the Java code - I am fairly new to this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在服务器上读取行,但没有发送行。
这意味着您的服务器坐在那里,接收数据,但等待从 readLine() 将一行文本返回到您的程序,由于没有换行符,所以不会发生这种情况,
\n
,被发送。当客户端退出时,readLine() 会返回迄今为止读取的数据。You are reading lines on the server, but you are not sending lines.
That means your server sits there, receiving data but waiting to return a line of text back to your program from
readLine()
, which does not happen since no newlines ,\n
, gets sent. When the client exits, readLine() gives you back the data it read thus far.