471,350 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

Socket ReceiveFrom Problem

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;
}

Jan 17 '06 #1
1 4295
> 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);

Jan 17 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by flupke | last post: by
1 post views Thread by Chang | last post: by
4 posts views Thread by Sa¹o Zagoranski | last post: by
1 post views Thread by scott | last post: by
reply views Thread by Ron L | last post: by
2 posts views Thread by Rene Sørensen | last post: by
5 posts views Thread by natambu | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.