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: server开发者_开发问答Socket = 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.
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".
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.)
精彩评论