C
C#5d 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();
}
}
}
}
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();
}
}
}
}
2 Replies
BlackAnt02
BlackAnt02OP5d ago
Client Code
using System.Net.Sockets;
using System.Net;
using System.Text;

namespace Client
{
internal class Program
{
public static byte[] data2 = null;
static void Main(string[] args)
{
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
client.Connect("IPHidden", 42069);
client.ReceiveBufferSize = 8192;
byte[] header = new byte[4];
client.Receive(header);
int size = (int)BitConverter.ToUInt32(header);
Console.WriteLine(BitConverter.ToString(header));
byte[] data = new byte[size];
client.Receive(data);
Console.WriteLine(BitConverter.ToString(data));
data2 = data;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}

Console.ReadKey();
Environment.Exit(0);
}
}
}
using System.Net.Sockets;
using System.Net;
using System.Text;

namespace Client
{
internal class Program
{
public static byte[] data2 = null;
static void Main(string[] args)
{
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
client.Connect("IPHidden", 42069);
client.ReceiveBufferSize = 8192;
byte[] header = new byte[4];
client.Receive(header);
int size = (int)BitConverter.ToUInt32(header);
Console.WriteLine(BitConverter.ToString(header));
byte[] data = new byte[size];
client.Receive(data);
Console.WriteLine(BitConverter.ToString(data));
data2 = data;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}

Console.ReadKey();
Environment.Exit(0);
}
}
}
sending through loopback receives full message since mtu is a lot larger
HtmlCompiler
HtmlCompiler4d ago
hint: look at Socket.Receive documentation you have to loop it, it's not guaranted to receive all the data at one time
Want results from more Discord servers?
Add your server