Socket ReceiveFrom Problem

N

Nuno Magalhaes

Does the function below returns an UDP packet, for example, from the
local machine? Why does it give me an error: "The best overloaded
method match for ReceiveFrom... has some invalid arguments". Why this
error?

How can I receive a packet from a specific adapter, for example, my
local machine? Is there a better way? This way won't work. The error is
in "ref ep"... how does receiveFrom work?

Thanks.

public byte[] ReceiveFrom(string ipAddress,int port)
{
IPEndPoint ep=new IPEndPoint(IPAddress.Parse(ipAddress),port);
byte[] buffer=new byte[1500];
int receivedBytes=socket.ReceiveFrom(buffer,ref ep);
byte[] packet=new byte[receivedBytes];
Array.Copy(buffer,0,packet,0,receivedBytes);
return packet;
}
 
P

Paul Henderson

Why does it give me an error: "The best overloaded
method match for ReceiveFrom... has some invalid arguments". Why this
error?

ReceiveFrom takes a ref to an EndPoint object; you're passing a ref to
an IPEndPoint, which can't be cast down to EndPoint implicitly as its
passed with ref. So, create another local, of type EndPoint, and set
this to (EndPoint)ep;, and pass it as the parameter, e.g.


IPEndPoint ep=new IPEndPoint(IPAddress.Parse(ipAddress),port);
EndPoint ep2 = (EndPoint)ep;
byte[] buffer=new byte[1500];
int receivedBytes=socket.ReceiveFrom(buffer,ref ep2);
 

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