Solved

Windows Service with UDP

Posted on 2016-11-07
2
60 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

735 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