Advertisement
relax4o

udp_client

Dec 17th, 2015
407
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.62 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.Networking;
  3. using System.Collections;
  4. using System.Text;
  5.  
  6. public class NetManager : MonoBehaviour {
  7.  
  8.     [SerializeField] private string ip = "127.0.0.1";
  9.     public int unreliableChannelId;
  10.  
  11.     private int socketId;
  12.     private int connectionId;
  13.     private string message = "Default text";
  14.  
  15.     void Start()
  16.     {
  17.         // Initializing Network Transport Layer
  18.         NetworkTransport.Init ();
  19.  
  20.         // Getting default configuration for host topology
  21.         ConnectionConfig CConfig = new ConnectionConfig();
  22.         unreliableChannelId = CConfig.AddChannel(QosType.Unreliable);
  23.  
  24.         // Creating host topology
  25.         HostTopology topology = new HostTopology(CConfig, 2);
  26.    
  27.         // Open client socket
  28.         socketId = NetworkTransport.AddHost (topology, 52000);
  29.  
  30.         Connect ();
  31.     }
  32.  
  33.     void OnGUI ()
  34.     {
  35.         message = GUI.TextField(new Rect(10, 10, 200, 20), message, 25);
  36.  
  37.         if (GUI.Button(new Rect(20, 70, 120, 20), "Send message")) {
  38.             SendSocketMessage(message);
  39.         }
  40.     }
  41.  
  42.     void Update()
  43.     {
  44.         int recHostId; // rec - received
  45.         int recConnectionId;
  46.         int recChannelId;
  47.         byte[] recBuffer = new byte[1024];
  48.         int bufferSize = 1024;
  49.         int dataSize;
  50.         byte error;
  51.  
  52.         NetworkEventType recNetworkEvent = NetworkTransport.Receive(out recHostId, out recConnectionId, out recChannelId, recBuffer, bufferSize, out dataSize, out error);
  53.  
  54.         switch (recNetworkEvent)
  55.         {
  56.             case NetworkEventType.Nothing:
  57.                 //Debug.Log("Nothing");
  58.             break;
  59.  
  60.             case NetworkEventType.ConnectEvent:
  61.                 Connected (recConnectionId);
  62.             break;
  63.  
  64.             case NetworkEventType.DataEvent:
  65.                 DataReceiver(recBuffer);
  66.                 //NetworkTransport.Disconnect(socketId, connectionId, out error);
  67.             break;
  68.  
  69.             case NetworkEventType.DisconnectEvent:
  70.                 Debug.Log("Socket closed!");
  71.             break;
  72.         }
  73.     }
  74.  
  75.     public void Connect()
  76.     {
  77.         byte error;
  78.         connectionId = NetworkTransport.Connect(socketId, "127.0.0.1", 3000, 0, out error);
  79.         Debug.Log("Connected to server. ConnectionId: " + connectionId);
  80.     }
  81.  
  82.     public void SendSocketMessage(string msg) {
  83.         byte error;
  84.         byte[] buffer = new byte[1024];
  85.         Encoding enc = Encoding.ASCII;
  86.         int length = enc.GetByteCount(msg);
  87.         buffer = enc.GetBytes (msg);
  88.        
  89.         int bufferSize = 1024;
  90.         NetworkTransport.Send(socketId, connectionId, unreliableChannelId, buffer, length, out error);
  91.     }
  92.  
  93.     private void Connected(int recConnectionId)
  94.     {
  95.         if( recConnectionId == connectionId ) {
  96.             Debug.Log("Connection established!");
  97.         }
  98.     }
  99.  
  100.     private void DataReceiver(byte[] bytes)
  101.     {
  102.         byte error;
  103.         Encoding enc = Encoding.ASCII;
  104.         string message = enc.GetString(bytes);
  105.         Debug.Log("Message received: " + message);
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement