0

I have developed a web based Java EE application. Recently I had a job to put extra functionality - to be able to send and receive messages to a TCP server which is programmed with .NET platform. Me and the other guy extended our systems so they can communicate and exchange bytes each other.

However there is a big problem and here it is:

It is almost impossible to make it work for more than a few hours without getting some socket I/O error. Sometimes the TCP server needs to be restarted, sometimes my web server gets restarted which shouldn't be a problem because when both servers are running they are pinging themselves with interval of 5 seconds and if a problem occurs the sockets are created again.

But as i mentioned it is almost impossible to exchange messages for more than a few hours without getting some arror.

Unfortunately i have no idea how the TCP server is working which is even more difficult for me to catch the source of the problem.

Is it really that complicated to use Sockets?

Actually i have to as a real question. So here it is:

Can you guys point me some tutorial ( not that simple one on sun's web site ) or tell me something more specific about sockets programming?

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
Joro Seksa
  • 1,525
  • 3
  • 18
  • 44
  • 2
    Are you absolutely certain that there are no network issues? – Lee White Apr 19 '13 at 08:48
  • 2
    "some socket I/O error". Can you post the specific error? It will give more info. – MasNotsram Apr 19 '13 at 08:51
  • even if there are network issues it should start working again when the network issues are removed. Now we have to restart the servers again and again and again... – Joro Seksa Apr 19 '13 at 08:52
  • 1
    When there are network issues (usually this translates itself to ping timeouts), the socket disconnects and you will have to reconnect manually. Reconnecting is often not a trivial matter, with objects needing to be reinitialised and whatnot. If it works when you restart the program, *there is a way to make it work without needing to restart it*. Somewhere you must have wrongful code that is unrelated to how Java and .NET interact with each other. – Lee White Apr 19 '13 at 08:54
  • "Can you point me to some tutorial" is not really the kind of question SO is for... And no, socket programming is not complicated compared to many other areas of programming. It's not trivial either, mind you. You have to know what you are doing and preferably understand a bit about the protocols you are using. – hyde Apr 19 '13 at 09:01
  • Read this documentation. http://docs.oracle.com/javase/tutorial/networking/sockets/index.html See these examples. http://cs.lmu.edu/~ray/notes/javanetexamples/ – prasanth Apr 19 '13 at 08:50

3 Answers3

2

Basic Client-Server Programming in Java.

http://edn.embarcadero.com/article/31995

With the help of notes specified in the tutorial you can almost avoid all the possible beginner mistakes.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
1

The internet if volatile, and keeping a TCP connection permanent is virtually impossible over it. For instance, some firewalls will disconnect it after some time that no info has passed through. The TCP server which you have no access to might do the same.

What I would recommend, is every time before sending data over the connection, check whether it is still active, and if not reconnect.

If you need to receive data from the server as well, so you need the connection alive all the time, you would do best by checking if the connection is active in regular intervals and re-establish it if not.

tbkn23
  • 5,205
  • 8
  • 26
  • 46
  • We are sending handshake message every 5 seconds – Joro Seksa Apr 19 '13 at 08:55
  • Timeout is just an example, there are other reasons that a connection might drop. Unfortunately you can't count on the connection to be permanent, even in an internal network, and definitely not in the internet. You can see some examples here if it interests you: http://stackoverflow.com/questions/251243/what-causes-a-tcp-ip-reset-rst-flag-to-be-sent – tbkn23 Apr 19 '13 at 09:04
0

When communicating over a network you should expect some downtime and write your client and server to be fault tolerant.

The server should detect when a client has dropped off (such as unexpected socket close etc) and should be able to accept a new connection from it. You can create some kind of session logic so that state can be preserved if the client needs to reconnect.

The client should be able to detect when it's connection has dropped to the server, and it should be able to reconnect and continue its job. It shouldn't be hard to automate this behaviour.

If you have any kind of UI then you should show to the user when running offline and that it is trying to reconnect.

Qwerky
  • 18,217
  • 6
  • 44
  • 80