C#C
C#13mo ago
BlackAnt02

Sending a file through Sockets C# TCP missing chunks

I send a 4 MB File through C# Socket using Socket.SendFile() and it sends all the data because wireshark shows all the data
but when running Socket.Receive() it just gets a small chunk of it and sets the rest to 0's.
Server Code
using System.Net.Sockets;
using System.Net;
using static System.Net.Mime.MediaTypeNames;

namespace Image_Testing
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socket.Bind(new IPEndPoint(IPAddress.Any, 42069));
            socket.Listen();
            while (true)
            {
                Socket client = socket.Accept();
                Thread thread = new Thread(() => OnConnection(client));
                thread.Start();
            }
        }
        public static void OnConnection(Socket client)
        {
            Console.WriteLine("Client Connected.");
            try
            {
                client.SendBufferSize = 8192;
                byte[] header = new byte[4];
                byte[] image = File.ReadAllBytes("test.jpg");
                byte[] data = new byte[image.Length + 4];
                header = BitConverter.GetBytes(image.Length);
                Array.Copy(header, 0, data, 0, 4);
                Array.Copy(image, 0, data, 4, image.Length);
                client.Send(data);
            }
            catch
            {
                Console.WriteLine("Client Disconnected.");
                client.Close();
            }
        }
    }
}
Was this page helpful?