Link to home
Start Free TrialLog in
Avatar of BRNIIT
BRNIIT

asked on

Windows service listening on udp port

I am trying to write a Windows service in C# that listens on a udp port, and starts a process depending on the message it receives.

Every time I start the service I get the error message:

A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied

I have tried to set up the udp socket in a number of different ways, but I cannot get past this error.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Net;
using System.Net.Mail;
using System.Net.Sockets;
using System.Threading;
using System.IO;
 
namespace MyService
{
    public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();
        }
 
        protected override void OnStart(string[] args)
        {
            Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            int udpPort = 11000;
            IPEndPoint iep = new IPEndPoint(IPAddress.Any, udpPort);
            byte[] receiveBytes = new byte[15];
            try
            {
                sock.Bind(iep);
                IPEndPoint remoteEndpoint = new IPEndPoint(IPAddress.Any, 0);
                EndPoint ep = (EndPoint)remoteEndpoint;
                sock.BeginReceive(receiveBytes, 0, receiveBytes.Length, SocketFlags.None, new AsyncCallback(OnReceive), sock);
            }
            catch (SocketException ex)
            {
            }
        }
 
        protected override void OnStop()
        {
            sock.Close();
        }
 
        void OnReceive(IAsyncResult ar)
        {
            try
            {
                IPEndPoint ipeSender = new IPEndPoint(IPAddress.Any, 0);
                EndPoint epSender = (EndPoint)ipeSender;
                sock.EndReceiveFrom(ar, ref epSender);
                ASCIIEncoding enc = new ASCIIEncoding();
                string receiveString = enc.GetString(receiveBytes);
            }
            catch (Exception ex)
            {
            }
        }
    }
}

Open in new window

Avatar of Anurag Thakur
Anurag Thakur
Flag of India image

the code posted is not complete
you cannot debug the service easily so its always better to write your code in a class and then call appropriate methods from OnStart and OnStop methods

anyways.... the Socket sock object will not be accessible from OnStop and OnReceive methods so make it a class level object
i could not find receivebytes methods so i had to comment that line and when i did these changes the code did not give me any errors

-- i was not able to test as i dont know how to send messages to UDP port.
ASKER CERTIFIED SOLUTION
Avatar of BRNIIT
BRNIIT

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial