Skip to content

Instantly share code, notes, and snippets.

@j3ffgray
Created March 4, 2019 20:51
Show Gist options
  • Save j3ffgray/20307a93f06bbd46e67502256791dc95 to your computer and use it in GitHub Desktop.
Save j3ffgray/20307a93f06bbd46e67502256791dc95 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class UdpSrvrSample : MonoBehaviour {
public static void Main() {
byte[] data = new byte[1024];
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 9050);
UdpClient newsock = new UdpClient(ipep);
Debug.Log("Waiting for a client...");
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
data = newsock.Receive(ref sender);
Debug.Log("Message received from {0}:" + sender.ToString());
Debug.Log(Encoding.ASCII.GetString(data, 0, data.Length));
string welcome = "Welcome to my test server";
data = Encoding.ASCII.GetBytes(welcome);
newsock.Send(data, data.Length, sender);
while(true)
{
data = newsock.Receive(ref sender);
Debug.Log(Encoding.ASCII.GetString(data, 0, data.Length));
newsock.Send(data, data.Length, sender);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment