C ++和Windows套接字问题 [英] C++ and Windows Socket Problem

查看:85
本文介绍了C ++和Windows套接字问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++和Windows套接字

我有一个一对一的Server Client应用程序,其中Server发送大约20,000个信号,而Client通过另一种方式独立发送2000个不同的信号.我正在使用一个共享端口地址进行异步通信.如果我开始将客户端信号发送到服务器,则服务器会正​​确更新其信号副本,但是它发送给客户端的任何信号都会很慢地更新已发送信号的客户端副本.如果我开始将服务器信号发送给客户端,则客户端会正确更新其信号副本,但无法有效跟踪其发送给服务器的任何信号.

客户端将数据发送到与其接收数据的端口不同的端口上是理想的.但是,我不知道这是否可能.我也许可以将客户端设置为服务器本身,以便仅通过其他端口将数据发送到真正的服务器.答案可能在于多线程,但是客户端计算机已禁用多线程!我已经读了一些关于完成端口的信息,但也可能依赖于多线程.

是否有一种简单的方法来设置高效的异步数据流(如果我只需要在当前代码中添加另一个端口地址,则双端口选项似乎最好),或者是否需要其他解决方案?

C++ and Windows Socket

I have a one to one Server Client app where the Server sends approximately 20,000 signals and the Client Independently sends 2000 different signals the other way. I am using asynchronous communication with one shared Port address. If I start sending the Client signals to the Server then the Server updates its copy of the signals correctly, but any signals it sends to the Client are slow at updating the Client copy of the sent signals. If I start sending the Server signals to the Client then the Client updates its copy of the signals correctly, but any signals it sends to the Server are not tracked efficiently.

It would be ideal for the Client to send its data on a different Port to that which it is recieving data. However, I dont know if that is possible. I might be able to set the Client up as a Server itself to just send data on a different Port to the true Server. The answer may lie in multi-threading, however the Client machine has multithreading disabled! I have read a few bits on completion ports but that may rely on multi-threading as well.

Is there an easy way of setting up efficient asynchronous data flows (dual port options would seem best if I only need to add another Port address to my current code), or will my other solutions be required, or possible?

推荐答案

一个简单的答案可能是ØMQ\ zeromq \ [ ^ ]

您还可以查看 OpenDDS [ ^ ],听起来您正在从事类似的工作.

最好的问候
Espen Harlinn
The easy answer to your question would probably be someting like ØMQ \zeromq\[^]

You could also look at OpenDDS[^] as it sounds like you are working on something similar.

Best regards
Espen Harlinn


在我看来,问题可能与套接字用来相互通信的协议有关.

您是否尝试过从Ws2_32.lib库在Winsock2.h中声明的WSAEnumProtocols函数?也许您只需要为您的旧系统使用正确的协议创建连接.
It sounds to me like the problem may be tied to the protocol that the sockets are using to communicate to one another.

Have you experimented with the WSAEnumProtocols function declared in Winsock2.h from the library Ws2_32.lib? Perhaps you just need to create a connection using the correct protocol for your legacy system.


首先观察一下-也许您使用的术语有些错误.

您的客户端/服务器连接正在使用一个(可能是TCP或UDP)连接(不是端口",实际上涉及2个端口,任何连接中的源端口和目的端口)

随后,客户端和服务器都将数据异步发送到连接中. (我假设您的信号"一词实际上意味着大量数据".)

可以在单个线程中执行客户端,实际上,通常情况下,接收和发送部分都是同一线程的一部分.因此,听起来很不寻常,您可以在不使用多线程的情况下运行客户端代码.

(当然,在服务器上,您无法执行此操作,因为每个客户端连接都是在单独的线程中处理的)

假设到目前为止,我正确理解了您的情况,那么您必须致电"receive"(或"recv")某个地方,如果未收到数据,则该呼叫会在预定时间内返回. (我不知道您是在使用原始套接字还是在顶部使用某些包装器-所以我在这里有点猜测.此外,我也不知道您的套接字是否设置为阻塞).如果在调用"recv"之后没有物理数据到达或等待缓冲区,则您的线程可能被阻塞,并且在没有多线程的情况下,客户端正在执行繁忙的等待.显然,在此期间,您无法发送或为此做任何事情.这可能就是您观察性能问题的原因.
如果使用套接字,则可以在调用"recv"以获取实际数据之前使用选择"功能检查套接字的状态,这样可以避免不必要的阻塞.

综上所述,我认为您必须对客户端代码的接收部分有一个很好的了解,以避免由于没有可用数据而导致其阻塞时间过长的任何情况.
First an observation - perhaps you are using slightly wrong terminology.

Your client/server connection is using one (probably TCP or UDP) connection (not "port", there are actually 2 ports involved, the source port and the destination port in any connection)

Subsequently, both client and server send data into the connection, asynchronously. (I am assuming your term "signal" really means "a chunk of data").

It is possible to execute a client in a single thread, in fact it is commonly the case that both receive and transmit parts are a part of the same thread. Therefore, as unusual as it sounds, you can run client code without multithreading.

(On the server, of course, you could not do this because each client connection is handled in a separate thread)

Assuming that so far I understand your situation correctly, then you must have a call to "receive" (or "recv") somewhere that returns, if no data is received, in a predefined time. (I don''t know if you are using raw sockets or some wrapper on the top - so I am guessing here a bit. Also, I don''t know if your sockets are set as blocking or not). If there is no physical data arriving or waiting in the buffer after a call to "recv", then your thread may be blocked, and with no multithreading, the client is performing busy waiting. Clearly, during this time you cannot send, or for that matter, do anything at all. This is probably why you are observing performance issues.

If you are using sockets, you can use "select" function to check for the status of the socket before calling "recv" to get the actual data, that would avoid blocking unnecessarily.

To sum it up, I think you have to have a really good look at receive section of client code to avoid any situation where it may block too long due to no data being available.


这篇关于C ++和Windows套接字问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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