This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Sockets and Internet Protocols and the fly likes socket not accepting data 2nd time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "socket not accepting data 2nd time" Watch "socket not accepting data 2nd time" New topic
Author

socket not accepting data 2nd time

Joel Bijapurkar
Greenhorn

Joined: Aug 01, 2011
Posts: 20

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:
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Aug 01, 2011
Posts: 20

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

Joined: Aug 01, 2011
Posts: 20

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
 
Don't get me started about those stupid light bulbs.
 
subject: socket not accepting data 2nd time