gpt4 book ai didi

java - 套接字 ObjectOutpuStream ObjectInputStream

转载 作者:行者123 更新时间:2023-11-29 08:01:23 25 4
gpt4 key购买 nike

我正在编写简单的客户端-服务器应用程序。一切正常,但是当我将 InputStream 和 OutputStream 更改为 ObjectOutputStream 和 ObjectInputStream 时,我的应用程序不发送消息。谁能帮助我并告诉我问题所在?

这是 Serwer.java:

class InWorke implements Runnable{

BufferedReader odczyt=null;
String slowo;
ObjectInputStream ois=null;
Message message;

InWorke(ObjectInputStream ois) {
this.ois=ois;
}

public void run() {
while(true) {
try {
slowo = (String) ois.readObject();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(slowo);
Thread.yield();
} }

}

class OutWorke implements Runnable{
Socket socket=null;
BufferedReader odczytWe=null;
DataOutputStream zapis=null;
String slowo=null;
Message message; // it is the simple class to serialization
ObjectOutputStream oos;

OutWorke(Socket socket,ObjectOutputStream oos) {
this.socket=socket;
this.oos=oos;
}

public void run() {
while(true) {
odczytWe=new BufferedReader(new InputStreamReader(System.in));
try {
slowo=odczytWe.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
oos.writeObject(slowo);
oos.flush();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Thread.yield();

}

}
}

public class Klient {
public static void main(String[] args) {

Socket socket=null;
ExecutorService exec=Executors.newCachedThreadPool();

ObjectOutputStream oos=null;
ObjectInputStream ois=null;

try {
socket=new Socket("localhost", 8881);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
//we=socket.getInputStream();
ois=new ObjectInputStream(socket.getInputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
//wy=socket.getOutputStream();
oos=new ObjectOutputStream(socket.getOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

exec.execute(new OutWorke(socket, oos));
exec.execute(new InWorke(ois));

}
}

客户端.java:

class InWorker implements Runnable{

Socket socket=null;
//InputStream we=null;
//OutputStream wy=null;
String slowo=null;
BufferedReader odczyt=null;
ObjectOutputStream oos;
ObjectInputStream ois;
Message message=null;

InWorker(Socket socket,ObjectInputStream ois) {
this.socket=socket;
this.ois=ois;
}

public void run() {
while(true) {
try {
slowo=(String) ois.readObject();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(slowo);
Thread.yield();
}

}
}

class OutWorker implements Runnable{

DataOutputStream zapis=null;
BufferedReader odczyt=null;
//OutputStream wy=null;
String tekst=null;
ObjectOutputStream oos=null;
Message message;

OutWorker(ObjectOutputStream oos) {
this.oos=oos;
}

public void run() {
while(true) {
odczyt=new BufferedReader(new InputStreamReader(System.in));
try {
tekst=odczyt.readLine();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
oos.writeObject(tekst);
oos.flush();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Thread.yield();
}
}

}

public class Serwer {

public static void main(String[] args) {
ServerSocket serversocket=null;
Socket socket=null;
//InputStream we=null;
//OutputStream wy=null;
ObjectOutputStream oos=null;
ObjectInputStream ois=null;

ExecutorService exec=Executors.newCachedThreadPool();

try {
serversocket=new ServerSocket(8881);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
socket=serversocket.accept();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
ois=new ObjectInputStream(socket.getInputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
oos=new ObjectOutputStream(socket.getOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
exec.execute(new InWorker(socket, ois));
exec.execute(new OutWorker(oos));

}

}

最佳答案

检查 ObjectInputStream 的构造函数:constructor .
它说:

This constructor will block until the corresponding ObjectOutputStream has written and flushed the header.

因此您需要创建并刷新相应的 ObjectOutputStream。现在您正在为服务器和客户端的输出流之前创建 ObjectInputStreams。它们阻止程序,因为没有创建输出流。您应该首先创建输出流,对它们调用 flush,然后才创建输入流。

关于java - 套接字 ObjectOutpuStream ObjectInputStream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14217245/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com