|
-
Aug 29th, 2006, 04:50 PM
#1
Thread Starter
Junior Member
[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);
}
}
}
}
Last edited by Outshynd; Aug 29th, 2006 at 04:52 PM.
Reason: fixed code alignment issue
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|