Solved

Windows Service with UDP

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

785 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