Home All Groups Group Topic Archive Search About

Asynchronous UDP Server not working..help pls????

Author
26 Mar 2005 7:19 AM
Chakkaradeep
Hi all,

i tried creating a UDP Asynchronous Server.....but am getting errors and am
not able to figure out why!!!.........

here is the code...........

*****************************code starts ********************
m_mainSocket = new Socket(AddressFamily.InterNetwork,
                    SocketType.Dgram,
                    ProtocolType.Udp);

IPHostEntry localHostEntry;
localHostEntry = Dns.GetHostByName(Dns.GetHostName());
IPEndPoint ipLocal = new IPEndPoint(localHostEntry.AddressList[0], port);

// Bind to local IP Address...
m_mainSocket.Bind( ipLocal );
// Start listening...
m_mainSocket.Listen(4);
// Create the call back for any client connections...
m_mainSocket.BeginAccept(new AsyncCallback (OnClientConnect), null);
*****************************code ends here********************

The error message is...

ATTEMPTED OPERATION IS NOT SUPPORTED FOR THE TYPE OF OBJECT REFERENCED

what is this error regarding?..

i would be happy if someone could help me around...

thanks in advance...

with regards,
C.C.Chakkaradeep

Author
26 Mar 2005 9:14 AM
Joerg Jooss
Chakkaradeep wrote:

Show quote
> Hi all,
>
> i tried creating a UDP Asynchronous Server.....but am getting errors
> and am not able to figure out why!!!.........
>
> here is the code...........
>
> *****************************code starts ********************
> m_mainSocket = new Socket(AddressFamily.InterNetwork,
>                     SocketType.Dgram,
>                     ProtocolType.Udp);
>
> IPHostEntry localHostEntry;
> localHostEntry = Dns.GetHostByName(Dns.GetHostName());
> IPEndPoint ipLocal = new IPEndPoint(localHostEntry.AddressList[0],
> port);
>
> // Bind to local IP Address...
> m_mainSocket.Bind( ipLocal );
> // Start listening...
> m_mainSocket.Listen(4);
> // Create the call back for any client connections...
> m_mainSocket.BeginAccept(new AsyncCallback (OnClientConnect), null);
> *****************************code ends here********************
>
> The error message is...
>
> ATTEMPTED OPERATION IS NOT SUPPORTED FOR THE TYPE OF OBJECT REFERENCED
>
> what is this error regarding?..
>
> i would be happy if someone could help me around...

Accept() and Listen() are intended for use with TCP server sockets.
You're using the wrong API ;-)

For UDP applications, use ReceiveFrom() or BeginReceiveFrom().

Cheers,
--
http://www.joergjooss.de
mailto:news-re***@joergjooss.de
Author
26 Mar 2005 1:59 PM
Chakkaradeep
Hi,

thanks for the reply..am a newbie in this stuff....

now i have to tell EndReceiveFrom( ) to receive the data after being
BeginReceive( )...will i get the remote end's ip address so that i can use it
in SendTo( ) function??

with regards,
C.C.Chakkaradeep

Show quote
"Joerg Jooss" wrote:

> Chakkaradeep wrote:
>
> > Hi all,
> >
> > i tried creating a UDP Asynchronous Server.....but am getting errors
> > and am not able to figure out why!!!.........
> >
> > here is the code...........
> >
> > *****************************code starts ********************
> > m_mainSocket = new Socket(AddressFamily.InterNetwork,
> >                     SocketType.Dgram,
> >                     ProtocolType.Udp);
> >
> > IPHostEntry localHostEntry;
> > localHostEntry = Dns.GetHostByName(Dns.GetHostName());
> > IPEndPoint ipLocal = new IPEndPoint(localHostEntry.AddressList[0],
> > port);
> >
> > // Bind to local IP Address...
> > m_mainSocket.Bind( ipLocal );
> > // Start listening...
> > m_mainSocket.Listen(4);
> > // Create the call back for any client connections...
> > m_mainSocket.BeginAccept(new AsyncCallback (OnClientConnect), null);
> > *****************************code ends here********************
> >
> > The error message is...
> >
> > ATTEMPTED OPERATION IS NOT SUPPORTED FOR THE TYPE OF OBJECT REFERENCED
> >
> > what is this error regarding?..
> >
> > i would be happy if someone could help me around...
>
> Accept() and Listen() are intended for use with TCP server sockets.
> You're using the wrong API ;-)
>
> For UDP applications, use ReceiveFrom() or BeginReceiveFrom().
>
> Cheers,
> --
> http://www.joergjooss.de
> mailto:news-re***@joergjooss.de
>
Author
26 Mar 2005 7:36 PM
Joerg Jooss
Chakkaradeep wrote:

> Hi,
>
> thanks for the reply..am a newbie in this stuff....
>
> now i have to tell EndReceiveFrom( ) to receive the data after being
> BeginReceive( )...will i get the remote end's ip address so that i
> can use it in SendTo( ) function??

Sure:

public IAsyncResult BeginReceiveFrom(
   byte[] buffer,
   int offset,
   int size,
   SocketFlags socketFlags,
   ref EndPoint remoteEP,
   AsyncCallback callback,
   object state
);

Note the EndPoint ref parameter. This will hold a reference to the
remote EndPoint that sends datagrams to your server socket.

Cheers,
--
http://www.joergjooss.de
mailto:news-re***@joergjooss.de

AddThis Social Bookmark Button