[2.0] C# Socket Programming Problem/Error/Bug
Using the following code, a limit of 1460 bytes seems to be put on any incoming packet and I cannot figure out for the life of me why this is. Is there any reason behind it? Limitation of the method being used? Any way to fix it or other techniques to get past or around the issue?
Any explanation of what's happening, why, and what can be done would be greatly appreciated. Whereas I want to fix the problem, primarily, I'd also like to know what's going wrong. Thanks.
Code:
using System;
using System.Net;
using System.Net.Sockets;
namespace TestServer
{
class Class1
{
static public Socket soc1;
static public Socket soc2;
public const int port = 36363;
public const string msg = "Hello World!";
[STAThread]
static void Main(string[] args)
{
try
{
soc1 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ipLocal = new IPEndPoint(IPAddress.Any, port);
soc1.Bind(ipLocal);
soc1.Listen(1);
soc1.BeginAccept(new AsyncCallback(connectCallback), null);
Console.WriteLine("The server is now listening on port {0}.\nPress CTRL+C to quit.", port);
while (true)
{
System.Threading.Thread.Sleep(50);
}
}
catch (SocketException serverException)
{
Console.WriteLine(serverException.Message);
}
}
static public void connectCallback(IAsyncResult result)
{
try
{
soc2 = soc1.EndAccept(result);
WaitForData(soc2);
Send(soc2);
}
catch (ObjectDisposedException)
{
Console.WriteLine("The connection has been closed.");
}
catch (SocketException connectionException)
{
Console.WriteLine(connectionException.Message);
}
}
static void Send(Socket IOsocket)
{
try
{
Console.Write("Sending this message: \"{0}\"", msg);
byte[] byteData = System.Text.Encoding.ASCII.GetBytes(msg);
IOsocket.Send(byteData);
}
catch (SocketException sendException)
{
Console.WriteLine(sendException.Message);
}
}
public class SocketPacket
{
public const int bufSize = ushort.MaxValue;
public byte[] dataBuf = new byte[bufSize];
public Socket thisSoc;
}
static public void WaitForData(Socket IOsocket)
{
try
{
SocketPacket packet = new SocketPacket();
packet.thisSoc = IOsocket;
IOsocket.BeginReceive(packet.dataBuf, 0, packet.dataBuf.Length, SocketFlags.None, new AsyncCallback(dataReceivedCallback), packet);
}
catch (SocketException dataException)
{
Console.WriteLine(dataException.Message);
}
}
static public void dataReceivedCallback(IAsyncResult result)
{
try
{
SocketPacket packet = (SocketPacket)result.AsyncState;
int size = packet.thisSoc.EndReceive(result);
char[] buf = new char[size];
System.Text.Decoder decoder = System.Text.Encoding.UTF8.GetDecoder();
int numCharacters = decoder.GetChars(packet.dataBuf, 0, size, buf, 0);
System.String str = new System.String(buf);
Console.WriteLine("Recv: \"{0}\"\nSize: {1}", str, size);
WaitForData(soc2);
}
catch (ObjectDisposedException)
{
Console.WriteLine("The connection has been closed.");
}
catch (SocketException dataReceivedException)
{
Console.WriteLine(dataReceivedException.Message);
}
}
}
}
Re: [2.0] C# Socket Programming Problem/Error/Bug
Are you communicating over a broadband connection?
Re: [2.0] C# Socket Programming Problem/Error/Bug
Yes. Is it a limitation of the TCP protocal, or something?
Re: [2.0] C# Socket Programming Problem/Error/Bug
The limitation seems to be from the following code:
public const int bufSize = ushort.MaxValue;
public byte[] dataBuf = new byte[bufSize];
You're setting the incoming buffer to ushort.MaxValue, whatever that int value is.
I can't remember code right now, but last time I did that, when a message is received, you have to loop to get the full message if the incoming buffer is set to in your case 1460.
Jennifer.
Re: [2.0] C# Socket Programming Problem/Error/Bug
That's the point, though: ushort.MaxValue is 0xFFFF, or 65535. I'm relatively certain that I'm not limiting myself in the code, but more that it's a limitation with the protocol (or something).
Re: [2.0] C# Socket Programming Problem/Error/Bug
no its not. Try this:
public const int bufSize = 1024;
public byte[] dataBuf = new byte[bufSize];
And see if it will read in by 1024 bytes at a time.
Re: [2.0] C# Socket Programming Problem/Error/Bug
Console.WriteLine(ushort.MaxValue);
>65535
Putting 1024 limits it at 1024. Putting 1461 limits it at 1460. It has nothing to do with that particular line.
Re: [2.0] C# Socket Programming Problem/Error/Bug
Check out the "MTU" value for your broadband router.