File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reseting socket after few seconds

 
Jigar Naik
Ranch Hand
Posts: 761
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jigar Naik
Ranch Hand
Posts: 761
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 761
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic