M
Matthew Groch
To All Networking/.NET/C# gurus out there:
Can anyone explain the behavior of what's happening under the covers
when the following client intereacts with the following server (code
below)? This code is basically a stripped down version of a system I'm
trying to put together that is having major stability issues.
Basically, the client connects to the server and starts pouring
information onto the socket connection in an asynchronous fashion. The
server takes it's time processing the information it receives off of
the socket connection. Eventually, the whole thing blows up... and
when it blows up, I mean until I kill the server process, I can't use
IE anymore, instant messager dies, PCAnywhere dies, etc..
Here's the exception that gets thrown server side:
Unhandled Exception: System.InvalidOperationException: AcceptCallback
---> Syste
m.Net.Sockets.SocketException: An operation on a socket could not be
performed b
ecause the system lacked sufficient buffer space or because a queue
was full
at System.Net.Sockets.Socket.BeginSend(Byte[] buffer, Int32 offset,
Int32 siz
e, SocketFlags socketFlags, AsyncCallback callback, Object state)
at Server.Server.HandleSocketAccept(IAsyncResult result) in
v:\socketbuffer\s
erver\class1.cs:line 65
at System.Net.Sockets.AcceptAsyncResult.AcceptCallback(Object
stateObject, Bo
olean Signaled)
--- End of inner exception stack trace ---
at System.Net.Sockets.AcceptAsyncResult.AcceptCallback(Object
stateObject, Bo
olean Signaled)
And, here's the code:
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace Server
{
public class Server
{
private AsyncCallback _acceptCallback;
private AsyncCallback _acceptSend;
private Socket _socket;
private byte[] _message;
public Server(int port)
{
string str = "aldgh;elskghlskhglsghlshg;slghl;sgha;lgh;lahg;lshg;lshg;lshg"
+
"aldgh;elskghlskhglsghlshg;slghl;sgha;lgh;lahg;lshg;lshg;lshg" +
"aldgh;elskghlskhglsghlshg;slghl;sgha;lgh;lahg;lshg;lshg;lshg" +
"aldgh;elskghlskhglsghlshg;slghl;sgha;lgh;lahg;lshg;lshg;lshg" +
"aldgh;elskghlskhglsghlshg;slghl;sgha;lgh;lahg;lshg;lshg;lshg" +
"aldgh;elskghlskhglsghlshg;slghl;sgha;lgh;lahg;lshg;lshg;lshg" +
"aldgh;elskghlskhglsghlshg;slghl;sgha;lgh;lahg;lshg;lshg;lshg" +
"aldgh;elskghlskhglsghlshg;slghl;sgha;lgh;lahg;lshg;lshg;lshg" +
"aldgh;elskghlskhglsghlshg;slghl;sgha;lgh;lahg;lshg;lshg;lshg" +
"aldgh;elskghlskhglsghlshg;slghl;sgha;lgh;lahg;lshg;lshg;lshg" +
"aldgh;elskghlskhglsghlshg;slghl;sgha;lgh;lahg;lshg;lshg;lshg" +
"aldgh;elskghlskhglsghlshg;slghl;sgha;lgh;lahg;lshg;lshg;lshg" +
"aldgh;elskghlskhglsghlshg;slghl;sgha;lgh;lahg;lshg;lshg;lshg" +
"aldgh;elskghlskhglsghlshg;slghl;sgha;lgh;lahg;lshg;lshg;lshg" +
"aldgh;elskghlskhglsghlshg;slghl;sgha;lgh;lahg;lshg;lshg;lshg" +
"aldgh;elskghlskhglsghlshg;slghl;sgha;lgh;lahg;lshg;lshg;lshg" +
"aldgh;elskghlskhglsghlshg;slghl;sgha;lgh;lahg;lshg;lshg;lshg" +
"aldgh;elskghlskhglsghlshg;slghl;sgha;lgh;lahg;lshg;lshg;lshg" +
"aldgh;elskghlskhglsghlshg;slghl;sgha;lgh;lahg;lshg;lshg;lshg" +
"aldgh;elskghlskhglsghlshg;slghl;sgha;lgh;lahg;lshg;lshg;lshg" +
"aldgh;elskghlskhglsghlshg;slghl;sgha;lgh;lahg;lshg;lshg;lshg" +
"aldgh;elskghlskhglsghlshg;slghl;sgha;lgh;lahg;lshg;lshg;lshg";
Encoding encoding = Encoding.GetEncoding("iso-8859-1");
_message = encoding.GetBytes(str);
_acceptCallback = new AsyncCallback(this.HandleSocketAccept);
_acceptSend = new AsyncCallback(this.HandleSendData);
_socket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
_socket.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.ReuseAddress,1);
IPEndPoint endPoint = new IPEndPoint(IPAddress.Any,port);
_socket.Bind(endPoint);
_socket.Listen(1);
_socket.BeginAccept(_acceptCallback,_socket);
}
private void HandleSocketAccept(IAsyncResult result)
{
Socket server = (Socket)result.AsyncState;
Socket socket = server.EndAccept(result);
// start test
while (true)
{
socket.BeginSend(_message,0,_message.Length,SocketFlags.None,_acceptSend,socket);
Console.WriteLine("Sent msg!");
}
}
private void HandleSendData(IAsyncResult result)
{
Socket socket = null;
try
{
socket = (Socket)result.AsyncState;
int numBytes = socket.EndSend(result);
Console.WriteLine("wrote " + numBytes + " bytes!");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
public class Class1
{
public static void Main(string[] args)
{
try
{
Server server = new Server(6969);
Console.WriteLine("Listening!");
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
}
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace Client
{
public class Client
{
private AsyncCallback _acceptReceive;
private Socket _socket;
private byte[] _buffer;
public Client(int port)
{
_buffer = new byte[1024];
_acceptReceive = new AsyncCallback(this.HandleReceiveData);
_socket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
_socket.Connect(new
IPEndPoint(Dns.Resolve("localhost").AddressList[0],port));
_socket.BeginReceive(_buffer,0,_buffer.Length,SocketFlags.None,_acceptReceive,_socket);
}
private void HandleReceiveData(IAsyncResult result)
{
Socket socket = null;
try
{
socket = (Socket)result.AsyncState;
int numBytes = socket.EndReceive(result);
if (numBytes > 0)
{
Console.Write(".");
Thread.Sleep(1000);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
class Class1
{
public static void Main(string[] args)
{
Client client = new Client(6969);
Console.WriteLine("Client started!");
Console.ReadLine();
}
}
}
Can anyone explain the behavior of what's happening under the covers
when the following client intereacts with the following server (code
below)? This code is basically a stripped down version of a system I'm
trying to put together that is having major stability issues.
Basically, the client connects to the server and starts pouring
information onto the socket connection in an asynchronous fashion. The
server takes it's time processing the information it receives off of
the socket connection. Eventually, the whole thing blows up... and
when it blows up, I mean until I kill the server process, I can't use
IE anymore, instant messager dies, PCAnywhere dies, etc..
Here's the exception that gets thrown server side:
Unhandled Exception: System.InvalidOperationException: AcceptCallback
---> Syste
m.Net.Sockets.SocketException: An operation on a socket could not be
performed b
ecause the system lacked sufficient buffer space or because a queue
was full
at System.Net.Sockets.Socket.BeginSend(Byte[] buffer, Int32 offset,
Int32 siz
e, SocketFlags socketFlags, AsyncCallback callback, Object state)
at Server.Server.HandleSocketAccept(IAsyncResult result) in
v:\socketbuffer\s
erver\class1.cs:line 65
at System.Net.Sockets.AcceptAsyncResult.AcceptCallback(Object
stateObject, Bo
olean Signaled)
--- End of inner exception stack trace ---
at System.Net.Sockets.AcceptAsyncResult.AcceptCallback(Object
stateObject, Bo
olean Signaled)
And, here's the code:
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace Server
{
public class Server
{
private AsyncCallback _acceptCallback;
private AsyncCallback _acceptSend;
private Socket _socket;
private byte[] _message;
public Server(int port)
{
string str = "aldgh;elskghlskhglsghlshg;slghl;sgha;lgh;lahg;lshg;lshg;lshg"
+
"aldgh;elskghlskhglsghlshg;slghl;sgha;lgh;lahg;lshg;lshg;lshg" +
"aldgh;elskghlskhglsghlshg;slghl;sgha;lgh;lahg;lshg;lshg;lshg" +
"aldgh;elskghlskhglsghlshg;slghl;sgha;lgh;lahg;lshg;lshg;lshg" +
"aldgh;elskghlskhglsghlshg;slghl;sgha;lgh;lahg;lshg;lshg;lshg" +
"aldgh;elskghlskhglsghlshg;slghl;sgha;lgh;lahg;lshg;lshg;lshg" +
"aldgh;elskghlskhglsghlshg;slghl;sgha;lgh;lahg;lshg;lshg;lshg" +
"aldgh;elskghlskhglsghlshg;slghl;sgha;lgh;lahg;lshg;lshg;lshg" +
"aldgh;elskghlskhglsghlshg;slghl;sgha;lgh;lahg;lshg;lshg;lshg" +
"aldgh;elskghlskhglsghlshg;slghl;sgha;lgh;lahg;lshg;lshg;lshg" +
"aldgh;elskghlskhglsghlshg;slghl;sgha;lgh;lahg;lshg;lshg;lshg" +
"aldgh;elskghlskhglsghlshg;slghl;sgha;lgh;lahg;lshg;lshg;lshg" +
"aldgh;elskghlskhglsghlshg;slghl;sgha;lgh;lahg;lshg;lshg;lshg" +
"aldgh;elskghlskhglsghlshg;slghl;sgha;lgh;lahg;lshg;lshg;lshg" +
"aldgh;elskghlskhglsghlshg;slghl;sgha;lgh;lahg;lshg;lshg;lshg" +
"aldgh;elskghlskhglsghlshg;slghl;sgha;lgh;lahg;lshg;lshg;lshg" +
"aldgh;elskghlskhglsghlshg;slghl;sgha;lgh;lahg;lshg;lshg;lshg" +
"aldgh;elskghlskhglsghlshg;slghl;sgha;lgh;lahg;lshg;lshg;lshg" +
"aldgh;elskghlskhglsghlshg;slghl;sgha;lgh;lahg;lshg;lshg;lshg" +
"aldgh;elskghlskhglsghlshg;slghl;sgha;lgh;lahg;lshg;lshg;lshg" +
"aldgh;elskghlskhglsghlshg;slghl;sgha;lgh;lahg;lshg;lshg;lshg" +
"aldgh;elskghlskhglsghlshg;slghl;sgha;lgh;lahg;lshg;lshg;lshg";
Encoding encoding = Encoding.GetEncoding("iso-8859-1");
_message = encoding.GetBytes(str);
_acceptCallback = new AsyncCallback(this.HandleSocketAccept);
_acceptSend = new AsyncCallback(this.HandleSendData);
_socket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
_socket.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.ReuseAddress,1);
IPEndPoint endPoint = new IPEndPoint(IPAddress.Any,port);
_socket.Bind(endPoint);
_socket.Listen(1);
_socket.BeginAccept(_acceptCallback,_socket);
}
private void HandleSocketAccept(IAsyncResult result)
{
Socket server = (Socket)result.AsyncState;
Socket socket = server.EndAccept(result);
// start test
while (true)
{
socket.BeginSend(_message,0,_message.Length,SocketFlags.None,_acceptSend,socket);
Console.WriteLine("Sent msg!");
}
}
private void HandleSendData(IAsyncResult result)
{
Socket socket = null;
try
{
socket = (Socket)result.AsyncState;
int numBytes = socket.EndSend(result);
Console.WriteLine("wrote " + numBytes + " bytes!");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
public class Class1
{
public static void Main(string[] args)
{
try
{
Server server = new Server(6969);
Console.WriteLine("Listening!");
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
}
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace Client
{
public class Client
{
private AsyncCallback _acceptReceive;
private Socket _socket;
private byte[] _buffer;
public Client(int port)
{
_buffer = new byte[1024];
_acceptReceive = new AsyncCallback(this.HandleReceiveData);
_socket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
_socket.Connect(new
IPEndPoint(Dns.Resolve("localhost").AddressList[0],port));
_socket.BeginReceive(_buffer,0,_buffer.Length,SocketFlags.None,_acceptReceive,_socket);
}
private void HandleReceiveData(IAsyncResult result)
{
Socket socket = null;
try
{
socket = (Socket)result.AsyncState;
int numBytes = socket.EndReceive(result);
if (numBytes > 0)
{
Console.Write(".");
Thread.Sleep(1000);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
class Class1
{
public static void Main(string[] args)
{
Client client = new Client(6969);
Console.WriteLine("Client started!");
Console.ReadLine();
}
}
}