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: 3140
posted
0
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.
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)
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: 3140
posted
0
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: