将数据同时发送到连接在单个端口上的多个客户端 [英] Sending Data to Multiple Clients Connected on Single Port Simultaneously

查看:139
本文介绍了将数据同时发送到连接在单个端口上的多个客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



1.我必须对服务器进行编程,使其可以在单个端口上监听多个客户端(例如8080).
2.与客户端建立连接后,客户端与服务器之间便开始通信.客户端发送-服务器接收,然后服务器发送-客户端接收.
3.如果在端口8080上一次将多个客户端连接到服务器,则所有客户端应同时接收数据.

好的,我已经完成了第1点和第2点.我了解套接字编程/TCP/IP编程的一些基础知识.我创建了一个保持侦听模式的TCPListener对象,每当客户端连接时,它就会接受它并创建一个线程来接收其数据并将数据发送到该客户端.

问题是当我连接两个客户端时,一个开始接收,而另一个客户端仅接收数据一两次,然后给出异常-

无法从传输连接中读取数据:远程主机强行关闭了现有连接."

为什么会这样?
有什么我想念或做错的事吗?
我认为线程部分有问题.

请帮忙.

在此先谢谢您.

Hye,

1. I have to program a server which can listen multiple clients on a single port (say 8080).
2. Once connection is established with a client, communication starts between client and server. Client sends - Server Receives, then Server sends - Client receives.
3. If multiple Clients are connected to server at a time on the port 8080, all clients should receive data simultaneously.

Ok, I have done the points 1 and 2. i understand some basics of Socket Programming / TCP/IP Programming. I have created a TCPListener object which keeps in listen mode, and whenever a client connects, it accepts it and create a thread to receive its data and send data to that client.

The problem is when i connect two clients, then the one starts receiving and other client just receive data for one or two time, and then gives an exception -

"Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host."

Why this is happening?
is there something i am missing or doing wrong.
I think there is something wrong in Threading part.

Please help.

Thanks in advance.

推荐答案

请参阅我对问题的评论.我还可以为您提供一些基本概念以及我在过去的解决方案中试图解释的正确设计的框架:
来自同一端口号的多个客户端 [ http://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener.aspx [ ^ ].

—SA
Please see my comment to the question. I can also give you some basic idea and the skeleton of right design I tried to explain in my past solution:
Multple clients from same port Number[^].

If something is not clear, feel free to ask your follow-up questions. The key here is accepting a client, getting an instance of socket or TCP client using System.Net.Sockets.TcpListener.AcceptSocket or System.Net.Sockets.TcpListener.AcceptTcpClient and using separate threads and thread synchronization properly, see http://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener.aspx[^].

—SA


这篇关于将数据同时发送到连接在单个端口上的多个客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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