473,386 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to restrict from getting duplicate or jumbled messages

375 256MB
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


Expand|Select|Wrap|Line Numbers
  1.  
  2. namespace SocketServer 
  3. {
  4.  
  5. class Program
  6.  
  7. {
  8. public AsyncCallback pfnWorkerCallBack;private Socket m_mainSocket; 
  9.  
  10. public string IPPort = "7030";
  11.  
  12. public byte[] m_byBuff = new byte[5000]; 
  13. public int flag2 = 0;
  14.  
  15. public string queuePath; 
  16. private string strDateFormat;
  17.  
  18. private string strLocation; 
  19. 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
  20.  
  21. public byte[] outvalue = new byte[10];public Program() 
  22. {
  23.  
  24. this.queuePath = ".\\private$\\AMI"; 
  25.  
  26. this.strDateFormat = null;
  27.  
  28. this.strLocation = null; 
  29. StartListen();
  30. }
  31.  
  32. public void StartListen() 
  33. {
  34. try
  35. {
  36. string portStr = this.IPPort; 
  37. int port = System.Convert.ToInt32(portStr);
  38.  
  39. // Create the listening socket...
  40. m_mainSocket = new Socket(AddressFamily.InterNetwork, 
  41. SocketType.Stream,
  42.  
  43. ProtocolType.Tcp);IPAddress ip; 
  44.  
  45. string ipstr = "192.168.0.20";
  46.  
  47. ip=IPAddress.Parse(ipstr); 
  48. IPEndPoint ipLocal = new IPEndPoint(ip, port);
  49.  
  50. //// Bind to local IP Address...
  51.  
  52. m_mainSocket.Bind(ipLocal);
  53.  
  54. //// Start listening...
  55.  
  56. m_mainSocket.Listen(100);
  57.  
  58. // Create the call back for any client connections...
  59.  
  60. while (true) 
  61. {
  62. m_mainSocket.BeginAccept(new AsyncCallback(OnClientConnect), null);Thread.Sleep(2000); 
  63. }
  64. }
  65. catch (Exception e1) 
  66. {
  67. WriteErrorLogMessage(e1.Message);
  68. }
  69. }
  70.  
  71. public void OnClientConnect(IAsyncResult asyn) 
  72. {
  73. try
  74. {
  75. Socket msocket; 
  76. msocket = m_mainSocket.EndAccept(asyn);
  77.  
  78. msocket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.KeepAlive, 1);msocket.IOControl(IOControlCode.KeepAliveValues, inValue, outvalue); 
  79.  
  80. WaitForData(msocket);
  81.  
  82. //BY COMMENTING THE BELOW LINE IT WILL NOT ACCEPT THE NEW CONNECTIONS
  83.  
  84. m_mainSocket.BeginAccept(new AsyncCallback(OnClientConnect), null); 
  85. }
  86. catch (Exception e1) 
  87. {
  88. WriteErrorLogMessage(e1.Message);
  89. }
  90. }
  91.  
  92. public void WaitForData(System.Net.Sockets.Socket soc) 
  93. {
  94.  
  95. try
  96. {
  97. if (pfnWorkerCallBack == null) 
  98. {
  99. pfnWorkerCallBack = new AsyncCallback(OnDataReceived); 
  100. }
  101.  
  102. soc.BeginReceive(m_byBuff, 0, m_byBuff.Length, SocketFlags.None, pfnWorkerCallBack, soc); 
  103. //BY CLEARING BUFFER NO DATA EVER GETS RECEIVED, EXCEPTION WILLL BE TRHOWN IN "ONDATARECEIVED" METHOD
  104.  
  105. //m_byBuff = null;
  106. }
  107. catch (Exception e1) 
  108. {
  109. WriteErrorLogMessage(e1.Message);
  110. }
  111. }
  112.  
  113. public void OnDataReceived(IAsyncResult asyn) 
  114. {
  115. try
  116. {
  117. Socket sock = (Socket)asyn.AsyncState; 
  118.  
  119. int nBytesRec = sock.EndReceive(asyn);string sReceived = System.Text.Encoding.ASCII.GetString(m_byBuff, 0, nBytesRec); 
  120. WriteLogMessage(sReceived);
  121.  
  122. //By MAKING THE BUFFER NULL OVER HERE, THE FIRST DATA GETS CAPTURED AND WHEN IT MOVES AGAIN TO SOC.BEGINRECEIVE, THE BUFFER BECOMES NULL
  123.  
  124. //m_byBuff = null;
  125.  
  126. this.ToMSMQ(sReceived);
  127.  
  128. Console.Write(sReceived); 
  129. WaitForData(sock);
  130. }
  131. catch (SocketException e1) 
  132. {
  133. Socket sock = (Socket)asyn.AsyncState; 
  134. sock.Close();
  135.  
  136. WriteErrorLogMessage(e1.Message);
  137. }
  138. catch (Exception e1) 
  139. {
  140. WriteErrorLogMessage(e1.Message);
  141. }
  142. }
  143.  
  144. public void ToMSMQ(string msg) 
  145. {
  146. //some process 
  147. }
  148.  
  149. static void Main(string[] args) 
  150. {
  151. Program p = new Program(); 
  152. }
  153. }
  154. }
  155.  
Jun 2 '08 #1
2 1196
Plater
7,872 Expert 4TB
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.
Jun 2 '08 #2
cmrhema
375 256MB
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
Jun 3 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
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...
5
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...
15
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...
0
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,...
12
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. ...
21
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...
7
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...
7
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...
1
by: ahmedahmed | last post by:
how to restrict duplicate values in msflexgridcontrol
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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
0
BarryA
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...
1
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...
0
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...
0
marktang
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,...
0
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...
0
Oralloy
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.