Asynchronous Client Server Application-Please Help
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 class CSocketPacket
{
public System.Net.Sockets.Socket thisSocket;
public byte[] dataBuffer = new byte[1];
public Hashtable threadtable= new Hashtable(); // storing thread and connectID
public Hashtable sockettable= new Hashtable(); // storing socket and connectID
}
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 );
}
}