本文整理汇总了C#中UdpClient类的典型用法代码示例。如果您正苦于以下问题:C# UdpClient类的具体用法?C# UdpClient怎么用?C# UdpClient使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
UdpClient类属于命名空间,在下文中一共展示了UdpClient类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ReceiveData
// receive thread
private void ReceiveData()
{
client = new UdpClient();
IPEndPoint localEp = new IPEndPoint(IPAddress.Any, port);
client.Client.Bind(localEp);
client.JoinMulticastGroup(IPAddress.Parse(MULTICAST_ADDR));
while (true)
{
try
{
byte[] data = client.Receive(ref localEp);
string text = Encoding.UTF8.GetString(data);
string[] message = text.Split(',');
Vector3 result = new Vector3(float.Parse(message[0]), float.Parse(message[1]), float.Parse(message[2]));
print(">> " + result);
lastReceivedUDPPacket = result;
}
catch (Exception err)
{
print(err.ToString());
}
}
}
示例2: ReceiveData
private void ReceiveData(){
client = new UdpClient(port); //Binds udp client to random port
while (true)
{
try
{
IPEndPoint IPEndPoint = new IPEndPoint(IPAddress.Any, 0);
byte[] data = client.Receive(ref IPEndPoint);
string text = Encoding.UTF8.GetString(data);
if(log){
Debug.Log(text);
}
lock(receivedUDPPackets){
receivedUDPPackets.Add(text);
}
}
catch (Exception err)
{
print(err.ToString());
}
}
}
示例3: Listen
private void Listen()
{
System.Random myRandom = new System.Random();
udpClient = new UdpClient(7272, AddressFamily.InterNetwork);
var endPoint = default(IPEndPoint);
byte[] bytes;
while (m_shouldRun)
{
try
{
bytes = udpClient.Receive(ref endPoint);
if (bytes == null || bytes.Length == 0)
break;
int offset = 0;
lock (m_threadLock)
{
m_head = FromOculusToUnity(Vector3FromBytes(bytes, ref offset));
m_rHand = FromOculusToUnity(Vector3FromBytes(bytes, ref offset));
//m_rHandRotation = Vector4FromBytes(bytes, ref offset);
m_lHand = FromOculusToUnity(Vector3FromBytes(bytes, ref offset));
//m_lHandRotation = Vector4FromBytes(bytes, ref offset);
m_rightClosed = BoolFromBytes(bytes, ref offset);
}
} catch (ThreadInterruptedException)
{
// Empty on purpose
}
}
}
示例4: init
public void init()
{
IP = "192.168.15.11";
port = 8051;
remoteEndPoint = new IPEndPoint(IPAddress.Parse(IP), port);
client = new UdpClient();
}
示例5: exportData
private void exportData(ref UdpClient client, ref IPEndPoint anyIP)
{
byte[] data;
string text;
text = "SOT"; // start of table
text = text + System.Environment.NewLine;
data = System.Text.Encoding.ASCII.GetBytes(text);
client.Send(data, data.Length, anyIP);
foreach (var ptr in timeGraphScript.dateTime_val_dic) {
text = ptr.Key.ToString("yyyy/MM/dd HH:mm:ss");
text = text + ",";
text = text + ptr.Value.ToString();
text = text + System.Environment.NewLine;
data = System.Text.Encoding.ASCII.GetBytes(text);
client.Send(data, data.Length, anyIP);
}
text = "EOT"; // end of table
text = text + System.Environment.NewLine;
data = System.Text.Encoding.ASCII.GetBytes(text);
client.Send(data, data.Length, anyIP);
}
示例6: procComm
void procComm()
{
port = getPort();
string ipadr = getIpadr ();
client = new UdpClient ();
// send
string sendstr = IFmsg.text + System.Environment.NewLine;
byte[] data = ASCIIEncoding.ASCII.GetBytes (sendstr);
client.Send (data, data.Length, ipadr, port);
// receive
client.Client.ReceiveTimeout = 2000; // msec
IPEndPoint remoteIP = new IPEndPoint(IPAddress.Any, 0);
lastRcvd = "";
try {
data = client.Receive (ref remoteIP);
if (data.Length > 0) {
string text = Encoding.ASCII.GetString (data);
lastRcvd = text;
}
} catch (Exception err) {
}
client.Close ();
}
示例7: LoadClient
public void LoadClient()
{
client = new UdpClient(System.Convert.ToInt32(port));
receivePoint = new IPEndPoint(IPAddress.Parse(ip),System.Convert.ToInt32(port));
Thread startClient = new Thread(new ThreadStart(start_client));
startClient.Start();
}
示例8: Start
//private CIPC_CS_Unity.CLIENT.CLIENT CIPCclient;
//private Thread thred;
//private FPSAdjuster.FPSAdjuster fps;
// Use this for initialization
void Start()
{
if (this.renderTexture == null )
{
return;
}
this.client = new UdpClient();
this.sendtexture = new Texture2D(this.renderTexture.width, this.renderTexture.height);
if (this.portNumber == 0)
{
this.portNumber = 15000;
}
//if (this.serverPort == 0)
//{
// this.serverPort = 50000;
//}
if (this.IPAdress == "")
{
this.IPAdress = "127.0.0.1";
}
//this.CIPCclient = new CIPC_CS_Unity.CLIENT.CLIENT(this.portNumber, this.IPAdress, this.serverPort);
//this.CIPCclient.Setup(CIPC_CS_Unity.CLIENT.MODE.Sender);
//this.fps = new FPSAdjuster.FPSAdjuster();
//this.fps.Fps = 30;
//this.fps.Start();
//this.thred = new Thread (new ThreadStart(this.sendimage));
//this.thred.Start();
}
示例9: ConnectAsync_StringHost_Success
public async Task ConnectAsync_StringHost_Success()
{
using (var c = new UdpClient())
{
await c.Client.ConnectAsync("114.114.114.114", 53);
}
}
示例10: init
// init
public void init(string targetIP, string name)
{
playerName = name;
GameObject.FindGameObjectWithTag("Game").GetComponent<Canvas>().enabled = true;
GameObject.FindGameObjectWithTag("GUI").GetComponent<Canvas>().enabled = false;
print("UDPSend.init()");
port = 8051;
IPAddress[] addrs = Dns.GetHostEntry(Dns.GetHostName()).AddressList;
foreach (var ip in addrs)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
playerIP = ip.ToString();
}
}
remoteEndPoint = new IPEndPoint(IPAddress.Parse(targetIP), port);
client = new UdpClient();
connected = true;
// status
print("Sending to " + targetIP + " : " + port);
print("Player IP is " + playerIP);
}
示例11: Main
public static void Main()
{
byte[] data = new byte[1024];
string input, stringData;
UdpClient udpClient = new UdpClient("127.0.0.1", 9999);
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
string welcome = "Hello";
data = Encoding.ASCII.GetBytes(welcome);
udpClient.Send(data, data.Length);
data = udpClient.Receive(ref sender);
Console.WriteLine("Message received from {0}:", sender.ToString());
stringData = Encoding.ASCII.GetString(data, 0, data.Length);
Console.WriteLine(stringData);
while(true)
{
input = Console.ReadLine();
udpClient.Send(Encoding.ASCII.GetBytes(input), input.Length);
data = udpClient.Receive(ref sender);
stringData = Encoding.ASCII.GetString(data, 0, data.Length);
Console.WriteLine(stringData);
}
udpClient.Close();
}
示例12: ReceiveData
// receive thread
private void ReceiveData() {
client = new UdpClient(port);
while (true) {
try {
// Bytes empfangen.
IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 0);
byte[] data = client.Receive(ref anyIP);
// Bytes mit der UTF8-Kodierung in das Textformat kodieren.
string text = Encoding.UTF8.GetString(data);
// Den abgerufenen Text anzeigen.
print(">> " + text);
// latest UDPpacket
lastReceivedUDPPacket=text;
// ....
allReceivedUDPPackets=allReceivedUDPPackets+text;
}
catch (Exception err) {
print(err.ToString());
}
}
}
示例13: ConnectAsync_IPAddressHost_Success
public async Task ConnectAsync_IPAddressHost_Success()
{
using (var c = new UdpClient())
{
await c.Client.ConnectAsync(IPAddress.Parse("114.114.114.114"), 53);
}
}
示例14: Program
//public Program()
//{
// socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
// localEP = new IPEndPoint(IPAddress.Any,port);
// socket.Bind(localEP);
// ConnectionEstablished = false;
//}
public Program()
{
localEP = new IPEndPoint(IPAddress.Any, port);
server = new UdpClient(localEP);
//IPEndPoint
//client.Send()
}
示例15: ReceiveData
//受信スレッド
private void ReceiveData()
{
//udpクライアント設定
udpClient = new UdpClient(port);
while(threadAvairable)
{
try
{
if(udpClient.Available > 0)
{
IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, 0);
byte[] data = udpClient.Receive(ref ipEndPoint);
recievedDataStr = Encoding.UTF8.GetString(data);
isDataRecieved = true;
}
}
catch(Exception err)
{
Debug.LogError(err.Message);
DebugConsole.LogError(err.Message);
break;
}
}
}