I am working on Java streams, In server side I'm trying to send int through objectOutputStream and recieve it in client side. However I am not receiving anything in client side.
Here is the server side sending int
public static int PLAYER1 = 1;
new ObjectOutputStream(player1.getOutputStream()).writeInt(PLAYER1);
Here is the client side receiving that int:
fromServer = new ObjectInputStream(socket.getInputStream());
Here is the part where I test the recieved int:
int player = fromServer.readInt();
if(player == PLAYER1){
System.out.println("yy working");
}else{
System.out.println("not working");
}
The problem is that neither I get an error nor system out.. I am using ObjectStreams and not DataStreams because of some reasons.
Your code doesn't seem to survive the call int player = fromServer.readInt();
. I would debug the call in readInt() or before that to see where it jumps out.
Call close()
or flush()
on server side:
public static int PLAYER1 = 1;
ObjectOutputStream oos = new ObjectOutputStream(player1.getOutputStream());
oos.writeInt(PLAYER1);
oos.flush();
// OR
oos.close();
flush()
will send any data already written to the client. This will leave you the possibility to reuse oos
.
If you don't need oos
anymore, call close()
. Calling close()
will flush any data not already sent.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.