Stackie Overflower
Stackie Overflower

Reputation: 211

Multiple java socket connections

I have created this game bot where it connects to the game, and starts playing. My problem is that i can't start more than one of these as the other then won't work.

Is is possible that if i run 2 instances of the same program the sockets are interfering with each other ? After all, they do connect to the same IP with the same port ?

And sometimes after i close(just closing cmd) the program is unable to connect again. Is that cause i didn't close the connections right ?. I hope this is enough else i'll just have to post my source code

Best regards.

Upvotes: 2

Views: 4232

Answers (3)

tartar
tartar

Reputation: 688

if you are working with a specific TCP port, then there is a close-wait period that this port cannot be claimed temporariliy for some time. also multiple programs cannot listen the same TCP port. Use threads.

Upvotes: 1

Darren
Darren

Reputation: 70718

You should have a server that listens for multiple connections. A server is bound to a port and once that port is in use another application cannot use it. So for the server just have one instance. Multiple clients can connect to this IP/Port as long as the Server accepts multiple connections.

If a client connects to the server and the other clients stop working this may be because the server does not support multiple clients. To do this you need to use multi threading in the server. The server should accept a client socket and create a new instance of a client with it's own StreamReader/Writer objects.

http://tutorials.jenkov.com/java-multithreaded-servers/multithreaded-server.html

Upvotes: 1

unludo
unludo

Reputation: 5010

It's possible to connect to the same socket/port several times. Actually a socket is a double peer: {client ip/ client port}{server ip/server port}. When you connect to a server, your client port is assigned dynamically. You will have a new and different client port per client. So it should work unless the server side forbid it.

Upvotes: 1

Related Questions