Click here to Skip to main content
14,978,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)

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.
Updated 18-Feb-18 20:15pm

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.
Member 13684712 19-Feb-18 2:17am
I am trying to create a program where server can listen to one port and on some event occurrence it will stop listening that port and start listening other port and after listening the other port it should resume the previous port.
Please help me with this.

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

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900