1. 程式人生 > >unity3d:最簡單的伺服器,把收到訊息發回客戶端

unity3d:最簡單的伺服器,把收到訊息發回客戶端

using UnityEngine;  
using System.Collections;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System;
using System.Text;  
using UnityEngine.UI;

public class Server: MonoBehaviour {  

	private static byte[] result = new byte[1024];  
	static Socket serverSocket;
    Thread myThread;

    void Start()  
	{  
		//伺服器IP地址  
		IPAddress ip = IPAddress.Parse(GameConst.IP);  
		serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);  
		serverSocket.Bind(new IPEndPoint(ip, GameConst.Port));  //繫結IP地址:埠  
		serverSocket.Listen(10);    //設定最多10個排隊連線請求  
		//通過Clientsoket傳送資料  
		myThread = new Thread(ListenClientConnect);  
		myThread.Start();  
		Console.ReadLine();  
	}  

	/// <summary>  
	/// 監聽客戶端連線  
	/// </summary>  
	private void ListenClientConnect()  
	{  
		while (true)  
		{ 
			Socket clientSocket = serverSocket.Accept();  
			Thread receiveThread = new Thread(ReceiveMessage);  
			receiveThread.Start(clientSocket); 
			//Debug.Log(clientSocket.RemoteEndPoint.ToString());
		}  
	}  

	/// <summary>  
	/// 接收訊息
	/// </summary>  
	/// <param name="clientSocket"></param>  
	private static void ReceiveMessage(object clientSocket)  
	{  
		Socket myClientSocket = (Socket)clientSocket;  
		while (true)  
		{  
			try  
			{  
				int receiveNumber = myClientSocket.Receive(result);
                if (receiveNumber > 0)
                {
                    myClientSocket.Send(result, receiveNumber,0);
                }
			}  
			catch(Exception ex)  
			{  
				//Console.WriteLine(ex.Message);  
				myClientSocket.Shutdown(SocketShutdown.Both);  
				myClientSocket.Close();  
				break;  
			}  
		}  
	}

    void OnApplicationQuit()
    {

        if (myThread != null)
        {
            myThread.Abort();
            myThread = null;
        }
    }


}