Groups | Blog | Home
all groups > c# > march 2005 >

c# : Asynchronous UDP Server not working..help pls????


Chakkaradeep
3/25/2005 11:19:08 PM
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,
Joerg Jooss
3/26/2005 1:14:37 AM
[quoted text, click to view]

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
Chakkaradeep
3/26/2005 5:59:08 AM
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

[quoted text, click to view]
Joerg Jooss
3/26/2005 11:36:34 AM
[quoted text, click to view]

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
AddThis Social Bookmark Button