1

I am using Java to do the socket programming as below.

Client program is as below:

 Socket MyClient;
    try {
           MyClient = new Socket("Machine name", PortNumber);
    }
    catch (IOException e) {
        System.out.println(e);
    }

Server program is as below:

ServerSocket MyService;
    try {
       MyServerice = new ServerSocket(PortNumber);
        }
        catch (IOException e) {
           System.out.println(e);
        }

Socket clientSocket = null;
    try {
       clientSocket = MyService.accept();
        }
    catch (IOException e) {
       System.out.println(e);
    }

Now my question is if I run more than one thread to open several sockets in one port (as the server code above), how my client program know which socket it is connecting to?

Sam YC
  • 10,725
  • 19
  • 102
  • 158

3 Answers3

3

Your client connects to the Servers port. So all clients will be having the same code MyClient = new Socket("Machine name", <port where server is listening>);
The port opened at client side is not important. The client will get a free port available in his OS.

basiljames
  • 4,777
  • 4
  • 24
  • 41
  • This is not what I ask. I want to know how the client know which socket that it is going to connect in server side? Because the server program create multiple thread to do that. – Sam YC Sep 12 '12 at 05:16
  • @GMsoF This is a perfectly reasonable answer, given that the question you asked doesn't actually make sense. – user207421 Sep 12 '12 at 07:07
2

how my client program know which socket it is connecting to?

The question doesn't make sense. It doesn't 'connect to a socket' at all, it connects to a listening port, and there is only one of those. Your server only accepts one client, so the second and subsequent threads will get an undefined behaviour ranging from a ConnectException to a ConnectionException to nothing, most probably the latter.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • I saw some article said that we can create more than one socket from one port. http://stackoverflow.com/questions/700594/how-many-sockets-can-be-created-from-a-port that is why I am confused. But your point make sense as well. How to interpret this thing? – Sam YC Sep 12 '12 at 04:45
  • I am getting more confused now, for my sample code, it totally follow the TCP/IP rule which is "one process run on one port", although it create multiple thread to do that.... – Sam YC Sep 12 '12 at 05:14
  • 1
    @GMsoF The server can accept as many sockets as it likes from a single listening port. Your server doesn't do that. There is no such rule in TCP as 'one process one port'. If you did that, servers would be impossible to write. – user207421 Sep 12 '12 at 05:20
  • OK, I think I almost get it. Allow me to ask one more question. If I have a program 'A' listening on port 1111 after that one client successfully connect to the socket A that was created in program A. While the socket A is being kept (communication is still going but program A is not listening on port 1111 anymore, it is enough with one socket), can program B listening on port 1111 and trying to create a socket B under port 1111 while socket A is still opened? – Sam YC Sep 12 '12 at 06:45
  • 1
    @GMsoF Yes but with difficulty. It's a very strange thing to do. The programs would have to co-operate to know when B could start listening, and the clients would also have to co-operate so that the one that expected to connect to A ran before the one that expected to connect to B. It would make much more sense to combine the servers. A port should only be used for one *protocol*. I think that is the sense of the 'rule' you cited above. – user207421 Sep 12 '12 at 07:06
0

Your application knows it because you set it up with a specific port. There is no "auto discovery" built into TCP/IP, it's up to you to pick a server-port and make sure you set your clients up to connect to that port. Either you hard-code this into your client application or, better yet, have it in some configuration file you include with the client.

This is why you have a bunch of "known ports", like http is port 80. This means that a browser will always connect to port 80 on a web-server, unless you explicitly indicate another port in the URL.

pap
  • 27,064
  • 6
  • 41
  • 46