在单个服务器上侦听多个端口 [英] Listening on multiple ports on a single server

查看:173
本文介绍了在单个服务器上侦听多个端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我必须对服务器进行编程,我最多(比方说)4个客户端。每个客户端都要收听一个特定的端口,意味着一个端口被分配给一个客户端,而该客户端只会将所有数据发送到该端口。



it意味着我必须一次将所有4个端口保持在监听模式。



现在我能够在单个端口上建立客户端和服务器之间的通信,发送和接收数据。



我在这里搜索并找到类似的东西(但是它用于Java,我需要类似于.Net的东西)http://stackoverflow.com/questions/5079172/java-server-multiple-ports [ ^ ]



请帮助,我该如何创建这样的套接字连接。



我需要这样做:



1 。启动服务器;

2.监听几个端口上的传入连接;

3.确定要连接的端口;

a。如果是端口1,则启动一个线程来监听客户端并输出消息类型x

b。如果是端口2,请启动一个线程来监听客户端并输出消息类型y



提前感谢。

解决方案

< blockquote>这几乎肯定是错误的方法。端口号是一种宝贵的资源,如果你能控制协议,最好只监听一个众所周知的端口,并让协议中的第一条消息成为切换消息,选择未来消息的更多细节(在此case,'我输入x / y / etc')。该消息可以像客户端发送一个字节一样简单(如果您考虑超过256种可切换类型,则为short或int)作为流中的第一件事。



连接类型应该是每个连接模型对象的属性,一旦你完成了它,无论是基于端口还是消息(或其他信息,例如客户端IP,配置)都无关紧要设置等)。所以你不应该使用像端口号一样稀缺的东西来执行一个切换任务。



如果(并且只有)你不控制协议,你应该只需启动多个服务器套接字并在每个服务器套接字上使用异步套接字I / O(例如,调用BeginAccept)即可。但我确实强调,如果您可以控制架构和协议,这听起来就像您从描述中所做的那样,您应该接受通过相同端口的所有通信,并从客户端和服务器之间发送的消息中解决所有其他问题。


我在这里得到了答案:

http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/09828be4-6ac4-45ec-a116-508314dab793 [ ^ ]


Hello,

i have to program a server, where I have at most (let say) 4 Clients. and each Client is to be listened to a particular port, means a port is assigned to a Client, and that Client will send all data to that port only.

it means i have to keep all 4 ports in listen mode at a time.

Now i am able to establish communication between a Client and Server on a single port, send and receive data.

I searched and found something similar here (but its for Java, and i need something similar for .Net) http://stackoverflow.com/questions/5079172/java-server-multiple-ports[^]

please Help, how can i create such Socket Connection.

I need to do this :

1. Start server;
2. Listen for incoming connections on several ports;
3. Identify the port being connected to;
a. If port 1, start a thread listening to client and outputting message type x
b. If port 2, start a thread listening to client and outputting message type y

thanks in advance.

解决方案

This is almost certainly the wrong approach. Port numbers are a precious resource and if you have control over the protocol, it is much better to only listen on one, well known port and have the first message in the protocol be a switching message that chooses further details of future messages (in this case, 'I am type x/y/etc'). That message can be as simple as the client sending a byte (or short or int if you are thinking of more than 256 switchable types) as the first thing in the stream.

The type of connection should be a property of each connection model object, and once you've worked it out, it doesn't matter whether it was based on a port or a message (or other information, e.g. client IP, configuration settings, etc). So you shouldn't use something as scarce as port numbers to perform a menial switching task.

If (and only if) you do not control the protocol, you should do this simply by starting several server sockets and using the asynchronous socket I/O (e.g. call BeginAccept) on each one. But I do stress that if you have control over the architecture and the protocol, which it sounds like you do from your description, you should accept all communication through the same port and work everything else out from messages sent between client and server.


I got my answer here :
http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/09828be4-6ac4-45ec-a116-508314dab793[^]


这篇关于在单个服务器上侦听多个端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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