简体   繁体   English

客户端未通过Java中的ObjectStream接收服务器发送的对象

[英]Client is not receiving Object sent by Server over ObjectStream in Java

I tried to solve the problem for quite a long time and finally decided to take help from experts. 我试图解决该问题很长时间,最终决定寻求专家的帮助。 I have developed Server-client application using Socket Programming and my client is able to connect to the Server successfully. 我已经使用套接字编程开发了Server-client应用程序,并且我的客户端能够成功连接到Server。 Now I am trying to send the object over the socket from server based on the request from client, my server is able to catch the request however, response(writeObject(...)) sent by server is not able to reach to the client. 现在我正在尝试根据客户端的请求通过服务器从套接字发送对象,我的服务器能够捕获该请求,但是服务器发送的response(writeObject(...))无法到达客户端。

Server code snippet : 服务器代码段:

ServerSocket sSocket = new ServerSocket(socketNumber);
ArrayList<LoginPassword> logIn = new ArrayList<>();
Socket cSocket = sSocket.accept();
ObjectOutputStream outputStream = new ObjectOutputStream(cSocket.getOutputStream());
outputStream.flush();
ObjectInputStream inputStream = new ObjectInputStream(cSocket.getInputStream());
LoginPassword lp1 = new LoginPassword("admin","admin");
logIn.add(lp1);
outputStream.writeObject(logIn);
outputStream.flush();

Clinet code Snippet : Clinet代码段:

Socket client = new Socket(InetAddress.getLocalHost(),socketNumber);
ArrayList<LoginPassword> myList = new ArrayList<>();
ObjectOutputStream outputStream = new ObjectOutputStream(client.getOutputStream());
outputStream.flush();
ObjectInputStream inputStream = new ObjectInputStream(client.getInputStream());
//Wait untill the data is available
myList = (ArrayList<LoginPassword>) inputStream.readObject();

In my case, clients readObject() is never called since it is not able to detect the data sent by Server.Any help please. 在我的情况下,永远不会调用客户端readObject(),因为它无法检测到Server发送的数据。请提供任何帮助。

You must create the ObjectOutputStream first, preferably at both ends. 您必须首先创建ObjectOutputStream ,最好在两端创建。 Otherise you will get a deadlock creating the ObjectInputStream . 否则,您将在创建ObjectInputStream遇到死锁。

Alternatively I'd be curious to know what the meaning of the following comment is: 另外,我很想知道以下评论的含义是什么:

// Wait untill the data is available

If this indicates a piece of missing code that calls available() , just delete it. 如果这表明缺少一部分代码调用了available() ,则将其删除。 readObject() will block for exactly as long as necessary. readObject()将根据需要完全阻塞。 There are few correct uses of available() , and this isn't one of them. available()正确用法很少,但这不是其中之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM