Click here to Skip to main content
13,298,361 members (77,152 online)
Rate this:
Please Sign up or sign in to vote.

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)[^]

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.
Posted 9-Jan-12 3:40am
Updated 9-Jan-12 3:42am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.171207.1 | Last Updated 13 Jan 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100