I'm working on a c# framework 4.8 project which has a number of tcp sockets (+/-20) waiting for data. When I start the project everything is fine and it receives like 10 packets a minute.
CPU is low round 10%
When the program runs for like 2 hours the CPU rises to 40-50% even when no data is received.
I've attached the remote debugger and discovered the Garbage Collector is collecting like Crazy. After profiling the CPU the Hotpath ends in System.Configuration.Dll.
Can anybody tell me what the system.configuration.dll has to do with sockets and how to fix this?
public class TcpConnectionState: ConnectionStateBase, IConnectionState
{
private byte[] _buffer;
private Socket _socket;
private ReceiveCallbackDelegate _receiveCallback;
public IPEndPoint Endpoint => (IPEndPoint)_socket.RemoteEndPoint;
private readonly ILogger _logger;
public TcpConnectionState(ILogger<TcpConnectionState> logger, Socket socket, byte[] buffer, IDevice device, ReceiveCallbackDelegate receiveCallback, DisconnectCallbackDelegate disconnectCallback):
base(device, disconnectCallback)
{
_logger = Validate.IsNotNull(logger, nameof(logger));
_socket = socket;
_buffer = buffer;
_receiveCallback = receiveCallback;
BeginReceive();
}
public bool IsConnected()
{
try
{
return !(_socket.Poll(1, SelectMode.SelectRead) && _socket.Available == 0);
}
catch (SocketException)
{
return false;
}
}
private void BeginReceive()
{
try
{
_socket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, BeginReceiveCallback, null);
}
catch (SocketException)
{
Disconnect();
}
}
private void BeginReceiveCallback(IAsyncResult ar)
{
try
{
int bytesReceived = _socket.EndReceive(ar);
if (bytesReceived > 0)
{
var data = _buffer.Take<byte>(bytesReceived).ToArray<byte>();
_receiveCallback(data, null, _socket.RemoteEndPoint);
}
BeginReceive();
}
catch (SocketException ex)
{
if (ex.SocketErrorCode == SocketError.ConnectionReset)
{
_logger.LogInformation(ex.Message);
}
else
{
_logger.LogError(ex, string.Empty);
}
Disconnect();
}
catch(Exception ex)
{
_logger.LogError(ex, string.Empty);
Disconnect();
}
}
public void Send(byte[] data)
{
try
{
_socket.BeginSend(data, 0, data.Length, SocketFlags.None, BeginSendCallback, null);
_logger.LogTrace(data, d => $"Send {BitConverter.ToString(d)}");
_logger.LogDebug($"Sent {data.Length} bytes");
}
catch(SocketException ex)
{
if (ex.SocketErrorCode == SocketError.ConnectionReset)
{
_logger.LogInformation(ex.Message);
}
else
{
_logger.LogError(ex, string.Empty);
}
Disconnect();
}
catch (Exception ex)
{
_logger.LogError(ex, string.Empty);
Disconnect();
}
}
private void BeginSendCallback(IAsyncResult ar)
{
var bytesSent = _socket.EndSend(ar);
}
public override void Disconnect()
{
_socket.BeginDisconnect(false, BeginDisconnectCallback, null);
}
private void BeginDisconnectCallback(IAsyncResult ar)
{
_logger.LogDebug("Disconnect");
base.Disconnect();
}
}
Related
Issue description.
I sending and receiving data. When i launch program, data who i send and receive, displayed is right. If relaunch program in 1 minutes, no more, data send and receive displayed is currect.
BUT! IF i relaunching program more then after 2 minutes, FIRST receiving and sending displayed is right, BUT SECOND sending and receiving data, is not displayed (((
What's happening, and how i fix this???
Thanks for any help!
public override bool Connect()
{
try
{
if (NetClient != null)
{
NetClient.Close();
}
NetClient = new Socket(ConnectionPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
IAsyncResult result = NetClient.BeginConnect(ConnectionPoint, null, null);
bool success = result.AsyncWaitHandle.WaitOne(connectionDelay, false/*true*/);
if (NetClient.Connected)
{
Log("report.log", string.Format("Connection OK"));
return true;
}
Log("report.log", string.Format("NetClient is not connected"));
}
catch (Exception e)
{
MessageBox.Show(e.Message);
Log("report.log", string.Format("NetClient error while connecting: {0}", e.Message));
}
return false;
}
public override bool Send(byte[] data)
{
try
{
if (NetClient.Connected)
{
NetClient.SendTo(data, ConnectionPoint);
Thread.Sleep(transferDelay);
return true;
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
return false;
}
public override string Receive(int length)
{
byte[] data = new byte[bufferSize]; // bufferSize = i tryed any buffer from 5 bytes to 16384 bytes
string strbuff = "";
try
{
if (NetClient.Connected)
{
do
{
IAsyncResult result = NetClient.BeginReceive(data, 0, bufferSize, SocketFlags.None, null, null);
bool success = result.AsyncWaitHandle.WaitOne(connectionDelay, true);
int receivedBytesCount = NetClient.EndReceive(result);
if (receivedBytesCount == 0)
{
receivedBytesCount = 0;
}
strbuff += Encoding.ASCII.GetString(data, 0, receivedBytesCount);
} while (NetClient.Available > 0);
if (NetClient != null && NetClient.Connected)
{
NetClient.Shutdown(SocketShutdown.Both);
NetClient.Close();
}
return strbuff;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return null;
}
public override void Disconnect()
{
try
{
if (NetClient.Connected)
{
NetClient.Shutdown(SocketShutdown.Both);
NetClient.Close();
}
else
{
if (NetClient != null)
{
NetClient.Close();
}
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
I am building a tcp port forwarding application. The client connects to the server on a particular port and internally the request is routed in the server to a remote port and response is passed back to the client. Below is my code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace BBGRouter
{
class Router
{
#region log4net
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
#endregion //log4net
bool isShutdown = false;
private Socket router = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
public event EventHandler OnRestartNeeded;
//only one time
Thread thRouterTh;
public Router()
{
thRouterTh = new Thread(new ParameterizedThreadStart(RouterProc));
}
IPEndPoint local, remote;
public void Start(IPEndPoint local, IPEndPoint remote)
{
if (log.IsInfoEnabled) { log.Info("Listening on " + local); }
this.local = local;
this.remote = remote;
router.Bind(local);
router.Listen(10);
thRouterTh.Name = "router thread";
thRouterTh.Start();
}
void RouterProc(object obj)
{
while (!isShutdown)
{
try
{
var source = router.Accept();
if (log.IsInfoEnabled) { log.Info("Creating new session...."); }
var destination = new Router();
var state = new State(source, destination.router);
destination.Connect(remote, source);
source.BeginReceive(state.Buffer, 0, state.Buffer.Length, 0, OnDataReceive, state);
}
catch (Exception ex)
{
if (log.IsErrorEnabled) { log.Error("Exception in Router thread", ex); }
if (isShutdown) { if (log.IsInfoEnabled) { log.Info("Shutting down..."); } }
}
}
}
public void Join()
{
if (thRouterTh != null)
thRouterTh.Join();
}
private void StopAndFireRestart(EventArgs e)
{
Stop();
log.Info("Stopped router");
EventHandler handler = OnRestartNeeded;
if (handler != null)
{
log.Info("Firing the restart event now");
handler(this, e);
}
}
public void Stop()
{
try
{
isShutdown = true;
if (log.IsInfoEnabled) { log.Info("Stopping router thread"); }
router.Shutdown(SocketShutdown.Both);
//router.Shutdown(SocketShutdown.Receive);
}
catch (Exception ex)
{
if (log.IsErrorEnabled) { log.Error("Exception while stopping", ex); }
}
finally
{
thRouterTh.Interrupt();
router.Dispose();
}
}
private void Connect(EndPoint remoteEndpoint, Socket destination)
{
if (log.IsInfoEnabled) { log.InfoFormat("connecting session at {0}", remoteEndpoint.ToString()); }
var state = new State(router, destination);
try
{
router.Connect(remoteEndpoint);
router.BeginReceive(state.Buffer, 0, state.Buffer.Length, SocketFlags.None, OnDataReceive, state);
}
catch (SocketException e)
{
if (log.IsErrorEnabled) { log.Error(string.Format("SocketException while connect: Exception: {0}, ErrorCode: {1}", e, e.ErrorCode)); }
// Stop the service
StopAndFireRestart(new EventArgs());
}
catch (Exception ex)
{
if (log.IsErrorEnabled) { log.Error("exception while connect {0}", ex); }
}
}
private void OnDataReceive(IAsyncResult result)
{
var state = (State)result.AsyncState;
try
{
var bytesRead = state.SourceSocket.EndReceive(result);
if (bytesRead > 0)
{
log.Info(string.Format("Bytes read: {0}", bytesRead));
state.DestinationSocket.Send(state.Buffer, bytesRead, SocketFlags.None);
state.SourceSocket.BeginReceive(state.Buffer, 0, state.Buffer.Length, 0, OnDataReceive, state);
}
}
catch (Exception e)
{
if (log.IsErrorEnabled) { log.Error("Exception receiving the data, Closing source/destination sockets", e); }
//state.DestinationSocket.Close();
//state.SourceSocket.Close();
//StopAndFireRestart(new EventArgs());
}
}
private class State
{
public Socket SourceSocket { get; private set; }
public Socket DestinationSocket { get; private set; }
public byte[] Buffer { get; private set; }
public State(Socket source, Socket destination)
{
SourceSocket = source;
DestinationSocket = destination;
Buffer = new byte[8192];
}
}
}
}
The problem happens when suddenly the client gets disconnected from the remote port randomly after 4-5 hrs. How can I reconnect the client gracefully so that there is no change in the client code ?
Why don't you solve this issue with netsh?
netsh interface portproxy add v4tov4 listenport=LOCALPORT listenaddress=localhost connectport=REMOTEPORT connectaddress=REMOTEADDRESS
or
netsh interface portproxy add v4tov4 listenport=LOCALPORT connectport=REMOTEPORT connectaddress=REMOTEADDRESS
More information on netsh available on MSDN.
I am working on a client-server application on C# using async sockets. As I listen for connections, I keep getting this error
An unhandled exception of type 'System.Net.Sockets.SocketException'
occurred in System.dll
Additional information: Only one usage of each socket address (protocol/network address/port) is normally permitted
This is the line where this exception happens:
_listener.Bind(new IPEndPoint(0, port));
The code is under Listener.Start(int port). In the main program, here's how I call the method:
void btnListen_Click(object sender, EventArgs e)
{
_listener = new Listener();
_listener.Accepted += new Listener.SocketAcceptedHandler(listener_Accepted);
_listener.Start(8192);
}
I am testing both the client and server applications in my PC.
Here's the Listener class.
namespace serverPC
{
class Listener
{
public delegate void SocketAcceptedHandler(Socket e);
public event SocketAcceptedHandler Accepted;
Socket _listener;
public int Port;
public bool Running
{
get;
private set;
}
public Listener() {Port = 0;}
public void Start(int port)
{
if (Running)
return;
_listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_listener.Bind(new IPEndPoint(0, port)); // This is where the SocketException error occurs
_listener.Listen(0);
_listener.BeginAccept(acceptedCallback, null);
Running = true;
}
public void Stop()
{
if (!Running)
return;
_listener.Close();
Running = false;
}
void acceptedCallback(IAsyncResult ar)
{
try
{
Socket s = _listener.EndAccept(ar);
if (Accepted != null)
{
Accepted(s);
}
}
catch
{
}
if (Running)
{
try
{
_listener.BeginAccept(acceptedCallback, null);
}
catch
{
}
}
}
}
}
Here's the Client class.
namespace serverPC
{
struct ReceiveBuffer
{
public const int BUFFER_SIZE = 1024;
public byte[] Buffer;
public int ToReceive;
public MemoryStream BufStream;
public ReceiveBuffer(int toRec)
{
Buffer = new byte[BUFFER_SIZE];
ToReceive = toRec;
BufStream = new MemoryStream(toRec);
}
public void Dispose()
{
Buffer = null;
ToReceive = 0;
Close();
if (BufStream != null)
BufStream.Dispose();
}
public void Close()
{
if (BufStream != null && BufStream.CanWrite)
{
BufStream.Close();
}
}
}
class Client
{
byte[] lenBuffer;
ReceiveBuffer buffer;
Socket _socket;
public IPEndPoint EndPoint
{
get
{
if (_socket != null && _socket.Connected)
{
return (IPEndPoint)_socket.RemoteEndPoint;
}
return new IPEndPoint(IPAddress.None, 0);
}
}
public delegate void DisconnectedEventHandler(Client sender);
public event DisconnectedEventHandler Disconnected;
public delegate void DataReceivedEventHandler(Client sender, ReceiveBuffer e);
public event DataReceivedEventHandler DataReceived;
public Client(Socket s)
{
_socket = s;
lenBuffer = new byte[4];
}
public void Close()
{
if (_socket != null)
{
_socket.Disconnect(false);
_socket.Close();
}
buffer.Dispose();
_socket = null;
lenBuffer = null;
Disconnected = null;
DataReceived = null;
}
public void ReceiveAsync()
{
_socket.BeginReceive(lenBuffer, 0, lenBuffer.Length, SocketFlags.None, receiveCallback, null);
}
void receiveCallback(IAsyncResult ar)
{
try
{
int rec = _socket.EndReceive(ar);
if (rec == 0)
{
if (Disconnected != null)
{
Disconnected(this);
return;
}
if (rec != 4)
{
throw new Exception();
}
}
}
catch (SocketException se)
{
switch (se.SocketErrorCode)
{
case SocketError.ConnectionAborted:
case SocketError.ConnectionReset:
if (Disconnected != null)
{
Disconnected(this);
return;
}
break;
}
}
catch (ObjectDisposedException)
{
return;
}
catch (NullReferenceException)
{
return;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return;
}
buffer = new ReceiveBuffer(BitConverter.ToInt32(lenBuffer, 0));
_socket.BeginReceive(buffer.Buffer, 0, buffer.Buffer.Length, SocketFlags.None, receivePacketCallback, null);
}
void receivePacketCallback(IAsyncResult ar)
{
int rec = _socket.EndReceive(ar);
if (rec <= 0)
{
return;
}
buffer.BufStream.Write(buffer.Buffer, 0, rec);
buffer.ToReceive -= rec;
if(buffer.ToReceive > 0)
{
Array.Clear(buffer.Buffer, 0, buffer.Buffer.Length);
_socket.BeginReceive(buffer.Buffer, 0, buffer.Buffer.Length, SocketFlags.None, receivePacketCallback, null);
return;
}
if (DataReceived != null)
{
buffer.BufStream.Position = 0;
DataReceived(this, buffer);
}
buffer.Dispose();
ReceiveAsync();
}
}
}
Why do I get the SocketException error? Any help is appreciated. Thanks!
So the obvious things to check - Something is already listening on port 8192 or there's some other reason why your machine would reject your program opening that port (firewalls, etc).
The other problem is while you are blocking binding twice with the "Running" check, you're only doing that for the instance of listener - hitting the button will create a whole new listener and attempt to have it listen on port 8192 - which since you already have a listener bound there, will fail with that exception.
using resources at http://www.developerfusion.com i came up with the following server code. It works well for single client. How can i make it for multiple clients. I tried adding array and different instances of the beiginreceive but was unable to. The server code:
class Program
{
public static AsyncCallback pfnWorkerCallBack;
public static Socket m_socListener;
public static Socket m_socWorker;
static void Main(string[] args)
{
try
{
//create the listening socket...
m_socListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ipLocal = new IPEndPoint(IPAddress.Any, 8221);
//bind to local IP Address...
m_socListener.Bind(ipLocal);
Console.WriteLine("UCManager Started");
//start listening...
m_socListener.Listen(4);
// create the call back for any client connections...
m_socListener.BeginAccept(new AsyncCallback(OnClientConnect), null);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
public static void OnClientConnect(IAsyncResult asyn)
{
try
{
m_socWorker = m_socListener.EndAccept(asyn);
WaitForData(m_socWorker);
}
catch (Exception se)
{
Console.WriteLine(se.Message);
}
}
public class CSocketPacket
{
public System.Net.Sockets.Socket thisSocket;
public byte[] dataBuffer = new byte[1];
}
public static void WaitForData(System.Net.Sockets.Socket soc)
{
try
{
if (pfnWorkerCallBack == null)
{
pfnWorkerCallBack = new AsyncCallback(OnDataReceived);
}
CSocketPacket theSocPkt = new CSocketPacket();
theSocPkt.thisSocket = soc;
// now start to listen for any data...
soc.BeginReceive(theSocPkt.dataBuffer, 0, theSocPkt.dataBuffer.Length, SocketFlags.None, pfnWorkerCallBack, theSocPkt);
}
catch (SocketException se)
{
Console.WriteLine(se.Message);
}
}
public static void OnDataReceived(IAsyncResult asyn)
{
try
{
CSocketPacket theSockId = (CSocketPacket)asyn.AsyncState;
//end receive...
int iRx = 0;
iRx = theSockId.thisSocket.EndReceive(asyn);
char[] chars = new char[iRx + 1];
Decoder d = Encoding.UTF8.GetDecoder();
int charLen = d.GetChars(theSockId.dataBuffer, 0, iRx, chars, 0);
String szData = new String(chars);
Console.WriteLine(szData);
int code = Convert.ToInt32(szData);
WaitForData(m_socWorker);
}
catch (Exception se)
{
Console.WriteLine(se.Message);
}
}
}
}
EDIT: Ok i did something on the lines of Async TCP Server for multiple Clients.
public static void OnClientConnect(IAsyncResult asyn)
{
try
{
Socket m_socWorkerInstance = m_socListener.EndAccept(asyn);
clientSocketList.Add(m_socWorkerInstance);
WaitForData(m_socWorkerInstance);
}
catch (ObjectDisposedException)
{
System.Diagnostics.Debugger.Log(0, "1", "\n OnClientConnection: Socket has been closed\n");
}
catch (SocketException se)
{
Console.WriteLine(se.Message);
}
}
public static void WaitForData(System.Net.Sockets.Socket soc)
{
try
{
if (pfnWorkerCallBack == null)
{
pfnWorkerCallBack = new AsyncCallback(OnDataReceived);
}
CSocketPacket theSocPkt = new CSocketPacket();
theSocPkt.thisSocket = soc;
// now start to listen for any data...
soc.BeginReceive(theSocPkt.dataBuffer, 0, theSocPkt.dataBuffer.Length, SocketFlags.None, pfnWorkerCallBack, theSocPkt);
m_socListener.BeginAccept(new AsyncCallback(OnClientConnect), null);
}
catch (SocketException se)
{
Console.WriteLine(se.Message);
}
}
Now i am able to connect two clients but not able to communicate ...I am unable to send message from server to client...any suggestions...also should i have different read functions for both clients if i connect with two client???
You call BeginAccept only once, so after one client has been accepted, your server won't accept any new connections. You should call BeginAccept again after accepting a connection.
Ok i was able to solve it using Async TCP Server for multiple Clients:
public static void OnClientConnect(IAsyncResult asyn)
{
try
{
Socket m_socWorkerInstance = m_socListener.EndAccept(asyn);
clientSocketList.Add(m_socWorkerInstance);
WaitForData(m_socWorkerInstance);
}
catch (ObjectDisposedException)
{
System.Diagnostics.Debugger.Log(0, "1", "\n OnClientConnection: Socket has been closed\n");
}
catch (SocketException se)
{
Console.WriteLine(se.Message);
}
}
public static void WaitForData(System.Net.Sockets.Socket soc)
{
try
{
if (pfnWorkerCallBack == null)
{
pfnWorkerCallBack = new AsyncCallback(OnDataReceived);
}
CSocketPacket theSocPkt = new CSocketPacket();
theSocPkt.thisSocket = soc;
// now start to listen for any data...
soc.BeginReceive(theSocPkt.dataBuffer, 0, theSocPkt.dataBuffer.Length, SocketFlags.None, pfnWorkerCallBack, theSocPkt);
m_socListener.BeginAccept(new AsyncCallback(OnClientConnect), null);
}
catch (SocketException se)
{
Console.WriteLine(se.Message);
}
}
Im writing an application which is going to act as a tcp listener for a single client. The client is a java applet and will periodically connect to the listener, send some data and then wait for a response.
The code for the TcpServer class below has been largely lifted from an example provided by a more knowledgeable stackoverflow member in response to a different question.
Everything was going great until I found something in testing which wasnt mentioned in any of the interface documents I have. After the server has responded to the client it must then disconnect the client and start listening again.
My first thought was to call Disconnect() from inside SendData() but this results in a call to ReceiveCompleted() from somewhere and a nasty exception about the socket already being disposed.
Is this requirement easily achievable with the code design I have, and will I run into any problems in reusing the socket for subsequent connections?
sealed class TcpServer : IDisposable
{
#region Fields
private const int SocketBufferSize = 1024;
private readonly TcpListener tcpListener;
private Socket connectedSocket;
private bool disposed = false;
#endregion Fields
#region Constructors
public TcpServer(int port)
{
tcpListener = new TcpListener(IPAddress.Any, port);
tcpListener.Start();
tcpListener.BeginAcceptSocket(EndAcceptSocket, tcpListener);
}
~TcpServer()
{
Dispose(false);
}
#endregion Constructors
#region Events
public event EventHandler<DataReceivedEventArgs> DataReceived;
public event EventHandler<IPEndPointEventArgs> SocketConnected;
public event EventHandler<IPEndPointEventArgs> SocketDisconnected;
#endregion Events
#region Methods
public void Dispose()
{
Dispose(true);
}
public void SendData(byte[] data)
{
if (connectedSocket == null)
{
return;
}
connectedSocket.Send(data);
}
private void BeginReceiveAsync(Socket sock, SocketAsyncEventArgs e)
{
if (!sock.ReceiveAsync(e))
{
ReceiveCompleted(sock, e);
}
}
private void Connected(Socket socket)
{
var endPoint = (IPEndPoint)socket.RemoteEndPoint;
connectedSocket = socket;
OnSocketConnected(endPoint);
}
private void Disconnect(Socket socket)
{
var endPoint = (IPEndPoint)socket.RemoteEndPoint;
socket.Close();
connectedSocket = null;
OnSocketDisconnected(endPoint);
tcpListener.BeginAcceptSocket(EndAcceptSocket, tcpListener);
}
private void Dispose(bool disposing)
{
if (this.disposed == false)
{
if (disposing)
{
try
{
if (tcpListener != null)
{
this.disposed = true;
tcpListener.Stop();
}
}
catch (Exception ex)
{
TraceLog.Error("TcpServer: tcpListener.Stop(): {0}", ex.Message);
}
try
{
if (connectedSocket != null)
{
connectedSocket.Close();
connectedSocket = null;
}
}
catch (SocketException ex)
{
TraceLog.Error("TcpServer: connectedSocket.Close(): {0}", ex);
}
}
this.disposed = true;
}
}
private void EndAcceptSocket(IAsyncResult asyncResult)
{
var listener = (TcpListener)asyncResult.AsyncState;
if (disposed)
{
return;
}
try
{
Socket sock = listener.EndAcceptSocket(asyncResult);
Connected(sock);
var e = new SocketAsyncEventArgs();
e.Completed += ReceiveCompleted;
e.SetBuffer(new byte[SocketBufferSize], 0, SocketBufferSize);
BeginReceiveAsync(sock, e);
}
catch (SocketException ex)
{
TraceLog.Error("TcpServer.EndAcceptSocket: {0}", ex.Message);
}
catch (Exception ex)
{
TraceLog.Error("TcpServer.EndAcceptSocket: {0}", ex.Message);
}
}
private void OnDataReceived(byte[] data, IPEndPoint ipEndPoint)
{
if (DataReceived != null)
{
DataReceived(this, new DataReceivedEventArgs(data, ipEndPoint));
}
}
private void OnSocketConnected(IPEndPoint ipEndPoint)
{
if (SocketConnected != null)
{
SocketConnected(this, new IPEndPointEventArgs(ipEndPoint));
}
}
private void OnSocketDisconnected(IPEndPoint ipEndPoint)
{
if (SocketDisconnected != null)
{
SocketDisconnected(this, new IPEndPointEventArgs(ipEndPoint));
}
}
private void ReceiveCompleted(object sender, SocketAsyncEventArgs e)
{
var sock = (Socket)sender;
if (!sock.Connected)
{
Disconnect(sock);
}
try
{
int size = e.BytesTransferred;
if (size == 0)
{
Disconnect(sock);
}
else
{
var buf = new byte[size];
Array.Copy(e.Buffer, buf, size);
ReceiveData(buf, (IPEndPoint)sock.RemoteEndPoint);
BeginReceiveAsync(sock, e);
}
}
catch (SocketException ex)
{
TraceLog.Error("TcpServer: ReceiveCompleted: {0}", ex.Message);
}
catch (Exception ex)
{
TraceLog.Error("TcpServer: ReceiveCompleted: {0}", ex.Message);
}
}
private void ReceiveData(byte[] data, IPEndPoint endPoint)
{
OnDataReceived(data, endPoint);
}
#endregion Methods
}
Whenever I'm writing code that wraps around System.Net.Sockets.Socket, I find myself constantly adding try/catch clauses for SocketException and ObjectDisposedException. In most cases, ObjectDisposedException can simply be ignored, as it, in 99% of cases, indicates that the client has simply disconnected.
At least that's my impression of how the Socket API in .NET works. Try adding some exception handlers here and there, and see how it goes. In any case, your Disconnect method should not do more than something like this:
public void Disconnect()
{
try
{
connectedSocket.Shutdown(SocketShutdown.Both);
}
catch (Exception)
{
// Ignore the exception. The client probably already disconnected.
}
connectedSocket.Dispose(); // This is safe; a double dispose will simply be ignored.
}
I hope that sheds some light on the issue...