PDA

Click to See Complete Forum and Search --> : Asynchronous Client Server Application-Please Help


Rahils573
Apr 8th, 2004, 04:07 PM
Hello Everybody:
I am creating a client server application asynchronously. I am storing the listener and the thread in a hashtable and then getting that connection from the hashtable .However when I access the connection the BeginReceive method does not work.Can anyone pls tell me what is the problem.

Thanks in advance..

Kapil


public void WaitForData(System.Net.Sockets.Socket soc)
{

try
{
if ( pfnWorkerCallBack == null )
{
pfnWorkerCallBack = new AsyncCallback (OnDataReceived);
}

CSocketPacket theSocPkt = new CSocketPacket ();
theSocPkt.thisSocket = soc;
if (connectId < 10000)
connectId++;
else
connectId = 1;
if (theSocPkt.sockettable.Count < MaxConnected )
{
while (theSocPkt.sockettable.Contains(connectId) )
{
Interlocked.Increment(ref connectId);
}
lock(this)
{
// it is used to keep connected Sockets
theSocPkt.sockettable.Add(connectId,m_socListener);
// it is used to keep the active thread
theSocPkt.threadtable.Add(connectId, tcpThd);

}



}

int realId= connectId;
soc =(Socket)theSocPkt.sockettable[realId];
while(true)
{
if(soc.Connected)
// now start to listen for any data...
{
soc.BeginReceive (theSocPkt.dataBuffer ,0,theSocPkt.dataBuffer.Length ,SocketFlags.None,pfnWorkerCallBack,theSocPkt);
}
}
}


catch(SocketException se)
{
MessageBox.Show (se.Message );
}

}