Home>

In assignments
I was worried about this time
My boss has created a source for UDP communication
Is this source receiving and transmitting at the same time?
I did not understand the meaning so much even if I looked it
It is said that it is decimal number though it must be just hexadecimal number.

using System.Collections.Generic;
using UnityEngine;
using System.Net;
using System.Net.Sockets;
using System.Threading;
public class UdpSocket: MonoBehaviour
{
    private string _MulticastAddr = "224.0.23.0";// multicast address
    private string _RemoteHost = "";// Sender address
    private int _SendPort = 3610;// send port
    private int _RecvPort = 3610;// receive port
    private UdpClient _UdpClient;// UDP
    private IPEndPoint _IpEndPoint;// IPEndPoint
    private Thread _RecvThread;// receive thread
    // connect
    public void Connect ()
    {
        IPAddress grpAddr = IPAddress.Parse (_MulticastAddr);
        if (_IpEndPoint! = null) _IpEndPoint = null;
        _IpEndPoint = new IPEndPoint (grpAddr, _RecvPort);
        // Join a multicast group
        Disconnect ();
        _UdpClient = new UdpClient (_RecvPort);
        _UdpClient.JoinMulticastGroup (grpAddr);
        // receive thread creation
        _RecvThread = new Thread (ReceiveMulticastThread);
        _RecvThread.Start ();
        // send node profile notification
        SendNodeProfile ();
    }
    // disconnect
    public void Disconnect ()
    {
        if (_RecvThread! = null)
        {
            _RecvThread.Abort ();
            _RecvThread = null;
        }
        if (_UdpClient! = null)
        {
            IPAddress grpAddr = IPAddress.Parse (_MulticastAddr);
            _UdpClient.DropMulticastGroup (grpAddr);
            _UdpClient.Close ();
            _UdpClient = null;
        }
    }
    // send node profile notification
    public void SendNodeProfile ()
    {
        byte [] pack = BuildNodeProfileInfo ();
        SendPacket (pack, _MulticastAddr);
    }
    // send
    public void SendPacket (byte [] packet, string host)
    {
        _UdpClient.Send (packet, packet.Length, host, _SendPort);
    }
    // receive thread
    public void ReceiveMulticastThread ()
    {
        byte [] packet;
        int i = 0;
        System.Text.StringBuilder s = new System.Text.StringBuilder ();
        packet = _UdpClient.Receive (ref _IpEndPoint);
        if (packet! = null)
        {
            // Received!s.Remove (0, s.Length);
            for (i = 0;i<packet.Length;i ++)
            {
                s.Append (System.Convert.ToString (packet [i], 16) .PadLeft (2, '0'));
            }
            Debug.Log (s.ToString ());
        }
    }
    // Create node profile notification packet
    private byte [] BuildNodeProfileInfo ()
    {
        byte [] pack;
        pack = new byte [17];
        pack [0] = 0x10;// EHD1
        pack [1] = 0x81;// EHD2
        pack [2] = 0x00;//
        pack [3] = 0x01;// ID
        pack [4] = 0x0E;// sender "node profile class"
        pack [5] = 0xF0;// EOJ = 0x0E F0 01
        pack [6] = 0x01;//
        pack [7] = 0x0E;// destination "node profile class"
        pack [8] = 0xF0;// EOJ = 0x0E F0 01
        pack [9] = 0x01;//
        pack [10] = 0x73;// ESV
        pack [11] = 0x01;// OPC
        pack [12] = 0xD5;// EPC
        pack [13] = 0x04;// PDC
        pack [14] = 0x01;// EDT
        pack [15] = 0x05;//
        pack [16] = 0xFF;//
        pack [16] = 0x01;//
        return pack;
    }
}
Code


Code

I want to send UDP communication with
unity
There is no error
Not sent even when button is pressed
First, there is no reaction. Nothing happens
Probably something from the source won't work `` `Enter your language here
Code
Because the transmission code was delayed, it was unresponsive
I thought there was something missing.

using UnityEngine;
using System.Net.Sockets;
using System.Text;

public class UDPClient: MonoBehaviour {
public string host ="224.0.23.0" ;;
public int port = 3610;
private UdpClient client;

// Use this for initialization
public void Start () {
client = new UdpClient ();
client.Connect (host, port);
}

// Update is called once per frame

public void Update () {

}
void OnGUI ()
{
if (GUI.Button (new Rect (10, 10, 100, 40),"button"))
{
byte [] dgram = Encoding.UTF8.GetBytes ("hello!");
client.Send (dgram, dgram.Length);
}
}

void OnApplicationQuit ()
{
client.Close ();
}

}

  • Answer # 1

    Since the designated IP is a class D IP address, isn't it necessary to communicate by multicast?

    Sender

    IPAddress multicastIpAddress = IPAddress.Parse ("224.0.23.0");
    client = new UdpClient ();
    client.JoinMulticastGroup (multicastIpAddress);
    IPEndPoint remoteEp = new IPEndPoint (multicastIpAddress, 3610);
    client.Send (dgram, dgram.Length, remoteEp);


    Receiver

    IPAddress multicastIpAddress = IPAddress.Parse ("224.0.23.0");
    udp = new UdpClient ();
    udp.JoinMulticastGroup (multicastIpAddress);
    IPEndPoint remoteEp = new IPEndPoint (IPAddress.Any, 3610);
    byte [] data = udp.Receive (ref remoteEp);


    So I think that both the sender and receiver must join the multicast group.
    (Since the source was written roughly, it has not been confirmed to work, and has not been compiled yet.)

    Maybe there is an IP address of class A to C like "192.168 ~" in addition to this "224.0.23.0". I think it is necessary.

  • Answer # 2

    UDP is a protocol that keeps sending, nothing happens just by sending it
    Let's make a receiver properly and check if it can be received.

    And if you can't receive anything, something is wrong and you will be looking for something wrong