简体   繁体   English

Java 客户端到 C# 服务器 TCP

[英]Java client to C# server TCP

The problem is when i send up to 40 KB everything is okay when i send more sometime half of the data received some time nothing ,is there a limit of the networkstream.Read ,even though i cunked the data ,i can't determine if the problem form the java or the c# from the network stream or the Output stream问题是当我发送最多 40 KB 时一切正常,当我发送更多数据时,有一半的数据没有收到,网络流是否有限制。读取,即使我压缩了数据,我也无法确定来自网络流或输出流的 java 或 c# 的问题

C# SERVER C# 服务器

private void ReadData(){

                                    if (networkStream.DataAvailable)
                                    {

                                        int size = GetBufferSize();
                                        Thread.Sleep(340);
                                        byte[] myReadBuffer = new byte[size];

                                        int numberOfBytesRead = 0;


                                        while (true)
                                        {
                                            numberOfBytesRead = networkStream.Read(myReadBuffer, 0, myReadBuffer.Length);

                                            if (numberOfBytesRead >= myReadBuffer.Length)
                                            {
                                                break;
                                            }
                                        }

                                        string str = Encoding.UTF8.GetString(myReadBuffer, 0, size);


                                        dynamic Message = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(str);
                                        // Android Message , JSON String
                                        if (OnAndroidMessage != null)
                                        {
                                            OnAndroidMessage(Message);
                                        }

                                    }
}
  private int GetBufferSize()
    {
        byte[] myReadBuffer = new byte[4];
        int numberOfBytesRead = 0;
        do
        {
            numberOfBytesRead = networkStream.Read(myReadBuffer, 0, myReadBuffer.Length);
        } while (networkStream.DataAvailable && numberOfBytesRead < myReadBuffer.Length);
        if (numberOfBytesRead > 0)
        {
            // reverse the byte array.
            if (BitConverter.IsLittleEndian)
            {
                Array.Reverse(myReadBuffer);
            }

            return BitConverter.ToInt32(myReadBuffer, 0);
        }
        else
        {
            return 0;
        }
    }

Java Client // i tested this also without cutting the data to smaller paces ,half of the data received not all of them Java 客户端// 我也测试了这个,没有将数据减少到更小的速度,一半的数据不是全部都收到

        mBufferOut = socket.getOutputStream();

private void sendMessage(final String message) {

    if (mBufferOut != null && message != null) {
        try {
            byte[] data = message.getBytes("UTF-8");
            Log.d("_TAG", "Sending: " + message);
            Log.d("_TAG", "Message length: " + Integer.toString(data.length));

            mBufferOut.write(toByteArray(data.length));
            mBufferOut.flush();
            List<byte[]> divideArray = divideArray(data, 10000);
            for (byte[] dataChunk : divideArray) {
                Log.e("_TAG","CHUNK SIZE > " + Integer.toString(dataChunk.length));
                mBufferOut.write(dataChunk, 0, dataChunk.length);
                mBufferOut.flush();
            }

        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }
}
  private  List<byte[]> divideArray(byte[] source, int chunksize) {

        List<byte[]> result = new ArrayList<byte[]>();
        int start = 0;
        while (start < source.length) {
            int end = Math.min(source.length, start + chunksize);
            result.add(Arrays.copyOfRange(source, start, end));
            start += chunksize;
        }

        return result;
    }

Any ideas ?有任何想法吗 ?

Solution from this post NetworkStream is reading data that should not be there这篇文章中的解决方案NetworkStream 正在读取不应该存在的数据

static void ReadExact(Stream stream, byte[] buffer, int offset, int count)
{
    int read;
    while(count > 0 && (read = stream.Read(buffer, offset, count)) > 0) {
        offset += read;
        count -= read;
    }
    if(count != 0) throw new EndOfStreamException();
}

the problem is the Read it takes size and want to get that size you need to give it chunks and check each chunk And also read does not restart from where it stopped until it reads the amount is set to read meaning if i set to read 10 then if it not find the 10 then it will read what it find as example it reads 6 ,it will return 6 and when to loop another time ti read the rest it dose not start from 6 it start from 0 and read until 4 so you overwrite your data ,and if it read 10 from the first try then it set the read to finish so it dose not start from 0 ,it needs to read the amount the has been set to it to re set the read to new buffer location.问题是读取它需要大小并且想要获得那个大小你需要给它块并检查每个块并且读取也不会从它停止的地方重新开始直到它读取数量设置为读取意义如果我设置为读取10然后,如果它没有找到 10,那么它将读取它找到的内容,例如它读取 6 ,它将返回 6 并且何时循环另一个时间 ti 读取其余的它不会从 6 开始它从 0 开始并读取到 4 所以你覆盖您的数据,如果它从第一次尝试中读取 10,那么它将读取设置为完成,因此它不会从 0 开始,它需要读取已设置的数量以将读取重新设置为新的缓冲区位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM