Name: mc57594 Date: 02/06/97
The complete source code is at the bottom of this message
aFromClient = new ObjectInputStream(aSock.getInputStream()); // line y
aToClient = new ObjectOutputStream(aSock.getOutputStream()); // line x
The program blocks on the first line.
(The problem seens to lie in the ObjectInputStream constructor
because getInputStream() seemed to work well)
If you swap the two lines, everything works well.
================================================================
Sample code:
Note: the problem is only in the server process
Server.java :
-----------------------------------------------------------
import java.net.*;
import java.io.*;
public class Server {
public static void main(String[] pArgs) {
ObjectOutputStream aToClient = null;
ObjectInputStream aFromClient = null;
ServerSocket aListenSock = null;
String aConnectionID = null;
Socket aSock = null;
try {
aListenSock = new ServerSocket(12345);
System.out.println("Waiting for client connection...");
aSock = aListenSock.accept();
} catch (IOException e) { System.out.println(e); }
// handle client connection
System.out.println("Setting object streams...");
try {
System.out.println("1");
// swap line x and line y and it will work
aFromClient = new ObjectInputStream(aSock.getInputStream()); // line y
System.out.println("2");
aToClient = new ObjectOutputStream(aSock.getOutputStream()); // line x
System.out.println("3");
} catch(UnknownHostException e) { System.out.println(e);}
catch(StreamCorruptedException e) { System.out.println(e);}
catch(IOException e) { System.out.println(e);}
// register connection ID
System.out.println("Trying to register new connection...");
try {
aConnectionID = (String) aFromClient.readObject();
} catch(UnknownHostException e) { System.out.println(e);}
catch (StreamCorruptedException e) { System.out.println(e);}
catch (IOException e) { System.out.println(e);}
catch (ClassNotFoundException e) { System.out.println(e);}
System.out.println("Read string: " + aConnectionID);
try {
aSock.close();
} catch (IOException e) { System.out.println(e); }
}
}
-----------------------------------------------------------
Client.java
-----------------------------------------------------------
import java.net.*;
import java.io.*;
public class Client {
public static void main(String[] pArgs) {
Socket aSock = null;
ObjectInputStream aFromServer = null;
ObjectOutputStream aToServer = null;
boolean lSuccessfulConnection = true;
try {
aSock = new Socket("localhost", 12345);
aFromServer = new ObjectInputStream(aSock.getInputStream());
aToServer = new ObjectOutputStream(aSock.getOutputStream());
}
catch (UnknownHostException e) {
System.out.println(e);
lSuccessfulConnection = false;
}
catch (IOException e) {
System.out.println(e);
lSuccessfulConnection = false;
}
if (lSuccessfulConnection == true) {
try {
aToServer.writeObject("Hello");
} catch(IOException e) {
System.out.println(e);
}
}
try {
aSock.close();
} catch (IOException e) { System.out.println(e); }
}
}
company - Beltron Technologies (http://www.beltrom.com) , email - ###@###.###
======================================================================