If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Socket idle time problem in Windows Service

/*
Hello,

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:
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Collections;
using TheCodeKing.Net.Messaging;
using System.Globalization;
using System.Diagnostics;
using System.Threading;
using System.ComponentModel;

/// <summary>
/// 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
/// </summary>
private static System.Collections.ArrayList arrayWorkerSocketList =
ArrayList.Synchronized(new System.Collections.ArrayList());

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[1024];

// Let the worker Socket do the further processing for the
// just connected client
WaitForData(socketPaketWithTimer);

// 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);
}
}

try
{
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);
}

Re: Socket idle time problem in Windows Service

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.

Code:

bool hook = true;
if (hook)
{
System.Threading.Thread.Sleep(15000);
}

This will give you about 15 seconds to attach once the service is started. I normally put a stop on the next line.

* The Perfect Platform for Game Developers: Android
Developing rich, high performance Android games from the ground up is a daunting task. Intel has provided Android developers with a number of tools that can be leveraged by Android game developers.

* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.