7
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

unity > udp > echo server > client.Client.Blocking = false;

Last updated at Posted at 2015-07-22

UnityでUDPのclientを使った文字列の受信

#code.01 (OnGUIが実行されなくなる)
参考にしたのは
http://forum.unity3d.com/threads/simple-udp-implementation-send-read-via-mono-c.15900/
にて la1nさんの Apr 6, 2008の投稿の UDPReceive.cs

そこから必要最低限のもの以外色々削ぎ落とした。

NG.cs
using UnityEngine;
using System.Collections;

using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;

public class udpEchoServer : MonoBehaviour {
	Thread rcvThr;
	UdpClient client;
	public int port = 6000;

	public string lastRcvd;
	private bool stopThr = false;

	void Start () {
		init ();
	}

	void OnGUI() {
		Rect rectObj=new Rect(40,10,200,100);
		GUI.Box (rectObj, "rcvd: \n" + lastRcvd);

		if (GUI.Button (new Rect (10, 250, 100, 40), "Quit")) {
			Application.Quit ();
		} 
	}

	void init() {
		rcvThr = new Thread( new ThreadStart(FuncRcvData));
		rcvThr.Start ();
	}

	void OnApplicationQuit() {
		stopThr = true;
		rcvThr.Abort ();
	}

	private void FuncRcvData()
	{
		client = new UdpClient (port);
		client.Client.ReceiveTimeout = 300; // msec
		while (stopThr == false) {
			try {
				IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 0);
				byte[] data = client.Receive(ref anyIP);
				string text = Encoding.ASCII.GetString(data);
				lastRcvd = text;

				client.Send(data, data.Length, anyIP); // echo
			}
			catch (Exception err)
			{
//				print(err.ToString());
			}
		}
	}
}

受信した文字列をそのまま返信している。

問題としては、受信したあとの文字の表示更新はOnGUIで行っているが、これがマウスクリックしないと実行されないこと。

OnGUIあたりの動作はこれから勉強しないといけない。


UDP clientについては、こちらも参考になりそう。
http://dobon.net/vb/dotnet/internet/udpclient.html


#code.02 (OnGUI()の実行は正常)

code.01でOnGUI()が実行されなくなるのを修正したバージョン。
違いはclient.Client.Blocking = false;を入れたこと。

OK.cs
using UnityEngine;
using System.Collections;

using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;

public class udpEchoServer : MonoBehaviour {
	Thread rcvThr;
	UdpClient client;
	public int port = 6000;
	
	public string lastRcvd;
	private bool stopThr = false;
	
	void Start () {
		init ();
	}
	
	void OnGUI() {
		Rect rectObj=new Rect(40,10,200,100);
		GUI.Box (rectObj, "rcvd: \n" + lastRcvd);
		
		if (GUI.Button (new Rect (10, 250, 100, 40), "Quit")) {
			Application.Quit ();
		} 
	}
	
	void init() {
		Debug.Log ("init");
		rcvThr = new Thread( new ThreadStart(FuncRcvData));
		rcvThr.Start ();
	}
	
	void OnApplicationQuit() {
		stopThr = true;
		rcvThr.Abort ();
	}
	
	private void FuncRcvData()
	{
		client = new UdpClient (port);
		client.Client.ReceiveTimeout = 300; // msec
		client.Client.Blocking = false; //<---------------------------------
		while (stopThr == false) {
			try {
				IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 0);
				byte[] data = client.Receive(ref anyIP);
				string text = Encoding.ASCII.GetString(data);
				lastRcvd = text;

				if (lastRcvd.Length > 0) {
					client.Send(data, data.Length, anyIP); // echo
				}
			}
			catch (Exception err)
			{
				//              print(err.ToString());
			}
		}
	}
}

(追記 2015/09/03)

上記をもとにuGUIを使ったecho serverにしたものはgithubにあります。

7
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?