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

G

Guest

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
 
J

Joerg Jooss

Chakkaradeep said:
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,
 
G

Guest

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

Joerg Jooss said:
Chakkaradeep said:
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,
 
J

Joerg Jooss

Chakkaradeep said:
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,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top