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 -
-
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();
-
}
-
}
-
}
-
2 1196
The device in question makes a connection to you, sends some data then disconnects. This process is repeated.
Is that correct?
Using the AsyncConnection could allow connections to be processed out of order I think.
Why messages would be mangeled or otherwise, I don't know.
The device in question makes a connection to you, sends some data then disconnects. This process is repeated.
Is that correct?
Using the AsyncConnection could allow connections to be processed out of order I think.
Why messages would be mangeled or otherwise, I don't know.
Yes , the device sends data and then disconnects, but does not disconnect so immediately, if GPS is available it will not disconnect at all
But jumbled messages come in irrespective of its frequency of connection
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Paul |
last post by:
Hi all, at present I I've built a website which can be updated by admin and
users.
My problem, I've combined "log in" and "access levels" to restrict access to
certain pages, using the built...
|
by: Glenn |
last post by:
Hi!
Server info -
Win2K3 Server +SP1 with 1 GB Memory and 1.5 GB Virtual Memory
SQL Server 2000 Enterprise Edition + SP3 running on this.
Required result -
Create a SQL Script that will...
|
by: sara |
last post by:
Hi
I'm pretty new to Access here (using Access 2000), and appreciate the
help and instruction.
I gave myself 2.5 hours to research online and help and try to get this
one, and I am not getting...
|
by: Joseph Kormann |
last post by:
I'm trying to tie my application into the local running Outlook application
through the Interop.Outlook.DLL. I'm looking for new / unread messages in
the Inbox. No problem getting into the Inbox,...
|
by: Jared Carr |
last post by:
First I wish I knew how this was caused but here is our problem.
Sometime in the recent past we got a duplicate table. Here is the
result of a pg_dump with a pg_restore for just that table.
...
|
by: Niu Xiao |
last post by:
I see a lot of use in function declarations, such as
size_t fread(void* restrict ptr, size_t size, size_t nobj, FILE*
restrict fp);
but what does the keyword 'restrict' mean? there is no...
|
by: ucfcpegirl06 |
last post by:
Hello,
I have a dilemma. I am trying to flag duplicate messages received off
of a com port. I have a software tool that is supposed to detect dup
messages and flag and write the text "DUP" on...
|
by: fran7 |
last post by:
Hi, I am having trouble with this dropdown. With it I am passing an id to the next page but displaying a description in the dropdown. Trouble is it displays a description for every postid so I am...
|
by: ahmedahmed |
last post by:
how to restrict duplicate values in msflexgridcontrol
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |