Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.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 如何防止1399字节上的套接字通道拆分?_Java_C#_Sockets - Fatal编程技术网

Java 如何防止1399字节上的套接字通道拆分?

Java 如何防止1399字节上的套接字通道拆分?,java,c#,sockets,Java,C#,Sockets,我有一个C#客户机,可以从Java套接字服务器发送和接收数据。当从服务器到客户端的输出数据通过1399时,它将分两部分接收: 第一部分:0-1399字节 第二部分:剩余部分 有没有办法防止它分裂 服务器端代码: if(outData.size() == 0) return; try { if(!socketChannel.isOpen()) return; byte[] bytesArray = outData.toByteArray(); outData.reset(

我有一个C#客户机,可以从Java套接字服务器发送和接收数据。当从服务器到客户端的输出数据通过1399时,它将分两部分接收:

  • 第一部分:0-1399字节
  • 第二部分:剩余部分
  • 有没有办法防止它分裂

    服务器端代码:

    if(outData.size() == 0) return;
    try {
        if(!socketChannel.isOpen()) return;
    
        byte[] bytesArray = outData.toByteArray();
        outData.reset();
    
        ByteBuffer buffer = ByteBuffer.allocateDirect(bytesArray.length);
    
        buffer.put(bytesArray);
        buffer.flip();
        socketChannel.write(buffer);
    
    } catch (IOException e) {
        disconnect();
        e.printStackTrace();
    }
    
    客户端代码:

    clientSocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
    recieveBuffer = new byte[8142];
    
    try
    {
        clientSocket.Connect("111.111.111.111",9889));
    }
    catch(SocketException ex)
    {
        Debug.Log(ex.Message);
    }
    clientSocket.BeginReceive(recieveBuffer,0,recieveBuffer.Length,SocketFlags.None,new AsyncCallback(ReceiveCallback),null);
    

    好吧,没有办法防止这种情况发生,因为它需要两个(或更多)网络数据包,因此当收到第一个数据包时,读取可能会返回;这取决于你检查并粘贴在一起。这就是为什么有些协议要么依赖于特定的开始/结束边界字节,要么随数据一起发送大小(可能还有校验和)。