Socket ReceiveFrom Problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nuno Magalhaes

    #1

    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(str ing ipAddress,int port)
    {
    IPEndPoint ep=new IPEndPoint(IPAd dress.Parse(ipA ddress),port);
    byte[] buffer=new byte[1500];
    int receivedBytes=s ocket.ReceiveFr om(buffer,ref ep);
    byte[] packet=new byte[receivedBytes];
    Array.Copy(buff er,0,packet,0,r eceivedBytes);
    return packet;
    }

  • Paul Henderson

    #2
    Re: Socket ReceiveFrom Problem

    > Why does it give me an error: "The best overloaded[color=blue]
    > method match for ReceiveFrom... has some invalid arguments". Why this
    > error?[/color]

    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(IPAd dress.Parse(ipA ddress),port);
    EndPoint ep2 = (EndPoint)ep;
    byte[] buffer=new byte[1500];
    int receivedBytes=s ocket.ReceiveFr om(buffer,ref ep2);

    Comment

    Working...