为什么接收时缓冲区比我发送的缓冲区小?

时间:2018-02-03 03:41:21

标签: c# .net sockets tcp tcpclient

我目前正在尝试从我的客户端向我的服务器发送缓冲区,我尝试发送的缓冲区是我PC上图像的字节数组。

它表示发送时大小约为200k字节,但我只收到61k,与我发送的图片无关,接收缓冲区总是一样的,为什么会这样?如何从客户端到服务器获取正确的缓冲区?

我需要整个东西,所以它可以在收到字节数组后下载图像。

客户端

using System;
using System.Drawing;
using System.IO;
using System.Net.Sockets;
using System.Text;

namespace TransferImageClient
{
    class Program
    {
        // Declare a port that we will be using for the TcpClient to connect to
        const int port = 1273;

        // Declare the server IP that TcpClient will be connecting to.
        const string server_ip = "127.0.0.1";

        static void Main(string[] args)
        {
            Console.Title = "Client";
            Console.ForegroundColor = ConsoleColor.Green;

            string texttoSend = DateTime.Now.ToString();

            TcpClient client = new TcpClient(server_ip, port);

            NetworkStream nwStream = client.GetStream();

            Console.WriteLine("Please enter the path to the image: ");

            byte[] bytesToSend = ConvertImage(Console.ReadLine());

            Console.WriteLine("Sending image..");

            nwStream.Write(bytesToSend, 0, bytesToSend.Length);

            byte[] bytesToRead = new byte[client.ReceiveBufferSize];

            int bytesRead = nwStream.Read(bytesToRead, 0, client.ReceiveBufferSize);
            Console.WriteLine("Recieved: " + Encoding.ASCII.GetString(bytesToRead, 0, bytesRead));
            Console.ReadLine();

            //Close the client so we're not leaving it open for people to eavesdrop.
            client.Close();
        }

        private static byte[] ConvertImage(string Image)
        {
            Bitmap tImage = new Bitmap(Image);
            byte[] bStream = ImageToByte(tImage);
            return bStream;
        }

        static byte[] ImageToByte(Bitmap iImage)
        {
            MemoryStream mMemoryStream = new MemoryStream();
            iImage.Save(mMemoryStream, System.Drawing.Imaging.ImageFormat.Png);
            return mMemoryStream.ToArray();
        }
    }
}

服务器

using System;
using System.Net;
using System.Net.Sockets;

namespace TransferImageServer
{
    class Program
    {
        const int port = 1273;
        const string serverIP = "127.0.0.1";

        static void Main(string[] args)
        {
            Console.Title = "Server";
            Console.ForegroundColor = ConsoleColor.Green;

            IPAddress localAdress = IPAddress.Parse(serverIP);

            TcpListener listener = new TcpListener(localAdress, port);
            Console.WriteLine("Listening..");

            listener.Start();

            TcpClient client = listener.AcceptTcpClient();

            NetworkStream nwStream = client.GetStream();

            byte[] buffer = new byte[client.ReceiveBufferSize];

            int bytesRead = nwStream.Read(buffer, 0, client.ReceiveBufferSize);

            Console.WriteLine("Recieved the image ");

            Console.ReadLine();
            //Console.WriteLine("Sending back : ");
            //nwStream.Write(buffer, 0, bytesRead);
            client.Close();
            listener.Stop();
            Console.ReadLine();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

TCP没有消息概念。这意味着它并不关心你一次写入多少字节 - 这意味着Read不会返回你一次写入的相同字节数。

允许

Read返回任意数量的数据,最多可以返回已发送的数量。如果您一遍又一遍地呼叫Read,您将不断获得更多数据(只要有更多数据)。