Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

socket not accepting data 2nd time

 
Greenhorn
Posts: 20
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am writing a client server program in which the client accepts data from the user along with information on type of operation to be performed. It then passes this information to the server which performs the operation and returns the result to the client. I am using an array-list object to pass data to the server. It works fine the 1st time but after that the server doesn't accept the next array list object for the next operation. Please tell me what is wrong with my code. Thank you.

Here is the server code:


and here is the client code:
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please note that ItDoesntWorkIsUseless.(⇐click) It would help if you would TellTheDetails(⇐click) about what "the server doesn't accept it" means. What exactly are you observing? In which code in particular, at least the method, preferably the specific line or two? Is there an error message? If so, copy/paste the entire thing and indicate clearly which code is causing it.
 
Joel Bijapurkar
Greenhorn
Posts: 20
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The above code does not give me an error during run time. But when I terminate the execution of the client and server programs I get the following error messages:

1) These error messages occur in the server output window when I terminate the client first

SEVERE: null
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:189)
at java.net.SocketInputStream.read(SocketInputStream.java:121)
at java.net.SocketInputStream.read(SocketInputStream.java:203)
at java.io.ObjectInputStream$PeekInputStream.peek(ObjectInputStream.java:2266)
at java.io.ObjectInputStream$BlockDataInputStream.peek(ObjectInputStream.java:2559)
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2569)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1315)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
at socketprogramming.Server.main(Server.java:36)



1) These error messages occur in the client output window when I terminate the server first

in the client output window :
at java.net.SocketInputStream.read(SocketInputStream.java:189)
at java.net.SocketInputStream.read(SocketInputStream.java:121)
at java.net.SocketInputStream.read(SocketInputStream.java:203)
at java.io.DataInputStream.readLine(DataInputStream.java:513)
at socketprogramming.Client.main(Client.java:111)
 
Joel Bijapurkar
Greenhorn
Posts: 20
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Finally found the solution.

In my server program the statement client = ss.accept() on line 21, creates a new object every time it executes . After executing once my client program is still trying to establish a connection to the old "client" object in the server program which no longer exits.

So I modified the client code and created a new instance of the socket "s" every time i want to communicate with the server. Socket "s" will then communicate with the new "client" object in the server program. I moved lines 12 -24 of the client code into the while loop beginning at line 25.

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joel Bijapurkar wrote:The above code does not give me an error during run time. But when I terminate the execution of the client and server programs I get the following error messages:



Then it does give you an error at runtime.

Glad you figured it out though.
 
Yeah, but does being a ninja come with a dental plan? And what about this tiny ad?
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic