具有同时全双工通信的TCPServer [英] TCPServer with simultaneous full duplex communication

查看:74
本文介绍了具有同时全双工通信的TCPServer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个c#服务器/客户端,该服务器/客户端将同时通过TCP在彼此之间发送字节数组.我正在设法解决这个问题.我看到的所有示例都等待消息,然后发送响应.我需要同时进行交流.

我需要为传入的&创建2个单独的TCP套接字连接吗?在服务器和服务器上都传出客户?我可以以全双工"方式通过1个连接同时传递数据吗?任何帮助表示赞赏.

解决方案

我建议您看一下异步套接字.原因是,它们在接收或发送数据时不会阻塞线程.

  Socket.BeginReceive(buffer,offset,size,endReceiveMethod); 

当接收到字节时,将调用endreceive方法.(在另一个线程上)发送相同.

  Socket.BeginSend(buffer,offset,size,endSendMethod); 

我记得在早期,我担心在同一个线程上进行读写,使用读取超时等方式创建困难的构造,并且每个客户端都是自己的线程.

异步套接字不需要此功能.每个客户端不使用单个线程.它使用I/O完成端口 http://msdn.microsoft.com/zh-cn/library/windows/desktop/aa365198(v = vs.85).aspx 而不是阻塞线程./p>

I am trying to write a c# server/client that will simultaneously send byte arrays over TCP across each other. I'm trying to wrap my head around how to accomplish this. All of the example I have seen wait for a message, then send a response. I need communication to happen simultaneously.

Would I need to create 2 separate TCP socket connections for ingoing & outgoing on both the server & client? Can I pass data simultaneously with 1 connection in a "full duplex" fashion? Any help is appreciated.

解决方案

I would advise you to look at the asynchronous sockets. The reason is, that they don't block threads while receiving or sending data.

Socket.BeginReceive(buffer, offset, size, endReceiveMethod);

The endreceive method will be called when there are bytes received. (on a other thread) This is the same for sending.

Socket.BeginSend(buffer, offset, size, endSendMethod);

I remember in the early days I was worried about reading and writing on the same thread, creating difficult constructions with read-timeouts etc and each client it's own thread.

This isn't needed with Asynchronous sockets. It doesn't use a single thread per client. It uses I/O Completion Ports http://msdn.microsoft.com/en-us/library/windows/desktop/aa365198(v=vs.85).aspx instead of blocking threads.

这篇关于具有同时全双工通信的TCPServer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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