| Author |
Reseting socket after few seconds
|
Jigar Naik
Ranch Hand
Joined: Dec 12, 2006
Posts: 744
|
|
Hi,
When i send continues messages from client - > server -> client my program stops responding... i mean the main thread is alive but nothing prints on console after sending few messages. and in that case currently i have to close the socket and open new connection between client and server.
is there any other way ? am i missing to clean the resources somewhere ?
the program works like comp A sends messages to B in infinite loop and B acknowledge back to A.
and below are the methods i have written for client & server
Server Code
Client Code
|
Jigar Naik
|
 |
Wouter Oet
Saloon Keeper
Joined: Oct 25, 2008
Posts: 2700
|
|
That is a lot of code. A couple of things stand out.
You're recreating ServerSocket's which is not how it should be done.
I would advise you to read the Socket tutorial.
Why are you creating a new ObjectOutputStream/ObjectInputStream every time you want to send/receive a message. Just create it once.
I would create a SocketHandler class which accepts a Socket from the ServerSocket and handles the communication.
Then the resources will be much easier to control.
|
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
|
 |
Jigar Naik
Ranch Hand
Joined: Dec 12, 2006
Posts: 744
|
|
Hi Wouter,
Thanks for the details.
Yeah i have gone through the tutorial.
Why are you creating a new ObjectOutputStream/ObjectInputStream every time you want to send/receive a message. Just create it once.
As you said i tried that, i moved the out = new ObjectOutputStream(clientSocket.getOutputStream()); into createScoket() method which is getting called only once. but it's throwing out of memory exception after sending few messages. and after getting out of memory exception I am not able to understand the behavior of out.write() and out.flush() method.
You're recreating ServerSocket's which is not how it should be done.
Actually at the client side my program was not reading the messages and that's why IO was getting blocked which was my silly mistake. but now i figured it out so i am no more creating a new ServerSocket.
I would create a SocketHandler class which accepts a Socket from the ServerSocket and handles the communication.
Then the resources will be much easier to control.
Can you please explain in detail, like what will be the method's in the SocketHandler class ? I am a bad designer, only computer can understand my code. Human Can Not I am trying my level best to learn.
Thanks & Regards,
Jigar
|
 |
Wouter Oet
Saloon Keeper
Joined: Oct 25, 2008
Posts: 2700
|
|
About the exception. I'm guessing that It's caused by sending a very large message without invoking flush() to be sure profile your application and look where the memory is used.
I would create something like:
|
 |
Jigar Naik
Ranch Hand
Joined: Dec 12, 2006
Posts: 744
|
|
That makes the application really simple.
Thanks a lot. Yes i will profile my application and will post the results. i am sending data of Min : 50 Bytes and Max 3000 bytes which is configurable.
Thanks & Regards,
Jigar
|
 |
 |
|
|
subject: Reseting socket after few seconds
|
|
|