Winsock只在程序关闭时发送数据 [英] Winsock only sending data at program close

查看:175
本文介绍了Winsock只在程序关闭时发送数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个c ++ / windows程序,它通过WM_COPYDATA消息从另一个c ++程序接收数据。然后应该使用Sockets / winsock将此消息发送到用Java编写的服务器。客户端连接到服务器很好,但它似乎无法及时发送消息。但是,一旦客户端关闭,它应该发送的所有消息都会被一次性发送。以下是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

当这些事件发生时,前两行由Java服务器输出。最后一行是来自客户端的消息。客户端在winsock初始化后立即发送hi from client,然后在程序后面的各个点测试,因为它通过WM_COPYDATA消息从其他c ++程序接收数据。

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.

这是Java服务器代码:

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);

这是发送消息的c ++函数:

Here is the c++ function where the messages are sent:

void sendDataWinsock(char* text){    
    int result = send(ConnectSocket,text,(int)strlen(text),0);
}

这里是WndProc的一部分,处理WM_COPYDATA消息:

And here is a section of WndProc where the WM_COPYDATA messages are processed:

case WM_COPYDATA: 
    sendDataWinsock("test");
    break;

有谁知道为什么会这样做?就好像客户端程序将所有这些消息添加到它应该发送的事物的队列中,但是太忙而无法立即发送它们,因此只在程序关闭时才发送它们,当它不再需要处理时Windows消息。或者,我想,错误实际上可能在Java代码中 - 我对此很新。

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.

推荐答案

您正在阅读服务器上的行,但你没有发送行。

You are reading lines on the server, but you are not sending lines.

这意味着你的服务器就在那里,接收数据但等待从<返回一行文本回到你的程序code> readLine(),由于没有新行, \ n ,因此不会发生。当客户端退出时,readLine()会返回它到目前为止读取的数据。

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.

这篇关于Winsock只在程序关闭时发送数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆