u3d udp服務器


using UnityEngine;
using System.Collections;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System;
using System.Threading;
public class udpServer :MonoBehaviour
{

    private Socket newsock;//定義一個socket變量
    IPEndPoint ip;//定義一個IP地址和端口號
    int recv;//定義一個接受值的變量
    byte[] data = new byte[1024];//定義一個二進制的數組用來獲取客戶端發過來的數據包
    string mydata;
    void Start()
    {
        //得到本機IP,設置TCP端口號         
        ip = new IPEndPoint(IPAddress.Any, 12345);//設置自身的IP和端口號,在這里IPAddress.Any是自動獲取本機IP
        newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);//實例化socket對象設置尋址方案為internetwork(IP版本的4存放),設置Soket的類型,為Dgram(支持數據報形式的數據),設置協議的類型,為UDP
        //綁定網絡地址
        newsock.Bind(ip);//綁定IP
        Debug.Log("This is a Server,host name is " + Dns.GetHostName());//輸出本地的名字
        Debug.Log("Waiting for a client");
        //BeginReceives();
        Thread test = new Thread(BeginListening);//定義一個子線程
        test.Start();//子線程開始    
    }

    void BeginListening()
    {
        IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);//實例化一個網絡端點,設置為IPAddress.Any為自動獲取跟我通訊的IP,0代表所有的地址都可以
        EndPoint Remote = (EndPoint)(sender);//實例化一個地址結束點來標識網絡路徑
      //  Debug.Log(Encoding.ASCII.GetString(data, 0, recv));//輸出二進制轉換為string類型用來測試
        while (true)
        {
            data = new byte[1024];//實例化data
            recv = newsock.ReceiveFrom(data, ref Remote);//將數據包接收到的數據放入緩存點,並存儲終節點
            //Debug.Log(Encoding.ASCII.GetString(data, 0, recv));
            mydata = Encoding.ASCII.GetString(data, 0, recv);
            Debug.Log(mydata);
            // newsock.SendTo(Encoding.ASCII.GetBytes(mydata),mydata.Length,SocketFlags.None,Remote);
        }
    }
    string input = "";
    void SendMessage(string message)
    {
        byte[] data = new byte[1024];
        Debug.Log("This is a client,host name is" + Dns.GetHostName());
        //IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);//實例化一個網絡端點,設置為IPAddress.Any為自動獲取跟我通訊的IP,0代表所有的地址都可以
        EndPoint Remote = (EndPoint)(ip);//實例化一個地址結束點來標識網絡路徑
        Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

        //string welcome = "你好";
        //data = Encoding.ASCII.GetBytes(welcome);
        ip = new IPEndPoint(IPAddress.Parse("192.168.1.120"), 12346);
       // server.SendTo(data, data.Length, SocketFlags.None, ip);
       // data = new byte[1024];
        server.SendTo(Encoding.ASCII.GetBytes(message), ip);
       // data = new byte[1024];
        Debug.Log("Stopping Client.");
        server.Close();
    }
    void OnGUI()
    {
        //input = GUILayout.TextField(input);
        input = GUI.TextField(new Rect(100, 50, 60, 30), input);
        if (GUI.Button(new Rect(100, 90, 60, 30), "Send"))
        {
            SendMessage(input);
        }
    }
    void OnApplicationQuit()
    {
        newsock.Close();
    }
}

  

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



public struct mystructs
{

    public int Age; //獲取結構里的
    public string name;
    public float Height;
    public mystructs(int myage, string myname, float myheight)
    {
        Age = myage;
        name = myname;
        Height = myheight;
    }
}
public class udpMystruct : MonoBehaviour
{
    int Age;
    string Name;//獲取
    float Height;
    private Socket newsock;//定義一個socket變量
    IPEndPoint ip;//定義一個IP地址和端口號
    int recv;//定義一個接受值的變量
    byte[] data = new byte[1024];//定義一個二進制的數組用來獲取客戶端發過來的數據包
    byte[] bytedate = new byte[1024];
    string mydata;
    void Start()
    {
        mystructs my;    //將結構初始化  付給另一個 
        Age = my.Age = 17;
        Name = my.name = "  haha  ";
        Height = my.Height = 180.50f;
        Debug.Log(Age + Name + Height);
        byte[] agebyte = new byte[1024];   //定義二進制數組
        byte[] namebyte = new byte[1024];
        byte[] heightbyte = new byte[1024];
        agebyte = Encoding.ASCII.GetBytes(Age.ToString());  //轉換成二進制的
        namebyte = Encoding.ASCII.GetBytes(Name);
        heightbyte = Encoding.ASCII.GetBytes(Height.ToString());
        bytedate = new byte[agebyte.Length + namebyte.Length + heightbyte.Length];  //byte
        agebyte.CopyTo(bytedate, 0); //把值 拷貝 給bytedate  索引
        namebyte.CopyTo(bytedate, agebyte.Length);  //agebyte索引開始
        heightbyte.CopyTo(bytedate, namebyte.Length);
        //string sss = Encoding.ASCII.GetString(data);
        //  Debug.Log(sss);

        //得到本機IP,設置TCP端口號         
        ip = new IPEndPoint(IPAddress.Parse("192.168.1.120"), 12346);//設置自身的IP和端口號,在這里IPAddress.Any是自動獲取本機IP
        newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);//實例化socket對象設置尋址方案為internetwork(IP版本的4存放),設置Soket的類型,為Dgram(支持數據報形式的數據),設置協議的類型,為UDP
        //綁定網絡地址
        newsock.Bind(ip);//綁定IP
        Debug.Log("This is a Server,host name is  " + Dns.GetHostName());//輸出本地的名字
        Debug.Log("Waiting for a client");
        Thread test = new Thread(BeginListening);//定義一個子線程
        test.Start();//子線程開始    
    }
    void BeginListening()
    {
        //      IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);//實例化一個網絡端點,設置為IPAddress.Any為自動獲取跟我通訊的IP,0代表所有的地址都可以
        EndPoint Remote = (EndPoint)(ip);//實例化一個地址結束點來標識網絡路徑
        Debug.Log(Encoding.ASCII.GetString(data, 0, recv));//輸出二進制轉換為string類型用來測試
        while (true)
        {
            data = new byte[1024];//實例化data
            recv = newsock.ReceiveFrom(data, ref Remote);//將數據包接收到的數據放入緩存點,並存儲終節點
            //Debug.Log(Encoding.ASCII.GetString(data, 0, recv));
            mydata = Encoding.ASCII.GetString(data, 0, recv);
            Debug.Log(mydata);
            // newsock.SendTo(Encoding.ASCII.GetBytes(mydata),mydata.Length,SocketFlags.None,Remote);
        }

    }
    string input = "";
    void SendMessage(string message)
    {

        byte[] data = new byte[1024];
        byte[] messbyte = new byte[1024];

        Debug.Log("This is a client,host name is  " + Dns.GetHostName());
        ip = new IPEndPoint(IPAddress.Parse("192.168.1.120"), 12345);
        Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        EndPoint ep = (EndPoint)ip;
        //string welcome = "你好";
        //data = Encoding.ASCII.GetBytes(welcome);
        server.SendTo(data, data.Length, SocketFlags.None, ep);
        messbyte = Encoding.ASCII.GetBytes(message);
        data = new byte[bytedate.Length + messbyte.Length];
        bytedate.CopyTo(data, 0);
        messbyte.CopyTo(data, bytedate.Length);

        server.SendTo(data, ep);
        data = new byte[1024];
        Debug.Log("Stopping Client.");
        server.Close();

    }
    void OnGUI()
    {

        input = GUILayout.TextField(input);
        if (GUILayout.Button("發送"))
        {
            SendMessage(input);
        }
    }
    void OnApplicationQuit()
    {
        newsock.Close();
    }

}

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM