通过套接字发送对象 [英] Sending objects through sockets

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

问题描述

我过去完成的唯一套接字编程是简单的文本流.我想知道通过套接字发送类似Java对象之类的最有效方法是什么.

The only socket programming I have done in the past is simple text streams. I am wondering what is the most effective way to send something like a Java object through a socket.

例如,如果我具有以下Employee类(Dependent将是由受抚养人的信息组成的简单类):

For instance if I have the following Employee class (Dependent would be a simple class composed of a dependent's information):

public class Employee {
    private String name;
    private double salary;
    private ArrayList<Dependent> dependents;
}

我应该使Employee对象可序列化并通过套接字发送实例.还是应该写一个包含雇员信息的xml文件并将其发送?任何指导将不胜感激.还是有一些完全不同的更好的方法?谢谢!

Should I just make the Employee object Serializable and send instances through the socket. Or should I write up an xml file containing the Employees information and send that? Any guidance would be greatly appreciated. Or is there some completely different and better way? Thank you!

推荐答案

如果仅在Java JVM之间发送数据,则这两种选择都是可能的.

If you are only sending data betwen Java JVMs, then either choice is possible.

文本表示形式(XML,JSON或自定义)具有以下优点:

A textual representation (XML, JSON, or custom) has several advantages:

  • 使其更易于在Java和其他语言之间互操作
  • 面对版本更改或套接字两端的代码版本稍有不同,它变脆了
  • 测试和调试要容易得多

根据格式,速度可能会稍慢一些,但这通常并不重要.

Depending on the format, it may be a little slower, but this often not significant.

这篇关于通过套接字发送对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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