Skip to content

Instantly share code, notes, and snippets.

@yubeneko
Created October 30, 2019 17:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yubeneko/b980aaf6f23dc40f4aaf710600bd19e6 to your computer and use it in GitHub Desktop.
Save yubeneko/b980aaf6f23dc40f4aaf710600bd19e6 to your computer and use it in GitHub Desktop.
Udp通信をするためのスクリプト。
using UnityEngine;
using System.Net.Sockets;
public class UdpSender
{
string _remoteHost = "";
int _remotePort = 60000;
UdpClient _udpClient;
public UdpSender(string remoteHost, int remotePort)
{
_remoteHost = remoteHost;
_remotePort = remotePort;
_udpClient = new UdpClient();
}
public void SendData (string data)
{
byte[] sendBytes = System.Text.Encoding.ASCII.GetBytes(data);
try
{
_udpClient.Send(sendBytes, sendBytes.Length, _remoteHost, _remotePort);
}
catch (SocketException se)
{
Debug.LogError(se.ToString());
Debug.LogError($"Error Code : {se.ErrorCode}");
}
}
public void UdpClientClose()
{
_udpClient.Close();
Debug.Log("udp was closed.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment