The Artima Developer Community
Sponsored Link

Java Answers Forum
What the heck is wrong with my server code? =[

1 reply on 1 page. Most recent reply: Jun 23, 2002 3:18 PM by Thomas SMETS

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
moh r jee

Posts: 3
Nickname: mohrjee
Registered: Jun, 2002

What the heck is wrong with my server code? =[ Posted: Jun 23, 2002 10:39 AM
Reply to this message Reply
Advertisement
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:
public static 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;
	}
	
	public static void 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


Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: What the heck is wrong with my server code? =[ Posted: Jun 23, 2002 3:18 PM
Reply to this message Reply
Probably changing your code to this should help
//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
 
    /**
     *
     */
    public void 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 :-)

Rgds,

Thomas SMETS,
SCJP2 - Brussels

Flat View: This topic has 1 reply on 1 page
Topic: pause in java Previous Topic   Next Topic Topic: adding action listioner to user defined component

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use