Sign in to follow this  
stu2000

[java] Java socket problems

Recommended Posts

stu2000    101
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);
CommunicationProtocol(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("system:"+broadcast.systemName);
System.out.println("reporter:"+ broadcast.nameOfCharacter);
addingConnection.GetOutputStreamWriter().write("Intelligence: " + broadcast.getAge() + "," + broadcast.systemName + "," + broadcast.nameOfCharacter + "\r\n");
addingConnection.GetOutputStreamWriter().flush();

}


//the ping happens in this bit in a continuous while(true) loop of the thread
connection.pingFrameCount++;
if(connection.pingFrameCount == 119)
{
//this needs to be performed to test the connection is still live
connection.pingFrameCount = 0;
connection.GetOutputStreamWriter().write("ping\n");
connection.GetOutputStreamWriter().flush();
}

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.
Stu

Share this post


Link to post
Share on other sites
elFarto    206
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);
CommunicationProtocol(message);
}
}
});
t.start();


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.

Regards
elFarto

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this