• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reseting socket after few seconds

 
Ranch Hand
Posts: 763
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 763
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 763
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic