ObjectStream issues

E

espresso

I'm having some difficulty with ObjectInputStream and
ObjectOutputStream in a client server application using sockets. Here
is the relevant code:

client...

System.out.println("Connecting to server...");
clientSock = new ObjectSocket("localhost", PORT);


System.out.print("Message to server? ");

clientSock.writeObject(hr); // send to server
System.out.print("Reply from server: ");
hr= (HRProtocol)clientSock.readObject(); // read response
from server
dept= (Department)hr.getArg();

ObjectSocket constructors:

public ObjectSocket(String host, short port)throws
UnknownHostException, IOException {
this(new Socket(host,port));

}

public ObjectSocket(Socket s)
throws UnknownHostException, IOException{
theSocket = s;
oss = new ObjectOutputStream(theSocket.getOutputStream());
oss.flush();
ois = new ObjectInputStream(theSocket.getInputStream());


}

Server code :

ServerSocket serverSock = new ServerSocket(PORT);
while(true){
System.out.println("Waiting for client connection...");
ObjectSocket clientSock = new ObjectSocket(serverSock.accept());
// wait for client request
System.out.println("Connected to client...creating processing
thread");
Thread newServerThread = new Thread(new
DBServerThread(clientSock)); // create new request processing thread
newServerThread.start()

ServerThread code:

ObjectSocket clientSock = null;
public DBServerThread(ObjectSocket o) {
clientSock = o;
}

public void run() {

HRProtocol hrp=null;
HRDatabase hrdb=null;
Department dept=null;
Employee emp=null;
int deptno = 0;
String empid="";
Boolean result=null;

try {

hrp = (HRProtocol) clientSock.readObject(); // read Object
from client
int code = hrp.getCode();
switch(code){
//department find request
case HRPROTOCOL_FIND_DEPARTMENT_REQUEST:
deptno = Integer.parseInt(hrp.getArg().toString());
//get dept using HRDatabase object
dept = hrdb.findDepartment(deptno);
//reset HRPROTOCOL object
hrp.setCode(HRPROTOCOL_FIND_DEPARTMENT_RESPONSE);
hrp.setArg(dept);
clientSock.writeObject(hrp);
break;

Everything works fine until I get to this part of the client code
where the progam hangs:

hr= (HRProtocol)clientSock.readObject();

Any ideas about what the problem is here?

Thanks
 
D

Dave Monroe

Everything works fine until I get to this part of the client code
where the progam hangs:

hr= (HRProtocol)clientSock.readObject();

Any ideas about what the problem is here?

The order in which you open the streams is important.

The simple answer (and there's more to it) is open the output stream
first on both ends and send a .flush() to wake the other guy up.

e.g.

clntout = new ObjectOutputStream(sock.getOutputStream());
clntout.flush();
clntin = new ObjectInputStream(sock.getInputStream());

That should fix it.
 
E

Esmond Pitt

The order in which you open the streams is important.
The simple answer (and there's more to it) is open the output stream
first on both ends and send a .flush() to wake the other guy up.

He'a doing that. I think he needs another flush after he writes the object.
 
E

espresso

Esmond Pitt said:
He'a doing that. I think he needs another flush after he writes the object.

I've tried the .flush() after write--it still hangs at the same place.

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top