Visual Basic .NET » Smart Device Application
Strange Socket Behavior -- Factor --


I am writing to ask for assitance with my socket programming. I'm having a strange issue happen with my software that I have not seen before and I do not know what changed.

Anyway, I have a program listening on a socket with an async callback set for when data comes in. In the past, this event fired every time something connected OR when it was closed remotely (it would throw SocketException error code # 10054 when it gets closed remotely.) All of a sudden a few days ago this stopped working 100% of the time. The callback simply doesn't run when a connection is opened and data sent in. On a few rare times the callback event fires but it cannot read any data from the reader (yes, data was sent.) Since I have learned all my .NET programming by myself, I'm sure I'm a hack, but I manage to pull things together that work most of the time. The problem here is that my code didn't change - it just stopped working and I have no idea how to debug it. I have my clients log in, send a connection string that I have devised and... the event just does not fire.

Around the same time this started happening I saw a .NET Framework 2.0 update go across Windows update.. so I thought maybe this could be it... initial testing confirmed this but more testing disproved it.
I'm not entirely convinced that it was not the problem yet but it's looking like that is not the culprit.

Any ideas of where to even start debugging this?

Note sure if this would help but here is some slimmed down code:

//This is my callback event that does neglects to fire... sometimes.
public void OnDeviceConnect(IAsyncResult asyn)
{
try
{
TCPServer Horse = new TCPServer();
//We got in here because something connected to us, so first accept the connection
//to a new Socket (worker socket)
Socket socWorker = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
socWorker = socMainListener.EndAccept(asyn);

//and now get what they sent, max 500 bytes
byte
;
socWorker.Receive(b);

// Since the main Socket Listener is now free, it can go back and wait for other devices who are
// attempting to connect to the server
socMainListener.BeginAccept(new AsyncCallback(this.OnDeviceConnect), null);

//convert the received bytes to a string
string message = ConvertBytesToString(b);

//Create a new instance of SomeClass, pass in the connection string to parse and load all status
//variables from SQL
SomeClass ss = new SomeClass (message);

//Check to see if this was a valid connection. If it was, we will start a
//Communications Thread and get to work
if (ss.Type != Constants.bad_device)
{ //We know a valid device contacted this server
bool s = ss.FillProduct();
if (s) //if all settings filled correctly
{
//Pass in the socket to the class.
ss.socWorker = socWorker;
ss.SendToConnectedSS("
OK
"); // Let client know we are ready to send packets

//Load this instance of SomeClass into a object to be pulled out in the thread.
DeviceQueue.Enqueue(ss);
//Start a new Thread, and make it background (so that when we close the main thread,
//everything closes)

Thread thrdDevice = new Thread(new ThreadStart(this.MainThread));
thrdDevice.IsBackground = true;
thrdDevice.Start();
}
}
else
{
ss.socWorker = socWorker;
ss.SendToConnectedSS("
DENIED
");
socWorker.Close();
}
}
catch(ObjectDisposedException ode)
{
MessageBox.Show(ode.Message);
}
catch(SocketException se)
{
if(se.ErrorCode == 10054) // Error code for Connection reset by peer
{
MessageBox.Show("Socket Closed by Device");
}
else
{
MessageBox.Show (se.Message);
}
}
}

-- Factor --


Just for clarification I know that the event is not firing because I set a breakpoint at teh first line. And it never breaks. Is this a wrong assumption?

[Submit Comment]Home