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 -
-
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 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.
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 in "log in" and "user authentication,
restrict access to page" features. But I find the after login I constantly
get redirected from the restricted...
|
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
|
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"...
|
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...
|
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
--
| |
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.
|
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:
|
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...
|
by: ahmedahmed |
last post by:
how to restrict duplicate values in msflexgridcontrol
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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...
| |