views:

32

answers:

2

i have a java game app that uses sockets to communicate with each other.

the issue is when i do a socket listen (server), i can run another instance of the game on the same machine using the same port as before to listen, and it results in listening again. now i have two instances of the application both listening on the same port. you can imagine only one connects when a connection comes through.

the question is: how do i prevent the app from listening on the same port as another instance is already listening to?

thanks in advance.

EDIT: serverSocket = new ServerSocket(serverPort, backlog); im using this. should i try to use: ServerSocket(int port, int backlog, InetAddress bindAddr) instead?

EDIT: SOLVED! i did not handle the exception only trapped it. now it is working well. thanks for your inputs.

A: 

Only one OS process can have a server socket open on a particular port. The language used to implement the process does not matter; it's a restriction of TCP socket addressing.

(OK, it's not strictly true when you start fiddling around with binding addresses to sockets. But it's a very good first approximation; the ways in which it is not true tend to be not very useful for general programs.)

Donal Fellows
+1  A: 

It's not possible for two applications to listen to the same port with the same IP. The second application will get an Exception "port already in use".

tangens
well thats what i thought but the reality is what i reported in the post. any ideas?
iEisenhower
when i use tcpview to monitor connections the second server does not add a new listening in the connections list. so that good but im lost as to why it is not reporting port already in use.
iEisenhower
i will give you the answer as you mentioned "Exception".
iEisenhower