Solved

Windows Service with UDP

Posted on 2016-11-07
2
56 Views
Last Modified: 2016-11-07
I need to create a Windows Service to receive connections UDP and send a command back to server.


I have this code

protected override void OnStart(string[] args)
{
  _mStarted = true;
  _mWorkReceberComando = new Thread(ReceberComando);
  _mWorkReceberComando.Name = "Recebimento Comando";
  _mWorkReceberComando.Start();
}

 private void ReceberComando()
    {
        _endPoint = new IPEndPoint(IPAddress.Any, _port);
        var udpClient = new UdpClient(_endPoint);
        while (_mStarted)
        {
            var remoteEp = new IPEndPoint(IPAddress.Any, 0);
            var data = udpClient.Receive(ref remoteEp);

            var receivedString = BitConverter.ToString(data);

            var comandoByte = new ObterComandocAck(receivedString);
            var ipEndereco = remoteEp.Address;

            udpClient.Connect(ipEndereco.ToString(), _port);
            udpClient.Send(comandoByte, comandoByte.Length);

            Console.WriteLine(receivedString); 
        }
    }

Open in new window


I'm receiving this erro:

An existing connection was forcibly closed by the remote host

HYRESULT -2147467259
0
Comment
Question by:berechit
2 Comments
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 500 total points
ID: 41877889
Did you verify you can connect to the port manually?

Before creating the service I would create  a console application that essentially does the same thing.
Step thru and find out where it's erroring.

Based on that error message the the client doesn't like the connection for some reason.  (Firewall?)
0
 

Author Comment

by:berechit
ID: 41878177
Hi Kyle,

Yes I did.

I'm need to connect to a GPS tracker and that need to open the connection first then I need to send the response in the same connection.

I've changed code to this:

private void ReceberComando()
    {
        _endPoint = new IPEndPoint(IPAddress.Any, _port);
        var udpClient = new UdpClient(_endPoint);
        while (_mStarted)
        {
            var remoteEp = new IPEndPoint(IPAddress.Any, 0);
            var data = udpClient.Receive(ref remoteEp);

            var receivedString = BitConverter.ToString(data);

            var comandoByte = new ObterComandocAck(receivedString);
            var ipEndereco = remoteEp.Address;

            udpClient.Connect(ipEndereco.ToString(), _port);
            udpClient.Send(comandoByte, comandoByte.Length,remoteEp  );

            Console.WriteLine(receivedString); 
        }
    }

Open in new window


Thanks for your help.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question