简体   繁体   English

TCP套接字交换kryo序列化对象

[英]TCP Socket exchanging kryo serialized objects

I am currently working on a project for sending/receiving objects through TCP sockets. 我目前正在研究一个通过TCP套接字发送/接收对象的项目。 I chose to work with the Kryo Serialization framework, since it is one of the most popular frameworks out there. 我选择使用Kryo序列化框架,因为它是目前最流行的框架之一。 I saw that for Network communication, KryoNet is recommended, but for my own reasons, I chose to use my own TCP Socket framework (mainly because I want custom control of TCP streams and Threads on my project). 我看到对于网络通信,推荐使用KryoNet,但是出于我自己的原因,我选择使用自己的TCP Socket框架(主要是因为我想在项目中自定义控制TCP流和线程)。 The problem I encounter is that I have created the following class for Messages: 我遇到的问题是我为消息创建了以下类:

public class MyMessage {

    public HashMap<String, String> _values;

    public MyMessage() {
        _values = new HashMap<String, String>();
    }
}

And on the server side I have the following code for reading input from a TCP Socket: 在服务器端,我有以下代码用于从TCP套接字读取输入:

import java.io.*;
import java.net.*;

public class MyServer {

    private ServerSocket _serverSocket;

    private Integer _serverPort;

    private boolean _killCommand;

    public MyServer() {
        _serverSocket = null;
        _serverPort = -1;
        try {
            _serverSocket = new ServerSocket(0);
            _serverPort = _serverSocket.getLocalPort();
            System.out.println("server started on IP: " + _serverSocket.getInetAddress().getHostAddress() + ":" + _serverPort);
        } catch (IOException e) {
            e.printStackTrace();
        }
        _killCommand = false;
    }

    public void runServer() {
        Socket _client= null;
        while(_killCommand == false) {
            try {
                _client = _serverSocket.accept();
                _out = _client.getOutputStream();
                _in = _client.getInputStream();
                (new Thread(new ServerThread(_in, _out))).start();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

the ServerThread class: ServerThread类:

import java.io.*;
import java.util.HashMap;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.*;

public class ServerThread implements Runnable {

    private InputStream _in;

    private OutputStream _out;

    private Kryo kryo;

    private Input _input;

    private Output _output;

    public SynEFOthread(InputStream in, OutputStream out) {
        _in = in;
        _out = out;
        kryo = new Kryo();
        _output = new Output(_out);
        _input = new Input(_in);
    }

    @Override
    public void run() {
        MyMessage msg = null;
        System.out.println("Thread worker: about to parse input message");
        msg = kryo.readObject(_input, MyMessage.class);
        }
    }
}

On the other side, the client code is the following: 另一方面,客户端代码如下:

import java.io.*;
import java.net.Socket;
import java.util.*;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.*;

public class MyClient {

    public static void main(String[] args) throws Exception {
        String server_ip = "";
        Integer server_port = -1;

        server_ip = args[0];
        server_port = Integer.parseInt(args[1]);

        Socket serverSocket = new Socket(server_ip, server_port);
        OutputStream _out = serverSocket.getOutputStream();
        InputStream _in = serverSocket.getInputStream();
        Output _output = new Output(_out);
        Input _input = new Input(_in);
        MyMessage msg = new MyMessage();
        msg._values = new HashMap<String, String>();
        msg._values.put("TASK_TYPE", "TOPOLOGY");
        Kryo kryo = new Kryo();
        kryo.writeObject(_output, msg);
        Thread.sleep(100);
        kryo.writeObject(_output, _msg);
        String _ack = kryo.readObject(_input, String.class);
        serverSocket.close();
    }
}

The problem is that on ServerThread.run() function, the kryo.readObject() call blocks and nothing is done after that. 问题在于,在ServerThread.run()函数上,kryo.readObject()调用被阻止,此后什么也不做。 Am I doing something wrong? 难道我做错了什么? Am I opening the streams properly for use with the Kryo Serialization Framework? 我是否可以正确打开流以与Kryo序列化框架一起使用?

  1. Register your class with kryo.register(MyMessage.class) both on client and server sides. 在客户端和服务器端都使用kryo.register(MyMessage.class)注册您的类。
  2. Create a test for your serialization/deserialization code, but instead of sockets use ByteArrayOutputStream/ByteArrayInputStream. 为您的序列化/反序列化代码创建一个测试,但是请使用ByteArrayOutputStream / ByteArrayInputStream代替套接字。
  3. Flush! 同花顺! After you write complete message to stream, flush written bytes to network: _output.flush() 在将完整的消息写入流之后,将写入的字节刷新到网络:_output.flush()

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

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