-
Bug
-
Resolution: Not an Issue
-
P4
-
None
-
1.1.7
-
x86
-
windows_nt
Name: dbT83986 Date: 02/17/99
In implementing a client/server application, I want to pass
objects between the client and the server. This is to be done
using a int and objects via Object Streams. The following two
(2) classes demonstrate the error with ReadIntClient and
ReadeIntServer.
There are two (2) sets of lines (about line 57 Server, 47 Client)
- the writeObject/readObject works fine, but
- the writeInt/readInt fails with the following exception:
"java.net.SocketException: Connection reset by peer"
The code below has the writeObject/readObject commented out and
will fail. If you comment out the writeInt/readInt and uncomment
the writeObject/readObject, it works fine.
To run, compile and run first the server then the client.
I use two (2) command prompt windows and type:
(Server)> java readint.ReadIntServer
(Client)> java readint.ReadIntClient
What am I missing?
//************************************
// Class ReadIntClient
//************************************
package readint;
import java.io.*;
import java.net.*;
/** Client class for readInt test. */
public class ReadIntClient
{
//// Constructors ////
public ReadIntClient()
{
int port = 5432;
String server = null;
Socket socket = null;
// Use your local machine as the server.
try
{
server = (InetAddress.getLocalHost()).getHostAddress();
}
catch (UnknownHostException e)
{
System.out.println("Error: Unable to get IP address: " + e);
System.exit(-1);
}
// Open the socket to the server.
try
{
socket = new Socket(server, port);
}
catch (IOException e)
{
System.out.println("exception, Test failed: " + e);
System.exit(-1);
}
try
{
// Open the Output Stream FIRST!
// See Java Developers Connection Bug Id 4030767. dww 12-23-98
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
System.out.println("In try, before write.");
// oos.writeObject("Bob");
// System.out.println("In try, after writeObject.");
oos.writeInt(1);
System.out.println("In try, after writeInt.");
}
catch (Exception e)
{
System.out.println("Exception! - " + e);
}
finally
{
System.out.println("In finally, did it work?");
}
} // default constructor ReadIntClient()
//// Methods ////
public static void main(String args[])
{
ReadIntClient me = new ReadIntClient();
} // main()
} // class ReadIntClient
//************************************
// Class ReadIntServer
//************************************
package readint;
import java.io.*;
import java.net.*;
/** Server class for readInt test. */
public class ReadIntServer
{
//// Constructors ////
public ReadIntServer()
{
int port = 5432;
String server = null;
ServerSocket s = null;
Socket s1 = null;
// Get the local IP address to run on.
try
{
server = (InetAddress.getLocalHost()).getHostAddress();
}
catch (UnknownHostException e)
{
System.out.println("Error: Unable to get IP address: " + e);
System.exit(-1);
}
// Setup the server side socket
try
{
s = new ServerSocket(port);
}
catch (IOException e)
{
System.out.println("Error: Unable to open server socket: " + e);
System.exit(1);
}
// Output the IP address and port to the console.
System.out.println("** Starting Server [" + server + ":" + port + "] **");
try
{
// Wait here and listen for a connection
s1 = s.accept();
// Open the Output Stream FIRST!
// See Java Developers Connection Bug Id 4030767. dww 12-23-98
ObjectOutputStream oos = new ObjectOutputStream(s1.getOutputStream());
ObjectInputStream ois = new ObjectInputStream(s1.getInputStream());
System.out.println("Got Client, before read.");
// String a = (String)ois.readObject();
// System.out.println("After readObject - Got: <" + a + ">");
int b = ois.readInt();
System.out.println("After readInt - Got: <" + b + ">");
}
catch (Exception e)
{
System.out.println("Error: exception, Test failed: " + e);
System.exit(-1);
}
} // default constructor
//// Methods ////
public static void main(String args[])
{
ReadIntServer myServer = new ReadIntServer();
} // main(args)
} // class ReadIntServer
(Review ID: 49030)
======================================================================