Only 49 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



  • Post Reply Bookmark Topic Watch Topic
  • New Topic

No data send trough socket  RSS feed

 
Roel Van Zeebroeck
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

This is is the first time I'm working with sockets. I'm running a distributed application which consitst of a server and different clients (though only one client was connected when this problem occured). The clients connect to the server using Sockets. The server uses a seperate thread for each client to constantly check for messages. If something goes wrong with a connection, IOExceptions are caught and the connection is reestablished.

At a certain moment, the server tried to send a message to a client, which seemed to be still connected.

The following code is used to send the message:


For some reason, the message was never received at the client. The statements on the server (output.write(), output.flush()) were executed without any exception being thrown. Please not that this code has worked without any problem for a few weeks now).

The only thing I can think of is that there was some connection problem. But then I wonder why no IOException was thrown at the server. I know that an IOException is not automatically thrown when the connection is lost, but I would expect that it is at least thrown when the server tries to send something to the client.

Does anyone know what could have caused such behavior? Or is maybe my knowledge about sockets too small to really understand what's going on?

Thanks in advance

Roel
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should spend a little time with the Java Networking Tutorial. A little time up front will save you a lot of time wandering around.
As for your problem, how are you reading the data on the client?
 
Roel Van Zeebroeck
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree. I should take some time to read more about how the technology exactly works. I will do exactly that.

This is the code I use at the client side:


The disconnect method will close everything (socket, streams, ...) and a new connection will be set up when that happens.
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see anything obviously wrong. My advice is to start fresh. Get a client and server exchanging simple text messages. Once you are comfortable with that, add features like your format byte.
 
Roel Van Zeebroeck
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess I'll have to look a bit harder to find what's wrong.

I have a few straightforward questions though, of which I'm not 100% sure about the answer:
1) If there is a problem with the connection, does the server side always throw an IOException when the read() method is called?
2) If there is a problem with the connection, does the server side always throw an IOException when the write() method is called?
3) Are the answers to questions 1) and 2) the same for client and serverside?

I ask these questions also because I noticed the follwowing behaviour: when I close a client application, it takes a while after I see the IOException on the server. 'a while' varies a lot: sometimes it's a few minutes, sometimes it is longer than an hour. Is this normal? Maybe this indicates a bug in my code.

Thanks again for your help.
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If there is a problem with the connection, I'm pretty sure you'd know immediately. If the connection is established and the client or server goes away, it may be some time before an exception is thrown depending on the state of the connection. If the server is not blocked on a read and does not attempt to write, there's really nothing to tell it that a client has gone away.
As I said before, chuck your current code because it doesn't work (you can always come back to it later). Read the tutorial. Start with some fresh code, get it working and add features one at a time.
 
Roel Van Zeebroeck
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's good to know that I should know a connection problem immediately when doing a write or haning on a read.

When I was reading the tutorial I remembered that I actually did read it before starting to program. In fact, I started from the echo server in the tutorial and added feature by feature. I probably didn't test enough between features.

My problem now is that the code works fine most of the time, and I need to find some way to reproduce the error. The only weird behavior that I can reproduce is the fact that it takes so long for the server to throw an IOException. I don't think that is normal because he is constatly in a loop that hangs on read(). I think something is wrong there. So I'll follow your advice and start with a program where the server throws the IOException immediatly and work my way up.

Something just came up my mind too. I'm using an SSLSocket and SSLServerSocket in stead of just normal Sockets. Could it be possible that they cause unexpected behavior? I'll try that right now just to be sure.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!