【问题标题】:sending multiple byte array over the socket通过套接字发送多字节数组
【发布时间】:2011-04-29 02:22:32
【问题描述】:

我想从客户端和服务器发送多字节数组?

我能够从客户端发送/接收一个字节数组并从服务器发送/接收一个字节数组:

我的代码是这样的:

服务器:

 Socket sock=null;
  ByteArrayOutputStream input=null;
  OutputStream out=null;
    InputStream in=null;
  try{
      ServerSocket server_sock=new ServerSocket(2972);


    sock=server_sock.accept(); 

   in = 
       sock.getInputStream();
    out=sock.getOutputStream();
    }catch(IOException e){
      System.out.println(e.getMessage());
  }
 String word="";

  //1-Receive

  try{

    ByteArrayOutputStream serverinput=new ByteArrayOutputStream();
 int len=0;
byte[] buf=new byte[1000];
        while ((len = in.read(buf))>=0) {
              serverinput.write(buf, 0, len);

        }

      sock.shutdownInput();

        word=new String(serverinput.toByteArray());
    System.out.println("Client send 1"+word);

    }catch(Exception e){
      System.out.println(e.getMessage());
  }


   String st="Server is a king";
try{ 
   out.write(st.getBytes());

   out.flush();

}catch(Exception e){
      System.out.println(e.getMessage());
  }

客户:

 Socket sock=null;
    OutputStream out=null;
    InputStream in=null;


  try{
      sock=new Socket("127.0.0.1",2972);
      }catch(IOException e){
      System.out.println(e.getMessage());
  }


String word="Hellow World"  ;
    try{
    in = 
       sock.getInputStream();
    out=sock.getOutputStream();
    }catch(IOException e){
      System.out.println(e.getMessage());
  }

//1- send
   try{

       System.out.println("Your string is"+word+"converted to byte"+word.getBytes());

    out.write(word.getBytes());
    out.flush();
 sock.shutdownOutput();
    }catch(Exception e){
      System.out.println(e.getMessage());
  }

    try{  ByteArrayOutputStream serverinput=new ByteArrayOutputStream();
 int len=0;
byte[] buf=new byte[1000];
        while ((len = in.read(buf))>=0) {
              serverinput.write(buf, 0, len);


        }
    System.out.println("server send 1 "+new String(serverinput.toByteArray()));
     System.out.println("Your string is"+word+"converted to byte"+word.getBytes());

    }catch(Exception e){
      System.out.println(e.getMessage());
  }

此代码适用于从客户端和服务器提交,但当我想发送/接收更多字节数组时它不起作用?

只有当我使用关机时它才有效,因为客户端和服务器都在读取和写入数据。

因此,我不能再次使用套接字通道...有替代解决方案吗? ...这不会导致僵局。

【问题讨论】:

    标签: java


    【解决方案1】:

    您遇到的问题是,您目前无法确定一个字节数组何时结束而下一个字节数组何时开始。 (在您的“一个数组”解决方案中,字节数组的结尾对应于流的结尾。当然,一旦流结束/关闭,如果不创建新的Socket 等,就无法重新打开它。)

    解决这个问题的简单方法如下,使用围绕各自套接字流的 DataOutputStream 和 DataInputStream 对:

    • 发送字节数组:

      1. 将数据转换为字节。

      2. 使用DataOutputStream.writeInt(int)方法发送字节数组大小。

      3. 使用DataOutputStream.write(byte[])方法发送字节数组。

    • 接收字节数组:

      1. 使用DataInputStream.readInt()方法接收字节数组大小。

      2. 分配所需大小的字节数组。

      3. 使用DataInputStream.read(byte[], int, int) 方法将字节接收到字节数组中……重复,直到获得所有字节。

    通过在前面发送字节数组的大小,您可以告诉接收者要读取多少字节。您可以根据需要多次重复此过程。发送方可以通过简单地关闭套接字流向接收方指示没有更多字节数组要发送。

    注意 - 这是伪代码。我假设您有能力将其转化为可用的 Java。

    不要忘记将 BufferedInputStreams 和 BufferedOutputStreams 插入到各自的流链中……以减少系统调用开销。

    【讨论】:

    • @Javalover... 嗯,你会介意在你完成代码之后分享那里的代码吗?
    • @gumuruh - 为什么?任何称职的 Java 程序员都应该能够在零帮助下编写代码。
    • 别生气@StephenC...我现在明白了。 :D
    【解决方案2】:

    尝试将您的套接字流包装在 DataInputStream 和 DataOutputStream 中。这应该可以让你做你想做的事。

    【讨论】:

    • @Javalover 那你做得不对,除非你更新帖子以反映你所做的事情,否则我们很难帮助你。
    【解决方案3】:

    你真的应该看看这个教程:Reading from and Writing to a Socket

    它似乎概述了如何读取和写入套接字。读取应该像创建服务器套接字并侦听您期望的端口然后等待数据一样简单。

    【讨论】:

    • 这不是通过socket读写,而是通过网络转换数据
    【解决方案4】:

    您还可以创建一个对象来保存您的字节数组并使用ObjectOutputStream 将其发送出去,然后使用writeObject(Object) 方法发送信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-10
      • 2023-03-05
      • 1970-01-01
      • 2011-03-30
      • 1970-01-01
      • 2012-08-29
      • 1970-01-01
      相关资源
      最近更新 更多