Code with lines commented-out works, code with those lines does not.....why? and how do I fix it? (note: roughly same code running on the other end)
public void run() {
try {
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream);
//ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
while (true) {
//System.out.println( ois.readUTF());
oos.writeUTF("hey");
}
} catch (Exception e) {e.printStackTrace(); }
}
Edit: Made some code to make it clearer what I'm getting at, yes its a little messy. (regardless of there being two sockets or not it doesn't seem to work):
Server.java (main):
public static void main(String[] args) {
try {
ServerSocket server = new ServerSocket(6666);
ServerSocket server2 = new ServerSocket(6667);
Socket socket = server.accept();
Socket socket1 = server2.accept();
OutputStream output = socket.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(output);
ObjectInputStream ois = new ObjectInputStream(socket1.getInputStream());
while (true) {
System.out.println("server reading");
System.out.println( ois.readUTF());
System.out.println("server writing");
oos.writeUTF("hey");
}
} catch (Exception e) {e.printStackTrace(); }
}
Client.java (main)
public static void main(String[] args) throws Exception {
Socket socket = new Socket("127.0.0.1", 6666);
Socket socket1 = new Socket("127.0.0.1", 6667);
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
ObjectOutputStream oos = new ObjectOutputStream(socket1.getOutputStream());
while (true) {
System.out.println("client writing");
oos.writeUTF("yo");
System.out.println("client reading");
System.out.println( ois.readUTF() );
}
}
Server:
server reading
Client:
client writing
client reading
.flush() your output streams. In my examples after writing on the output stream I have:
oos.flush();
I should have always flushed when I wanted to be sure the message was 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.