Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# .NET TCP/IP multithreading , +
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.
Posted 9-Jan-12 2:40am
Edited 9-Jan-12 2:42am
v2
Rate this: bad
good
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.
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 839
1 OriginalGriff 400
2 CPallini 275
3 George Jonsson 191
4 Richard Deeming 145
0 OriginalGriff 5,450
1 Sergey Alexandrovich Kryukov 4,532
2 CPallini 4,500
3 George Jonsson 3,102
4 Gihan Liyanage 2,445


Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 13 Jan 2012
Copyright © CodeProject, 1999-2014
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