View Single Post

  #2 (permalink)  
Old 08-28-2007, 08:20 PM
jillujillu jillujillu is offline
Junior Member
 
Join Date: Aug 2007
Posts: 1
jillujillu is on a distinguished road
Thumbs up

Hi,
Here the answer goes..

The algorithm is like this.

1) Get the data
2) Open the socket
3) Wait for the recieve data
4) Send the data (3 and 4 can be replacable)
5) Receive the data
6) process and use the data
7) go and wait for the recieve data


public class Communicator
{
Socket ServerSocket;
Queue MessageQeue;
AutoResetEvent QeueSetEvent;
Thread CommThread;

IAsyncResult result;
public AsyncCallback pfnCallBack;
public StringBuilder rcvXMLRPCMsg = new StringBuilder();

public void InitializeComm()
{
this.MessageQeue = new Queue();
this.QeueSetEvent = new AutoResetEvent(false);

this.CommThread = new Thread(new ThreadStart(StartMaint));
CommThread.Start();
}
public void SendMaintenanceData(object objMessage)
{
this.MessageQeue.Enqueue(objMessage);
this.QeueSetEvent.Set();

}
private Socket ConnectWithServer()
{
SetServerSocket(null);
bool bCOnnectionMade = false;
IPAddress ipAddress = IPAddress.Parse("192.168.1.1");
IPEndPoint remoteEp = new IPEndPoint(ipAddress, 5000);
Socket tempSocket = new Socket(remoteEp.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

while(!bCOnnectionMade)
{
tempSocket.Connect(remoteEp);
if(tempSocket.Connected)
{
bCOnnectionMade = true;
SetServerSocket(tempSocket);
WaitForData();
}
else
{
SetServerSocket(null);
Thread.Sleep(1000);
continue;
}
}
return tempSocket;
}

private void SendDataToServer(string data)
{
Byte[] bytesSent = Encoding.ASCII.GetBytes(data);
Socket ServerSocket = GetServerSocket();
if (ServerSocket == null)
{
ServerSocket = ConnectWithServer();
SendDataToServer(data);
}
else
{
int nErrorCode = ServerSocket.Send(bytesSent, bytesSent.Length, 0);
}
}
private void StartMaint()
{
while(true)
{
this.QeueSetEvent.WaitOne();

while(MessageQeue.Count > 0)
{
data = (string) MessageQeue.Dequeue();
SendDataToServer(data);
}
}
}
public void OnDataReceived(IAsyncResult asyn)
{
SocketPacket theSockId = (SocketPacket)asyn.AsyncState ;
int iRx = theSockId.thisSocket.EndReceive (asyn);
char[] chars = new char[iRx + 1];
if(iRx > 0)
{
System.Text.Decoder d = System.Text.Encoding.ASCII.GetDecoder();
int charLen = d.GetChars(theSockId.dataBuffer, 0, iRx, chars, 0);
System.String szData = new System.String(chars);
rcvXMLRPCMsg.Append(szData);
}
WaitForData();
}
private void WaitForData()
{
if ( pfnCallBack == null )
{
pfnCallBack = new AsyncCallback (OnDataReceived);
}
SocketPacket theSocPkt = new SocketPacket ();
theSocPkt.thisSocket = GetServerSocket();
// Start listening to the data asynchronously
this.result = GetServerSocket().BeginReceive (theSocPkt.dataBuffer,
0, theSocPkt.dataBuffer.Length,
SocketFlags.None,
pfnCallBack,
theSocPkt);
}
public class SocketPacket
{
public System.Net.Sockets.Socket thisSocket;
public byte[] dataBuffer = new byte[2048];
}

private Socket GetServerSocket()
{
return this.ServerSocket;
}
private void SetServerSocket(Socket Sock)
{
this.ServerSocket = Sock;
}
private void ReinitializeSocket()
{
ServerSocket.Close();
SetServerSocket(null);
}

for more information visit ekutta . com
Thanks
Satheesh
Reply With Quote