BlackAnt02
BlackAnt02
CC#
Created by BlackAnt02 on 12/17/2024 in #help
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();
}
}
}
}
4 replies
CC#
Created by BlackAnt02 on 9/22/2024 in #help
MemoryMappedFile not working under global
I am trying to use memorymappedfile for communication between a service and a program and it keeps saying file not found when I try to use OpenExisting.
4 replies