如何在java中通过TCP连接发送序列化数据

如何在java中通过TCP连接发送序列化数据,java,exception,serialization,tcp,datainputstream,Java,Exception,Serialization,Tcp,Datainputstream,我想通过TCP连接发送已序列化的数据。 我已经创建了一个客户机/服务器连接,并在序列化它之后发送一个对象。 然而,我不知道该如何读取数据 以下是代码片段: 发送功能: 函数调用: String hostname=somevalue; int-portNo=somevalue; Hashtable对象=somevalue; 发送到(主机名、端口号、…、对象); 接收功能: DataInputStream input=newdatainputstream(clientSocket.getInput

我想通过TCP连接发送已序列化的数据。 我已经创建了一个客户机/服务器连接,并在序列化它之后发送一个对象。 然而,我不知道该如何读取数据

以下是代码片段:

发送功能: 函数调用:
String hostname=somevalue;
int-portNo=somevalue;
Hashtable对象=somevalue;
发送到(主机名、端口号、…、对象);
接收功能:
DataInputStream input=newdatainputstream(clientSocket.getInputStream());
int length=input.readInt();
字节[]字节=新字节[长度];
输入。可读(字节);

哈希表recvObj=(哈希表使用
ObjectOutputStream
写入对象,并
ObjectInputStream
读取对象。

请告诉我们第一个片段中
output
变量的数据类型programming5.io.Serializer.serializeBytes和deserialize看起来像什么?是的,看起来您使用的是一个非标准库。Google仅显示。)提到这个序列化程序类并没有真正的帮助,当然不足以真正帮助您。@Alexander:DataOutputStream output=new DataOutputStream(sock.getOutputStream());
sendTo(String receiverAddr, int receiverPort,....., Object data) {
.
.
.
  if (data != null) { 
    byte[] byteObj = programming5.io.Serializer.serializeBytes(data);
    output.writeInt(byteObj.length);
    output.write(byteObj, 0, byteObj.length);
    output.flush();
  }
  output.close();
  sock.close();
}
String hostname = somevalue;
int portNo = somevalue;
Hashtable <Integer, Integer> object = somevalue;

sendTo(hostname,portNo,...,object);
DataInputStream input = new DataInputStream(clientSocket.getInputStream());

int length = input.readInt();
byte[] bytes = new byte[length];
input.readFully(bytes);

Hashtable<Integer, Integer> recvObj = (Hashtable<Integer,Integer)programming5.io.Serializer.deserialize(bytes);