I wrote class for management clients socket connection using Windows Service.
In this class I want to manage client idle time. For this I'm using SocketPaketWithTimer,
with System.Timers.Timer and OnTimerElapsed event. The problem is, that after OnTimerElapsed
event occurred I want pass parameters in CloseClientSocket, but at this moment windows service stops responding
(When client sends "Close" the socket service closes client socket without problem).
This occurs on second client close. I think the problem is Timer event, which is executed in separate thread.
Here is my class:
private int intGlobalClientCount = 0;
private string strGlobalConnIdent = "";
public AsyncCallback pfnWorkerCallBack;
/// An ArrayList is used to keep track of worker sockets that are designed
/// to communicate with each connected client. Make it a synchronized ArrayList For thread safety
private static System.Collections.ArrayList arrayWorkerSocketList =
private Socket mainSocket;
public class SocketPacket
public Socket m_currentSocket;
public int m_clientNumber;
public string m_clientIdent;
public DateTime m_openDateTime;
// Buffer to store the data sent by the client
public byte dataBuffer = new byte;
// Constructor which takes a Socket and a client number
public SocketPacket(System.Net.Sockets.Socket socket, int clientNumber, string clientIdent, DateTime connOpenDateTime)
m_currentSocket = socket;
m_clientNumber = clientNumber;
m_clientIdent = clientIdent;
m_openDateTime = connOpenDateTime;
public class SocketPaketWithTimer
public SocketPacket m_currentSocketPacket;
public System.Timers.Timer m_connIdleTimer;
public SocketPaketWithTimer(SocketPacket objSocketPacket, System.Timers.Timer objSocketPacketTimer)
m_currentSocketPacket = objSocketPacket;
if (objSocketPacketTimer == null)
m_connIdleTimer = new System.Timers.Timer(20000);
// Here we complete/end the BeginAccept() asynchronous call
// by calling EndAccept() - which returns the reference to
// a new Socket object
Socket workerSocket = mainSocket.EndAccept(asyn);
// Now increment the client count for this client
// in a thread safe manner
strGlobalConnIdent = clsAddings.RandomString();
SocketPacket socketPacket = new SocketPacket(workerSocket, intGlobalClientCount, strGlobalConnIdent, dtCurrentDateTime);
SocketPaketWithTimer socketPaketWithTimer = new SocketPaketWithTimer(socketPacket, null);
// Add the workerSocket reference to arrayWorkerSocketList ArrayList
// Send a welcome message to client
string msg = "Welcome client " + m_clientCount + "\n";
// Update the list box showing the list of clients (thread safe call)
// Let the worker Socket do the further processing for the
// just connected client
// Since the main Socket is now free, it can go back and wait for
// other clients who are attempting to connect
mainSocket.BeginAccept(new AsyncCallback(OnClientConnect), null);
catch (Exception exc)
clsSystemEvents.writeEvent("შეცდომა კომუნიკაციის დამყარების დროს: " + exc.Message, EventLogEntryType.Error, 1);
/// ელოდება მონაცემებს მიერთებული მომხარებლისგან
private void WaitForData(SocketPaketWithTimer socPacketWithTimer)
int aa = Thread.CurrentThread.ManagedThreadId;
if (pfnWorkerCallBack == null)
// Specify the call back function which is to be
// invoked when there is any write activity by the
// connected client
pfnWorkerCallBack = new AsyncCallback(OnDataReceived);
// Complete the BeginReceive() asynchronous call by EndReceive() method
// which will return the number of characters written to the stream
// by the client
int iRx = socketData.m_currentSocketPacket.m_currentSocket.EndReceive(asyn);
char chars = new char[iRx + 1];
// Extract the characters as a buffer
System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
int charLen = d.GetChars(socketData.m_currentSocketPacket.dataBuffer,
0, iRx, chars, 0);
System.String szData = new System.String(chars);
// კავშირის დახურვა
if (szData == "Close\0")
// Continue the waiting for data on the Socket
catch (ObjectDisposedException exc)
System.Diagnostics.Debugger.Log(0, "1", "\nOnDataReceived: Socket has been closed\n");
catch (SocketException se)
if (se.ErrorCode == 10054) // Error code for Connection reset by peer
string msg = "Client " + socketData.m_currentSocketPacket.m_clientNumber + " Disconnected" + "\n";
// Remove the reference to the worker socket of the closed client
// so that this object will get garbage collected
arrayWorkerSocketList[socketData.m_currentSocketPacket.m_clientNumber - 1] = null;
You're going to need to be able to step through your code to find the problem. With Windows Services this can be a bit trickier than a normal exe or web project.
First, you'll need to find the InstallUtil.exe for the .Net Framework version that you are working with. It is installed with your .Net Framework. Mine are under C:\Windows\Microsoft.NET\Framework\ in the folder for the version. Versions 2.0, 3.0, and 3.5 use the one in the folder for 2.0 (mine is v2.0.50727). The one for 4.0 is under the 4.0 folder (mine is v4.0.30319). It is simple to use, see http://msdn.microsoft.com/en-us/libr...(v=vs.90).aspx.
Next, I ad some code in my OnStart handler for my service to allow time to attach the Visual Studio debugger to the running service e.g.
bool hook = true;
This will give you about 15 seconds to attach once the service is started. I normally put a stop on the next line.
1. Install your service's exe (the one in your project's Debug folder) using InstallUtil.exe.
2. Open the Computer Management Console and start the service.
3. Immediately switch to Visual Studio and click on "Tools\Attatch to Process" from the menu bar.
4. Select your running service from the dialog and click the Attach button.
Once attached to the process and the 15 seconds has elapsed, you will be able to step through your code in debug mode just like any other type of project.
Put plenty of stops through out the code that you need to check and remember to remove the code you added to your OnStart.
Last edited by CGKevin; November 6th, 2012 at 05:16 PM.