Hi Berry
appologies for the confusing statement there. What I meant was that the
Socket Server is running Asynchronously. What is really bugging me is
that the system performance takes a big hit when sending the
transactions thru sockets.
here's what the code looks like:
void CreateServer()
{
int nPort = GetPortNumber();
serverSocket = new Socket(AddressFamily.InterNetwork,SocketType.Strea m,
ProtocolType.Tcp);
IPEndPoint ipLocal = new IPEndPoint (IPAddress.Any, nPort);
serverSocket.Bind( ipLocal );
serverSocket.Listen(100);
serverSocket.BeginAccept(new AsyncCallback (OnConnectRequest), null);
}
public void OnConnectRequest(IAsyncResult asyncRes)
{
Socket workerSocket = serverSocket.EndAccept (asyncRes);
Interlocked.Increment(ref m_clientCount);
m_workerSocketList.Add(workerSocket);
TransManager txnProcThread = new TransManager(m_clientCount,
fieldDescFilePath, ref htFieldDescList, componentName);
txnProcThread.Init();
htTxnProc.Add(m_clientCount, txnProcThread );
AwaitNOFData(workerSocket, m_clientCount);
ratlServerSocket.BeginAccept(new AsyncCallback ( OnConnectRequest
),null);
}
public void AwaitNOFData(System.Net.Sockets.Socket soc, int
clientNumber)
{
if ( pfnWorkerCallBack == null )
{ pfnWorkerCallBack = new AsyncCallback (ReadNOFData); }
SocketPacket theSocPkt = new SocketPacket (soc, clientNumber);
soc.BeginReceive (theSocPkt.dataBuffer, 0,
theSocPkt.dataBuffer.Length, SocketFlags.None,
pfnWorkerCallBack, theSocPkt);
}
public void ReadNOFData(IAsyncResult asyn)
{
SocketPacket socketData = (SocketPacket)asyn.AsyncState ;
int iRx = socketData.m_currentSocket.EndReceive(asyn);
char[] chars = new char[iRx + 1];
System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
int charLen = d.GetChars(socketData.dataBuffer, 0, iRx, chars, 0);
//Get the input data into string format for invoking PROCESS MESSAGE
string szData = new System.String(chars);
// READ THE INPUT DATA AND INVOKE THE TRANSACTION
try
{
TransManager currentTxn =
(TransManager)htTxnProc[socketData.m_clientNumber];
string returnMsg = currentTxn.DoProcessMsg(szData);
byte[] byData = System.Text.Encoding.ASCII.GetBytes(returnMsg);
Prevresponse = returnMsg.ToString();
Socket workerSocket = (Socket)socketData.m_currentSocket;
workerSocket.Send(byData);
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
throw;
}
// Continue the waiting for data on the Socket
AwaitNOFData(socketData.m_currentSocket, socketData.m_clientNumber );
}
Regards
Pawan