BeginSendFile throws SocketException 'The parameter is incorrect'

  • Thread starter Thread starter Majx-
  • Start date Start date
M

Majx-

Guest
I'm using BeginSendFile(filePath, socket) to send files to a remote host, it worked before but now visiting the project again whenever I try to send a large file (tested with > 3 GB files) I get this SocketException :

System.Net.Sockets.SocketException: 'The parameter is incorrect'

However, small files doesn't seem to trigger the exception (tested with < 1.3 GB).

Extra info : Windows Defender and Firewall are disabled and no AVs on the machine.
Tested on win 10 2004 and 1809 builds

Here is the code I'm using :

Sender :


public static void SendFile(string dstIp, string filePath)
{
// Establish the local endpoint for the socket.
IPAddress ipAddr = IPAddress.Parse(dstIp);
IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, 39993);

// Create a TCP socket.
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

// Connect the socket to the remote endpoint.
client.Connect(ipEndPoint);

// Send the file
client.BeginSendFile(@"C:\Users\username\Desktop\file.ext", SentFileCallback, client); // <- client is marked as invalid parameter in VS
}

private static void SentFileCallback(IAsyncResult ar)
{
var client = ar.AsyncState as Socket;

// Complete sending the data to the remote device.
client.EndSendFile(ar);

// Release the socket.
client.Shutdown(SocketShutdown.Both);
client.Close();
}



Receiver :


public static async Task StartFileListener()
{
try
{
//Start listening for incoming data
var listener = new TcpListener(IPAddress.Any, 39993);
listener.Start();

while (true)
{
using (var client = await listener.AcceptTcpClientAsync())
{
using (var stream = client.GetStream())
{
Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\test2");

using (var output = File.Create(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\test\\" + "test2.dat"))
{
//Read the file in chunks of 10MB
var buffer = new byte[1024 * 1024 * 10];
int bytesRead;
while ((bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length)) > 0)
{
await output.WriteAsync(buffer, 0, bytesRead);
}
}
}
}
}
}
catch (Exception e)
{
throw e;
}
}

Continue reading...
 

Similar threads

P
Replies
0
Views
176
Policy standard local admin account with Active Di
P
E
Replies
0
Views
124
elfenliedtopfan55
E
Back
Top