Polling servers at the same port - Threads and Java

Posted by John on Stack Overflow See other posts from Stack Overflow or by John
Published on 2010-05-29T16:53:02Z Indexed on 2010/05/29 19:42 UTC
Read the original article Hit count: 215

Filed under:
|
|

Hi there.

I'm currently busy working on an IP ban tool for the early versions of Call of Duty 1. (Apparently such a feature wasn't implemented in these versions).

I've finished a single threaded application but it won't perform well enough for multiple servers, which is why I am trying to implement threading.

Right now, each server has its own thread. I have a Networking class, which has a method; "GetStatus" -- this method is synchronized. This method uses a DatagramSocket to communicate with the server. Since this method is static and synchronized, I shouldn't get in trouble and receive a whole bunch of "Address already in use" exceptions.

However, I have a second method named "SendMessage". This method is supposed to send a message to the server. How can I make sure "SendMessage" cannot be invoked when there's already a thread running in "GetStatus", and the other way around? If I make both synchronized, I will still get in trouble if Thread A is opening a socket on Port 99999 and invoking "SendMessage" while Thread B is opening a socket on the same port and invoking "GetStatus"? (Game servers are usually hosted on the same ports)

I guess what I am really after is a way to make an entire class synchronized, so that only one method can be invoked and run at a time by a single thread.

Hope that what I am trying to accomplish/avoid is made clear in this text.

Any help is greatly appreciated.

© Stack Overflow or respective owner

Related posts about java

Related posts about multithreading