Created
October 30, 2019 17:29
-
-
Save yubeneko/b980aaf6f23dc40f4aaf710600bd19e6 to your computer and use it in GitHub Desktop.
Udp通信をするためのスクリプト。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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