Solved

Windows Service with UDP

Posted on 2016-11-07
2
26 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 39

Accepted Solution

by:
Kyle Abrahams earned 500 total points
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now