Search Unity

UDP help

Discussion in 'Multiplayer' started by elpeedros, Nov 2, 2015.

  1. elpeedros

    elpeedros

    Joined:
    Jun 6, 2013
    Posts:
    14
    Hello, the project I'm working on requires hooking into a UDP socket. I'm able to send data successfully, but I can't receive anything on the same port. Can you send and receive data on the same address and port over UDP?
     
    Last edited: Nov 3, 2015
  2. Brent_Farris

    Brent_Farris

    Joined:
    Jul 13, 2012
    Posts:
    881
    Hello, I have made a post previously about writing your own server without using things like Forge (though I'm pro Forge Networking as the architect). I believe that in that post I do go over some of the things you need to watch out for when doing UDP packets.

    So basically with a UDP socket you can receive data on the port you have bound to. When you send out a message you will need to provide the endpoint for the packet. UDP works differently from TCP. TCP is a stream where you make a connection to another machine and then your transaction is through a stream. With UDP, it is connectionless. That means you never make a connection to the machine on the other end. What you do is you send a message to an address/port and hope for the best. There is no conformation of send and there is no guarantee that it will get there. Did you provide the correct address/port when you sent the packet? I personally haven't sent out a packet to the same exact machine on the bound port using that socket but I believe it should work fine. Are you using the loopback address (127.0.0.1)? If so, maybe you should try using the local actual network address as the operating system may be ignoring a send to the loopback on the bound port.
     
  3. elpeedros

    elpeedros

    Joined:
    Jun 6, 2013
    Posts:
    14
    Thanks for your response. The project I am working requires UDP. I know the technicalities involved as well. I am able to receive data if I use localhost. However, this needs to work on a network and it doesn't work. Again, I am able to send messages successfully just not receive.

    Code (CSharp):
    1. using UnityEngine;
    2. using System;
    3. using System.Text;
    4. using System.Net;
    5. using System.Net.Sockets;
    6. using System.Threading;
    7.  
    8. public class UDPReceiver : MonoBehaviour {
    9.  
    10.     public int Port = 15000;
    11.  
    12.     private IPEndPoint endPoint;
    13.     private UdpClient client;
    14.     private Thread receiveThread;
    15.     private bool isOn;
    16.  
    17.     private void Start() {
    18.         Init(Port);
    19.     }
    20.  
    21.     private void OnApplicationQuit() {
    22.         Kill();
    23.     }
    24.  
    25.     public void Init(int _port) {
    26.         Port = _port;
    27.         isOn = true;
    28.  
    29.         client = new UdpClient(0);
    30.         client.Client.Blocking = false;
    31.         endPoint = new IPEndPoint(IPAddress.Any, Port);
    32.  
    33.         receiveThread = new Thread(new ThreadStart(ReceiveData));
    34.         receiveThread.IsBackground = true;
    35.         receiveThread.Start();
    36.     }
    37.  
    38.     public void Kill() {
    39.         isOn = false;
    40.  
    41.         if (client != null) {
    42.             client.Close();
    43.             client = null;
    44.         }
    45.  
    46.         if (receiveThread != null) {
    47.             receiveThread.Abort();
    48.             receiveThread = null;
    49.         }
    50.     }
    51.  
    52.     private void ReceiveData() {
    53.         while (isOn) {
    54.             try {
    55.                 byte[] data = client.Receive(ref endPoint);
    56.                 string text = Encoding.UTF8.GetString(data);
    57.                 Debug.Log("ReceiveData: data=" + text);
    58.  
    59.             } catch (Exception err) {
    60.                 Debug.Log("ReceiveData: nothing " + err);
    61.             }
    62.         }
    63.     }
    64. }