Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 通过套接字发送多字节数组_Java - Fatal编程技术网

Java 通过套接字发送多字节数组

Java 通过套接字发送多字节数组,java,Java,我想从客户端和服务器发送多字节数组 我能够从客户端发送/接收一个字节数组,从服务器发送/接收一个字节数组: 我的代码如下: 服务器: Socket sock=null; ByteArrayOutputStream input=null; OutputStream out=null; InputStream in=null; try{ ServerSocket server_sock=new ServerSocket(2972); sock=serve

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

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

我的代码如下:

服务器:

 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());
  }

这段代码对于一个从客户端和服务器提交的用户来说运行良好,但是当我想发送/接收更多字节数组时,它就不工作了

它只有在我使用shutdown时才起作用,因为客户端和服务器都在读写数据


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

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

你真的应该看看本教程:


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

您所面临的问题是,当前无法确定一个字节数组何时结束,下一个字节数组何时开始。(在“一个数组”解决方案中,字节数组的结尾对应于流的结尾。当然,一旦流结束/关闭,在不创建新的
套接字的情况下无法重新打开它。)

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

  • 要发送字节数组,请执行以下操作:

  • 将数据转换为字节

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

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

  • 要接收字节数组,请执行以下操作:

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

  • 分配所需大小的字节数组

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

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

注意-这是伪代码。我假设您能够将其转换为可工作的Java


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

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

这不是通过套接字写入或读取,它正在通过网络传输数据network@Javalover那么你做得不对,我们很难帮助你,除非你更新帖子以反映你所做的。@Javalover。。。呃,你能在你完成后在那里分享代码吗?@gumuruh-为什么?任何有能力的Java程序员都应该能够在没有帮助的情况下编写代码。。。我现在明白了D