Hi,
We have developed a socket program as below, The program works fine, except that it sometimes gives me jumbled messages or same message gets repeated twice.
The whole code is produced below.
I will let you know what exactly are we doing.
This a socket program for tracking vehicle.
The vehicle sends the signal through gprs+gsm mode, to our port.
We retrieve those messages using the above socket program.
Most of the messages come with GPRMC .
such as
$GPRMC,040302.663,A,3939.7,N,10506.6,W,0.27,358.86,200804,,*1A
Kindly help and let me know where am I going wrong
Regards
cmrhema
namespace SocketServer
{
class Program
{
public AsyncCallback pfnWorkerCallBack;private Socket m_mainSocket;
public string IPPort = "7030";
public byte[] m_byBuff = new byte[5000];
public int flag2 = 0;
public string queuePath;
private string strDateFormat;
private string strLocation;
public byte[] inValue = new byte[] { 1, 0, 0, 0, 0, 0, 5, 0, 1, 0, 0, 0 }; //1 -Minutes // 0,5 -- 6th parameter --seconds, 7th parameter--->Minutes
public byte[] outvalue = new byte[10];public Program()
{
this.queuePath = ".\\private$\\AMI";
this.strDateFormat = null;
this.strLocation = null;
StartListen();
}
public void StartListen()
{
try
{
string portStr = this.IPPort;
int port = System.Convert.ToInt32(portStr);
// Create the listening socket...
m_mainSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);IPAddress ip;
string ipstr = "192.168.0.20";
ip=IPAddress.Parse(ipstr);
IPEndPoint ipLocal = new IPEndPoint(ip, port);
//// Bind to local IP Address...
m_mainSocket.Bind(ipLocal);
//// Start listening...
m_mainSocket.Listen(100);
// Create the call back for any client connections...
while (true)
{
m_mainSocket.BeginAccept(new AsyncCallback(OnClientConnect), null);Thread.Sleep(2000);
}
}
catch (Exception e1)
{
WriteErrorLogMessage(e1.Message);
}
}
public void OnClientConnect(IAsyncResult asyn)
{
try
{
Socket msocket;
msocket = m_mainSocket.EndAccept(asyn);
msocket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.KeepAlive, 1);msocket.IOControl(IOControlCode.KeepAliveValues, inValue, outvalue);
WaitForData(msocket);
//BY COMMENTING THE BELOW LINE IT WILL NOT ACCEPT THE NEW CONNECTIONS
m_mainSocket.BeginAccept(new AsyncCallback(OnClientConnect), null);
}
catch (Exception e1)
{
WriteErrorLogMessage(e1.Message);
}
}
public void WaitForData(System.Net.Sockets.Socket soc)
{
try
{
if (pfnWorkerCallBack == null)
{
pfnWorkerCallBack = new AsyncCallback(OnDataReceived);
}
soc.BeginReceive(m_byBuff, 0, m_byBuff.Length, SocketFlags.None, pfnWorkerCallBack, soc);
//BY CLEARING BUFFER NO DATA EVER GETS RECEIVED, EXCEPTION WILLL BE TRHOWN IN "ONDATARECEIVED" METHOD
//m_byBuff = null;
}
catch (Exception e1)
{
WriteErrorLogMessage(e1.Message);
}
}
public void OnDataReceived(IAsyncResult asyn)
{
try
{
Socket sock = (Socket)asyn.AsyncState;
int nBytesRec = sock.EndReceive(asyn);string sReceived = System.Text.Encoding.ASCII.GetString(m_byBuff, 0, nBytesRec);
WriteLogMessage(sReceived);
//By MAKING THE BUFFER NULL OVER HERE, THE FIRST DATA GETS CAPTURED AND WHEN IT MOVES AGAIN TO SOC.BEGINRECEIVE, THE BUFFER BECOMES NULL
//m_byBuff = null;
this.ToMSMQ(sReceived);
Console.Write(sReceived);
WaitForData(sock);
}
catch (SocketException e1)
{
Socket sock = (Socket)asyn.AsyncState;
sock.Close();
WriteErrorLogMessage(e1.Message);
}
catch (Exception e1)
{
WriteErrorLogMessage(e1.Message);
}
}
public void ToMSMQ(string msg)
{
//some process
}
static void Main(string[] args)
{
Program p = new Program();
}
}
}