Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Socket.ReceiveFromAsync result with endpoint 0.0.0.0:0 (Unity 5.3.1f1 Personal)

Discussion in 'Multiplayer' started by Kirlim, Feb 7, 2016.

  1. Kirlim

    Kirlim

    Joined:
    Aug 6, 2012
    Posts:
    126
    I am importing a code that uses SocketAsyncEventArgs into my project, using UDP. The socket is of datagram type

    Code (csharp):
    1. m_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
    Sending a message with an SendToAsync call seems to be working properly, however, ReceiveFromAsync "returns" with the endpoint in the "receival" SocketAsyncEventArgs as the address 0.0.0.0:0.

    I am using a "server" socket and a "client" socket for testing, running one on Unity Editor and another from a build. Both are set with Api Compatibility Level to .net 2.0 (instead of the subset).Their listen addresses are:
    - Server => 0.0.0.0:60000
    - Client => 0.0.0.0:60001

    The client sends the message to the address 127.0.0.1:60000. Testing the same code on a Visual Studio project (non-unity related), the returned ReceiveFromAsync endpoint, when the server receives a message from the client, is 127.0.0.1:60001.

    I've searched around, and it was ambiguous if SocketAsyncEventArgs is compatible with Unity or not (i.e dscroggi post on http://forum.unity3d.com/threads/socket-receivefromasync-sender-endpoint-doesnt-work.224758/). Am I forgetting to set up something else? Or actually it really is incompatible and I should change to synchronous socket?
     
  2. Kirlim

    Kirlim

    Joined:
    Aug 6, 2012
    Posts:
    126
    Replying as a new post instead of an edit, since I believe this actually is an answer.

    I have updated to Unity 5.3.2f1, and the problem persisted. So I decided to do some testing on my code. Before calling ReceiveFromAsync, I set the SocketAsyncEvent endpoint to something nonsensical:

    Code (csharp):
    1. asyncEventArgs.RemoteEndPoint = (new IPEndPoint(IPAddress.Parse("255.254.253.252"), 60005)) as EndPoint;
    Then I started my server with listen 0.0.0.0:60000 and my client with listen on 0.0.0.0:60001. I sent a message to the server using the destination address 127.0.0.1:60000, and logged the return from ReceiveMessageAsync. I got this line:

    Incoming new message from 255.254.253.252:60005.
    UnityEngine.Debug:Log(Object)

    That being checked, it seems that Mono ReceiveMessageAsync just accepted the message (it ignores the RemoteEndPoint argument, which might be correct since I'm on UDP and datagrams - didn't compare with .net itself, but I expected that no messages would be received, since server isn't listening on port 60005), but failed to update the RemoteEndPoint property and kept it as the fictitious ip address I've set before starting the asynchronous operation.

    I short, don't use SocketAsyncEvents directly. Maybe it'll work if being used as a plugin? I won't test that...
     
  3. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    Did you report it as a bug, along with a test project code? Or is the behavior documented in the Manual? I can't tell anything because I never played around with Sockets yet.
     
  4. Kirlim

    Kirlim

    Joined:
    Aug 6, 2012
    Posts:
    126
    Didn't report. I did some digging and it looks like asynchronous sockets and SocketAsyncEventArgs have been somewhat "ambiguous" since long ago - SocketAsyncEventArgs itself is not fully implemented yet and some features do throw not implemented exceptions. It does look like synchronous sockets are fully operational, though. That, and the Begin/End pairs of methods, that might have some GC feeding (returning interfaces).
     
  5. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    Okay, then that means you will have to let the people who handles the Documentations know to "update" the info regarding it. Posting a Doc request post in the Documentations subforums is enough for the job.
     
  6. Kirlim

    Kirlim

    Joined:
    Aug 6, 2012
    Posts:
    126
    Is there any documentation on Unity about Sockets? I mean, its .Net / Mono stuff, didn't even know there were documentation on Unity about them. I thought unity's documentation had only about its own netcode and stream.
     
  7. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    Ask the Documentation people.