Solved

Windows Service with UDP

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

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…
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

630 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