Solved

Windows service listening on udp port

Posted on 2008-10-14
2
5,054 Views
Last Modified: 2013-12-17
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

0
Comment
Question by:BRNIIT
2 Comments
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 22722524
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.
0
 

Accepted Solution

by:
BRNIIT earned 0 total points
ID: 22727731
It seems that I am not able to do this with Sockets for some reason.  I tried it using UdpClient instead, and it works just fine.  So I will withdraw this question.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Viewers will learn how to properly install and use Secure Shell (SSH) to work on projects or homework remotely. Download Secure Shell: Follow basic installation instructions: Open Secure Shell and use "Quick Connect" to enter credentials includi…
After creating this article (http://www.experts-exchange.com/articles/23699/Setup-Mikrotik-routers-with-OSPF.html), I decided to make a video (no audio) to show you how to configure the routers and run some trace routes and pings between the 7 sites…

911 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

17 Experts available now in Live!

Get 1:1 Help Now