Jump to content
  • Advertisement
Sign in to follow this  

[java] Java socket problems

This topic is 2788 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey all!
Ive been writing information to sockets and reading it. Its all been going well until now.

I have the following code for reading messages on the client:

String message = "";

while(m_inputStream.available() > 0)
message = m_input.readLine();
System.out.println("Message From server: " + message);

(yes it is surrounded by a try/ catch and compiles and works perfectly.

The problem is that m_inputStream.available() is lying to me at the beginning, but works when the server sends update messages later.

At the moment, the client connnects to the server. The server sends lots of messages (lines) at once providing the client with an up to date 'state' and all subsequent updates are sent across too (each update just takes one line). The updates come across ok (maybe because they are just one line?), but only the first line of the initilization lines comes through, and he rest come through much later when I send an automatic ping approximately 60 seconds later.

the server code is:

//intial stuff when someone connects
for(int s=0; s<this.intelligence.size();s++)
IntelligenceBroadcast broadcast = (IntelligenceBroadcast)intelligence.get(s);
System.out.println("Sending New Client: Intelligence:" + broadcast.getAge());
System.out.println("reporter:"+ broadcast.nameOfCharacter);
addingConnection.GetOutputStreamWriter().write("Intelligence: " + broadcast.getAge() + "," + broadcast.systemName + "," + broadcast.nameOfCharacter + "\r\n");


//the ping happens in this bit in a continuous while(true) loop of the thread
if(connection.pingFrameCount == 119)
//this needs to be performed to test the connection is still live
connection.pingFrameCount = 0;

The ping and initialization lines are written in two different threads, but i know they are getting across ok becuase i have tried with the clinet just continuously outputting any bytes it gets immediately with just a int i = read() and then converting the i to char and writing that out.
The problem with doing that (reading the bytes and outputting it) is I cant figure out a way to stop it from getting blocked. It will get stuck on the condition while((i = blah.read()) != -1).

Any help appreciated. I realise im probably doing all this completely wrong.

Thanks in advance.

Share this post

Link to post
Share on other sites
I've found that available() is not a particularly reliable method. The simplest method for you would probably be to use a separate thread to receive data from the socket.

You would just need to do something like this when you create your client:

Thread t = new Thread(new Runnable() {
public void run() {
while(true) {
String message = m_input.readLine();
System.out.println("Message From server: " + message);

This will allow the socket to block, but not prevent your main loop from being blocked by it.

This, of course, opens you up to synchronisation issues, since CommunicationProtocol can now be called at any time. You could modify the above method to, instead of calling CommunicationProtocol directly, it could place the message in a Queue (either an ArrayBlockingQueue or LinkedBlockingQueue would do) and process that queue in your main loop.


Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!