自己写协议,把数据转换成字符串,用byte方式传送,
具体的实现,请参考《Unix高级网络编程》,绝对经典

解决方案 »

  1.   

    Unix高级网络编程 有得下吗?
      

  2.   

    最好用序列化的类。
    public static Object breaddato(byte[] data) 
       {
          Object bx = new Object();
          try 
          {
             ObjectInputStream fin;
             ByteArrayInputStream b = new ByteArrayInputStream(data);
             fin = new ObjectInputStream(b);
             bx = fin.readObject();
             fin.close();
             fin = null;
          }
          catch(Exception e) 
          {
             log(" bAppending/writing object data error : " + e.toString());
          }
          return bx;
       }
    public static byte[] bwritedato(Object data) 
       {
          byte[] bx = new byte[1];
          try 
          {
             ObjectOutputStream fin;
             ByteArrayOutputStream b = new ByteArrayOutputStream();
             fin = new ObjectOutputStream(b);
             fin.writeObject(data);
             fin.flush();
             fin.close();
             fin = null;
             bx = new byte[b.size()];
             bx = b.toByteArray();
          }
          catch(Exception e) 
          {
             log(" bAppending/writing object data error : " + e.toString());
          }
          return bx;
       }
      

  3.   

    序列化的问题几乎每本 java 书上都会讲, 自己看书学习吧.自己定协议还要说吗? 
    a: 我要开始传了.
    b: 好吧, 传吧, 我等着.
    a: 这是一个整数, 值是 8
    a: 这是一个字符串, 长度是 10.
    b: ok, 都收到了.