套接字程序发送和接收用户定义的对象不起作用 [英] Socket program to send and receive user defined objects not working

查看:94
本文介绍了套接字程序发送和接收用户定义的对象不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户定义的类Message,我想在客户端和服务器之间传递它的对象。

I have a user defined class Message, whose object I would like to pass between the client and the server.

Message类如下:

The Message class is as follows:

import java.io.Serializable;

public class Message implements Serializable
{
    String CorS;
    int data_id;
    int status_id;
    Integer value;
    boolean withdraw;

    public Message()
    {
        CorS = null;
        data_id = 0;
        status_id = 0;
        value = 0;
        withdraw = false;
    }

    public Message(String CorS, int data_id, int status_id, Integer value)
    {
        this.CorS = CorS;
        this.data_id = data_id;
        this.status_id = status_id;
        this.value = value;
    }

    public Message(boolean withdraw)
    {
        this.withdraw = withdraw;
    }
}

客户端发送对象的代码服务器如下:

The code in the client side which sends the object to the server is as follows:

Socket s = null;
    ObjectInputStream in = null;
    ObjectOutputStream out = null;

    String hostname = null;
    int port_no = 0;

    HashMap<String, Integer> map = null;

    Message m = null;

    map = servers.get("Server" + server);

    for(String key : map.keySet())
    {
        hostname = key;
        port_no = map.get(key);
    }
    //System.out.println(hostname + " " + port_no);

    s = new Socket(hostname, port_no);
    in = new ObjectInputStream(new BufferedInputStream(s.getInputStream()));
    out = new ObjectOutputStream(new BufferedOutputStream(s.getOutputStream()));

    s_now = s;

    m = new Message(client, data, 0, 0);
    out.writeObject(m);
    out.flush();
    System.out.println("Sent obj");

同样,服务器端的代码如下:

Similarly, the code on the Server side is as follows:

while (true)
    {
        try 
        {
            System.out.println("Waiting for client on port " + serverSocket.getLocalPort() + "...");
            Socket server = serverSocket.accept();
            System.out.println("Just connected to " + server.getRemoteSocketAddress());

            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(server.getInputStream()));
            //ObjectOutputStream out = new ObjectOutputStream(server.getOutputStream());

            Message m = (Message) in.readObject();

            System.out.println(m.value);
        } 

        catch (IOException e) 
        {
            e.printStackTrace();
            break;
        }
        catch (ClassNotFoundException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

问题是对象没有被打印。我得到的输出如下:

The problem is that the object is not getting printed. The output I get is as follows:

Waiting for client on port 1051...
Just connected to /127.0.0.1:59216

我们将非常感谢这方面的任何帮助。谢谢:)

Any help in this regard will be greatly appreciated. Thanks :)

推荐答案

你需要在<之前创建 ObjectOutputStream 两端都是code> ObjectInputStream 。

You need to create the ObjectOutputStream before the ObjectInputStream at both ends.

原因是,如Javadoc所述,各自的构造函数会写入和读取流标题。因此,在对等体的输出流构造函数执行之前,输入流构造函数不能返回。因此,如果您首先构造两个输入流,则存在死锁。

The reason is that, as described in the Javadoc, the respective constructors write and read a stream header. So the input stream constructor can't return until the output stream constructor at the peer has executed. So if you construct both input streams first there is a deadlock.

这篇关于套接字程序发送和接收用户定义的对象不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆