473,549 Members | 2,935 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to restrict from getting duplicate or jumbled messages

375 Contributor
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.6 63,A,3939.7,N,1 0506.6,W,0.27,3 58.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 1216
Plater
7,872 Recognized Expert Expert
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 Contributor
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
3117
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 in "log in" and "user authentication, restrict access to page" features. But I find the after login I constantly get redirected from the restricted...
5
2331
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 contain a set of create, update, insert & delete statements (about 17500 lines including blank lines) that
15
2944
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 it. Simple database: I want to have a user enter Supply Orders (just for tracking purposes) by Item. The user may also enter a new item - "new"...
0
2412
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, but there is a problem using the Outlook.Items.Restrict method. The returned ItemsClass shows 10 unread messages (correct), but cycling through the...
12
3661
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. -- -- TOC entry 59 (OID 11462032) -- Name: order_to_do; Type: TABLE; Schema: public; Owner: www -- Data Pos: 0 --
21
6481
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 definition found in K&R 2nd.
7
1832
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 the GUI of the software tool to let the user know that a duplicate message was sent or received. Here is the code:
7
1853
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 getting multiple duplicate descriptions in the dropdown. like drawing drawing drawing ceramics ceramics. I can see the problem as I am passing a...
1
1300
by: ahmedahmed | last post by:
how to restrict duplicate values in msflexgridcontrol
0
7446
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7469
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7808
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6040
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5368
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5087
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3498
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3480
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
757
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.