关于java套接字的愚蠢问题:当客户端将第一件事写入对象时,我可以在输入流上创建客户端侦听器吗?
InetAddress Address = InetAddress.getByName("172.24.3.154");
kkSocket = new Socket(Address, 2003);
out = new ObjectOutputStream(kkSocket.getOutputStream());
in = new ObjectInputStream(kkSocket.getInputStream());
public static <T> Object sendReceive(T obj) {
try {
out.writeObject(obj);
out.flush();
System.out.println("Client : " + obj.toString());
Object resp = in.readObject();
if (resp != null) {
System.out.println("Server : " + resp.toString());
}
return resp;
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
这是我向服务器发送请求的客户端方法。
out = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
System.out.println("Server: S-a conectat :" + socket.getInetAddress());
Protocol protocol = new Protocol(server);
while (socket.isClosed() != true) {
Object response = protocol.processInput(in.readObject());
System.out.println("Server: message Received: " + getName());
if (response != null) {
out.writeObject(response);
out.flush();
} else {
out.writeObject(null);
out.flush();
}
}
这就是我的服务器所做的。它也有效。我的问题是:为套接字进行此设置,我如何为客户端创建一个单独的侦听器,以便在某个时间向客户端发送一条消息,同时客户端仍然正常工作?
我尝试创建一个新线程来管理客户端的输入流,但应用程序无法启动,它只是卡在线程的 run 方法中
谢谢。
编辑:
我正在做的是一个带有套接字的多客户端应用程序,使用多线程。我有上面的代码并为我工作,用于调用“sendReceive”方法向服务器发出请求并返回一些内容。我是什么尝试做的是,当我收到特定请求时,我想通知所有在线客户端。我应用了这样的观察者模式:服务器是可观察的,线程是观察者。当收到特定请求时,我会通知所有步骤,但我无法让每个威胁立即向客户端发送消息,因为客户端不会监听。也许我会在错误的方式。有人可以帮忙吗?
InetAddress Address = InetAddress.getByName("172.24.3.154");
kkSocket = new Socket(Address, 2003);
out = new ObjectOutputStream(kkSocket.getOutputStream());
in = new ObjectInputStream(kkSocket.getInputStream());
public static <T> Object sendReceive(T obj) {
try {
out.writeObject(obj);
out.flush();
System.out.println("Client : " + obj.toString());
Object resp = in.readObject();
if (resp != null) {
System.out.println("Server : " + resp.toString());
}
return resp;
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
this is my Client method that i send a request to the Server.
out = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
System.out.println("Server: S-a conectat :" + socket.getInetAddress());
Protocol protocol = new Protocol(server);
while (socket.isClosed() != true) {
Object response = protocol.processInput(in.readObject());
System.out.println("Server: message Received: " + getName());
if (response != null) {
out.writeObject(response);
out.flush();
} else {
out.writeObject(null);
out.flush();
}
}
this is what my server does. And it works too.my question is : Giving this setup for the sockets how can i make a separate listener for the client in order to send at a certain time one message to the client , while the client still functions normally ?
I tryied to create a new thread fo manage the input stream on the client side , but the app won`t start and it just gets stuck in the run method of the thread
Thx.
EDIT :
What I am doing is a multi client App whith sockets,using mutli threading.I have the code above and works for me, for calling the "sendReceive" method for making a request to the server and it returns something.What i am trying to do is when i receive a specific request i want to notify all the online clients.I applied the observer pattern like this: The server is Observable and the threads are Observers. when a specific request comes in i notify all the treads , but i cant get each threat to send to the clients immediately a message because the client doesn
t listen.Maybe I'm going with this on the wrong way.Can someone Help pls?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您不知道服务器何时向客户端发送消息,则您需要使用两个套接字(每个套接字都在自己的端口上 - 异步通信),或者您需要一个更好定义的协议来知道何时读取和何时写入(一个套接字 - 同步通信)。
如果您无法预测客户端何时需要从套接字读取数据,那么您的应用程序也将无法弄清楚。 :)
在两个套接字方法中,每个套接字可以有一个线程,因此您不必担心 I/O 阻塞。
Either you need to use two sockets (each on their own port - asynchronous communication) if you have no idea when the server will send a message to the client, or you need a better defined protocol that knows when to read, and when to write (one socket - synchronous communication).
If you can't predict when the client will need to read from the socket, then your app won't be able to figure it out either. :)
In the two socket approach, you could have one thread per socket, so you don't have to worry about I/O blocking.