[英]How to send Objects outside the run method?
I want to be able to use my sendMessage()
method to send something to my client while also waiting for client messages at the same time (the client should be able to do the same). 我希望能够使用sendMessage()
方法向客户端发送内容,同时也要等待客户端消息(客户端应该能够执行相同操作)。 I tried to define the Socket and Streamobjects everywhere but I think I don't really understand how they work. 我试图在各处定义Socket和Streamobject,但我认为我不太了解它们是如何工作的。
public class Server extends Thread{
private ObjectOutputStream oos;
public void sendMessage(String message){
try
{
oos.writeObject(message);
} catch (IOException e)
{
e.printStackTrace();
}
}
public void run() {
try {
ServerSocket server = new ServerSocket(4444);
Socket socket = server.accept();
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
this.oos = oos;
while(true) {
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
String message = (String) ois.readObject();
System.out.println("Server Received: " + message);
ois.close();
socket.close();
}
} catch (Exception e) {
}
}
public class Main {
public static void main(String[] args)
{
Server s = new Server();
s.start();
new Client().start();
s.sendMessage("test");
}
EDIT 编辑
Ok i tried to create a thread for each client but I still have the problem that I can't access the objectoutputstream... 好的,我尝试为每个客户端创建一个线程,但是仍然存在无法访问objectoutputstream的问题。
public class SuperServer {
private Server s = new Server();
public void startServer(){
s.setDaemon(true);
s.start();
if(s.isAlive()){
System.out.println("server started");
}
}
public void sendMessage(String message) throws IOException
{
s.handler.sendMessage(message);
}
}
public class Server extends Thread {
ServerSocket server;
Socket socket;
ServerHandler handler;
public void run()
{
try {
server = new ServerSocket(4444);
while (true)
{
socket = server.accept();
ServerHandler serverHandler = new ServerHandler(socket);
handler = serverHandler;
}
} catch (IOException e)
{
e.printStackTrace();
}
}
}
public class ServerHandler extends Thread {
protected Socket socket;
private ObjectOutputStream oos;
private ObjectInputStream ois;
public ServerHandler(Socket socket){
this.socket = socket;
}
public void sendMessage(String message) throws IOException
{
oos.writeObject(message);
}
public void run()
{
try(
ServerSocket server = new ServerSocket(4444);
Socket socket = server.accept();
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
)
{
this.socket = socket;
this.oos = oos;
while (true)
{
String message = (String) ois.readObject();
System.out.println("Server Received: " + message);
}
} catch (IOException e)
{
e.printStackTrace();
} catch (ClassNotFoundException e)
{
e.printStackTrace();
}
}
}
public class Main {
public static void main(String[] args) throws IOException
{
SuperServer s = new SuperServer();
s.startServer();
SuperClient c = new SuperClient();
c.clientStart();
Scanner sc = new Scanner(System.in);
System.out.println("input 1:");
int a = Integer.parseInt(sc.nextLine());
if(a == 1) s.sendMessage("test");
}
}
You use incorrectly Thread and Socket. 您使用不正确的线程和套接字。
1) In the server side, generally you want to create a new Thread for each new connection with a client to parallelize the communication between the server and its clients. 1)在服务器端,通常您希望为与客户端的每个新连接创建一个新线程,以并行化服务器与其客户端之间的通信。
Here you create a Thread for the whole server processing... 在这里,您将为整个服务器处理创建一个线程...
2)That creates another issue because Threads.start()
doesn't make run necessarily right now the thread. 2)这带来了另一个问题,因为Threads.start()
不一定使线程现在就必须运行。 So here : 所以在这里 :
Server s = new Server();
s.start();
new Client().start();
s.sendMessage("test");
The s.sendMessage("test");
s.sendMessage("test");
may be invoked before Server
and Client
Threads are executed and terminated. 可以在执行和终止Server
线程和Client
线程之前调用它。 So the ObjectOutputStream
field could be still null
. 因此, ObjectOutputStream
字段可能仍为null
。
3) You close the ServerSocket once a first message is read. 3)一旦读取第一条消息,就关闭ServerSocket。
So even if s.sendMessage("test");
因此,即使s.sendMessage("test");
was invoked at the right time (by luck), ois
will be not null
but closed this time. 如果在正确的时间(通过运气)被调用,则ois
不会为null
而是这次关闭。
So in summarize : 所以总结:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.