Hi! I created a server / client program where the server sends an object (array) to the client (both @ local ip's), the client processes it and sends it (array) back to the server...the client side prints the "Sent object to server..." however the server doesn't receive it, it just comes up with a: "java.net.SocketException:Connection reset by peer:JVM_recv in socket input stream read" i suspect an i/o stream conflict, but then again if i knew 4 sure y would i post this?
if u wanna help, have a solution and/or need more code/info please email me at:
mohrjee@hotmail.com
thank you all. =P
Server:
System.out.println("SERVER STARTED...\n");
//Create Server Socket
ServerSocket servSock = new ServerSocket(8000);
//Listen on serversocket for connection attempts and continue when a client actually does connect
System.out.println("Waiting for Client to Connect...");
Socket connectToClient = servSock.accept();
System.out.println("Connection Accepted!");
//Create input and output streams for data from/to client
System.out.println("Creating Input and Output Streams...");
ObjectInputStream ois = new ObjectInputStream(connectToClient.getInputStream());
ObjectOutputStream oos = new ObjectOutputStream(connectToClient.getOutputStream());
System.out.println("Streams created!");
//Loop forever (at least until there is no IP connection, at which point an exception will be thrown)
while(true)
{
System.out.println("Waiting...");
//Send array to client
System.out.println("Sending object to client...");
oos.writeObject(aDetails);
System.out.println("Object sent to client!");
//Receive array from client
System.out.println("Waiting for object from client...");
aDetails =(AddressData[])ois.readObject();
System.out.println("Object received from client!");
//Save (modified) array (to addresses.adr):
SaveData(aDetails);
}
}
catch(Exception ex)
{
System.err.println(ex);
}
CLIENT:
publicstatic AddressData[] ReadData(AddressData[] aDetails)
{
try
{
System.out.println("CLIENT STARTED...\n");
//Create instance of socket
Socket connectToServer = new Socket("localhost", 8000);
//Setup input and output stream from/to server
ObjectOutputStream oos = new ObjectOutputStream(connectToServer.getOutputStream());
ObjectInputStream ois = new ObjectInputStream(connectToServer.getInputStream());
System.out.println("Waiting for object from server...");
aDetails =(AddressData[])ois.readObject();
System.out.println("Object received from server!");
}
catch(Exception ex)
{
System.err.println(ex);
}
return aDetails;
}
publicstaticvoid WriteData(AddressData[] aDetails)
{
try
{
//Create instance of socket
Socket connectToServer = new Socket("localhost", 8000);
System.out.println("Connection Accepted!");
//Setup input and output stream from/to server
//ObjectInputStream ois = new ObjectInputStream(connectToServer.getInputStream());
ObjectOutputStream oos = new ObjectOutputStream(connectToServer.getOutputStream());
System.out.println("Streams created!");
System.out.println("Sending object to server...");
oos.writeObject(aDetails);
System.out.println("Object sent to server...");
}
catch(Exception ex)
{
System.err.println(ex);
}
}
//input: AddressData object aDetails
//Create Server Socket
ServerSocket servSock = new ServerSocket(8000);
//Listen on serversocket for connection attempts and
// continue when a client actually does connect
while (true)
{
System.out.println("Waiting for Client to Connect...");
Socket connectToClient = servSock.accept();
System.out.println("Connection Accepted!");
// Create an instance that will take care of the
// Socket.
new ProcessSocketIO (connectToClient ).start ();
...
class ProcessSocketIO
extends Thread // For the sake of brievety (you could implement a Runnable)
{
Socket s = null;
ProcessSocketIO (Socket aSocket)
{
s = aSocket;
} // do as little a possible
/**
*
*/
publicvoid run ()
{
// Do whatever you want with the Socket instance you just 'got'
InputStream is = s.getInputStream ();
OutputStream os = s.getOutputStream ();
// Do what you wanna do here
}
} // End of inner class
}
What you need to keep in mind is that all the handling or IO's is better done in separated threads (do not have two threads that handle Input for one Output for the second, though... ).
Do you Serialize your Object as they should ? Also it would be of interest if you did post the Network topology :-)