套接字同时发送/接收 [英] Socket Send/receive simultaneously

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

问题描述

任何人都可以指出你不能同时在套接字上发送和接收的原因吗?



据我所知有2个流可以推送和一个拉



如果你试图同时发送/接收你会得到wasealready错误



什么是套接字抛出wasealready错误的原因(10035)

是否与接收方发回的ack窗口有任何关系?

好​​像要保留为窗口打开行?

can any one point out the reason why u cant send and receive on a socket at the same time ?

to my understanding there are 2 streams one to push and one to pull

if you attempt to send/receive simultaneously you will get wasealready error

what is the reason that the socket throws wasealready error (10035)
does it have any thing to do with the ack window the receiving side sends back ?
as if to keep the line open for the window ?

推荐答案

您可以同时在同一个套接字上发送和接收(通过多个线程)。但是发送和接收实际上可能不会同时发生,因为一个操作可能阻止另一个操作开始直到它完成。这就是它在Winsock中的表现。 .NET包装器/实现可能允许也可能不允许这样做 - 它取决于您使用的类(您需要使用Reflector等工具查看实现,看看是否允许这样做。)
You can send and receive on the same socket at the same time (via multiple threads). But the send and receive may not actually occur simultaneously, since one operation may block the other from starting until it''s done. That is how it''s in Winsock. The .NET wrappers/implementation may or may not permit this - it depends on which class you are using (you will need to look at the implementation using some tool like Reflector to see if this is permitted).

事实上,您可以同时发送和接收。你不能拥有的是通过同一个TCP / IP套接字发送的两个线程或者在同一个TCP / IP套接字上接收的两个线程。



我使用这个很多在我的远程处理框架中。请参阅:

我正在向客户端发送文件(所以我一直打电话给写信)。

客户端由于某种原因想要取消该文件,但不要我想断开连接(我的应用程序做很多事情)。那么,它怎么能告诉服务器这种情况呢?



其中一种方法是:

写一些字节。阅读以获得回复。写更多字节。但是,在这种情况下,我需要在写入之间读取。



另一种方法是:

我做了一段时间(canSend)并写字节。

从另一个线程,我读取相同的套接字。如果我收到取消消息,我将canSend设置为false。



请注意,我只是简化了示例。但如果您需要任何异步消息,您可能需要同时发送和接收。



****我搜索了WSAEALREADY:据我所知,它只发生在非阻塞套接字中,所以我认为最好确切地知道你是如何使用它的。说实话,我总是使用阻塞套接字并创建我自己的线程来访问它们(因此线程可以被阻塞,直到它收到响应)。
In fact, you can send and receive at the same time. What you can''t have is two threads sending over the same TCP/IP socket or two threads receiving at the same TCP/IP socket.

I use this a lot in my remoting framework. See:
I am sending a file to the client (so I keep calling write).
The client, for some reason, wants to cancel the file, but don''t want to disconnect (my application does many things). So, how can it inform such situation to the server?

One of the ways is:
Write some bytes. Read for a response. Write more bytes. But, in this case, I need to read between writes.

Another approach is:
I do a while(canSend) and write bytes.
From another thread, I read the same socket. If I receive a "cancellation message", I set canSend to false.

Note that I am only simplifying the example. But if you need any asynchronous message, you may need to send and receive at the same time.

**** I searched for WSAEALREADY: In my understanding, it only happens in non-blocking sockets, so I think it will be better to know exactly how you are using it. To be honest, I always used blocking sockets and created my own threads to access them (so a thread can be blocked until it receives a response).


你是什么意思在同一个时间,到底?至少可以使用相同的插座进行读写。



但是当使用套接字来交错读取和右边时,这是非常典型的。当套接字读取消息时,这是非常通常的协议,然后发送确认或类似的东西。



此外,Nishant关于不同线程的说明是很重要。套接字是线程安全的,但不仅如此。您是否知道历史上套接字主要是作为在同一台机器上使用的进程间通信原语而设计的?所以他们是一个真正的IPC工具。



-SA
What do you mean "at the same time", exactly? At least the same socket can be used for reading and writing.

But it''s quite typical when a socket is used to interlace read and right. It''s very usual protocol when a socket reads a message, then sends a confirmation or something like that.

Also, Nishant''s note about different thread is very important. Sockets are thread-safe, but not just so. Do you know that historically sockets were designed primarily as the inter-process communication primitive to be used on the same machine? So they are a real IPC tool.

—SA


这篇关于套接字同时发送/接收的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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