Hyluss

[Unity] Server with sockets

May 6th, 2018
559
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.22 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Net;
  6. using System.Net.Sockets;
  7. using UnityEngine;
  8.  
  9. public class Server : MonoBehaviour {
  10.  
  11.     [SerializeField] int port;
  12.  
  13.  
  14.     List<ServerClient> clients;
  15.     List<ServerClient> disconnectList;
  16.  
  17.     TcpListener server;
  18.     bool serverStarted;
  19.  
  20.     private void Start()
  21.     {
  22.         clients = new List<ServerClient>();
  23.         disconnectList = new List<ServerClient>();
  24.  
  25.         try
  26.         {
  27.             server = new TcpListener(IPAddress.Any, port);
  28.             server.Start();
  29.  
  30.             StartListening();
  31.             serverStarted = true;
  32.             Debug.Log("Server has been started on port " + port);
  33.         }
  34.         catch(System.Exception e)
  35.         {
  36.             Debug.Log("socekt error: " + e.Message);
  37.         }
  38.     }
  39.  
  40.     private void Update()
  41.     {
  42.         if(!serverStarted)
  43.         {
  44.             return;
  45.         }
  46.  
  47.         foreach (ServerClient client in clients)
  48.         {
  49.             //is the client still connected
  50.             if(!IsConnected(client.tcp))
  51.             {
  52.                 client.tcp.Close();
  53.                 disconnectList.Add(client);
  54.                 continue;
  55.             }
  56.             //check for message form the client
  57.             else
  58.             {
  59.                 NetworkStream networkStream = client.tcp.GetStream();
  60.                 if(networkStream.DataAvailable)
  61.                 {
  62.                     StreamReader reader = new StreamReader(networkStream, true);
  63.                     string data = reader.ReadLine();
  64.  
  65.                     if(data !=null)
  66.                     {
  67.                         OnIncomingData(client, data);
  68.                     }
  69.                 }
  70.             }
  71.         }
  72.     }
  73.  
  74.     void OnIncomingData(ServerClient client, string data)
  75.     {
  76.         Debug.Log(client.clientName + "has sent the following message: " + data);
  77.  
  78.     }
  79.  
  80.     bool IsConnected(TcpClient client)
  81.     {
  82.         try
  83.         {
  84.             if(client !=null && client.Client != null && client.Client.Connected)
  85.             {                  
  86.                 //true if Listen has been called and a connection is pending;
  87.                 if (client.Client.Poll(0,SelectMode.SelectRead) )
  88.                 {
  89.                     return !(client.Client.Receive(new byte[1], SocketFlags.Peek) == 0);
  90.                 }
  91.                 return true;
  92.             }
  93.             else
  94.             {
  95.                 return false;
  96.             }
  97.         }
  98.         catch
  99.         {
  100.             return false;
  101.         }
  102.     }
  103.  
  104.     void StartListening()
  105.     {
  106.         server.BeginAcceptTcpClient(AcceptTcpClient, server);
  107.     }
  108.  
  109.     void AcceptTcpClient(IAsyncResult asyncResult)
  110.     {
  111.         TcpListener listener = (TcpListener)asyncResult.AsyncState;
  112.         clients.Add(new ServerClient(listener.EndAcceptTcpClient(asyncResult)));
  113.         StartListening();
  114.  
  115.         //send a message to everyone, say someone has connected
  116.     }
  117.  
  118. }
  119.  
  120. public class ServerClient
  121. {
  122.     public TcpClient tcp;
  123.     public string clientName;
  124.  
  125.     public ServerClient(TcpClient clientSocket)
  126.     {
  127.         clientName = "Guest";
  128.         tcp = clientSocket;
  129.     }
  130. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment