Advertisement
Guest User

ServerUDP Example

a guest
Oct 19th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.00 KB | None | 0 0
  1. /**
  2.  * ServerUDP.cs
  3.  * Created by: Jadson Almeida [jadson.sistemas@gmail.com]
  4.  * Created on: 19/10/2017 (dd/mm/yy)
  5.  */
  6.  
  7. using System;
  8. using System.Net;
  9. using System.Net.Sockets;
  10. using System.Threading;
  11.  
  12. /// <summary>
  13. /// Simple example for a server UDP receive and send data
  14. /// </summary>
  15. public class ServerUDP {
  16.  
  17.     /// <summary>
  18.     /// UDP connection to send data
  19.     /// </summary>
  20.     UdpClient udpSend;
  21.     /// <summary>
  22.     /// UDP connection to receive data
  23.     /// </summary>
  24.     UdpClient udpReceive;
  25.     /// <summary>
  26.     /// IP end point used to send data in UDP connection
  27.     /// </summary>
  28.     IPEndPoint ipEndPointSend;
  29.     /// <summary>
  30.     /// IP end point used to receive data in UDP connection
  31.     /// </summary>
  32.     IPEndPoint ipEndPointReceive;
  33.     /// <summary>
  34.     /// Port used to send data in UDP connection
  35.     /// </summary>
  36.     int portToSend;
  37.     /// <summary>
  38.     /// Port used to send data in UDP connection
  39.     /// </summary>
  40.     int portToReceive;
  41.     /// <summary>
  42.     /// Thread used to listener UDP connection with <see cref="ReceiveData"/>
  43.     /// </summary>
  44.     Thread thread;
  45.  
  46.     public ServerUDP()
  47.     {
  48.         // Creates a UDP connection to send data
  49.         ipEndPointSend = new IPEndPoint(IPAddress.Parse("127.0.0.1"), portToSend);
  50.         udpSend = new UdpClient();
  51.         // Creates a UDP connection to receive data
  52.         ipEndPointReceive = new IPEndPoint(IPAddress.Any, portToReceive);
  53.         udpReceive = new UdpClient()
  54.         {
  55.             ExclusiveAddressUse = false
  56.         };
  57.         udpReceive.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
  58.         udpReceive.Client.Bind(ipEndPointReceive);
  59.         // Start thread to listen received data
  60.         thread = new Thread(new ThreadStart(ListeningData));
  61.         thread.Start();
  62.     }
  63.    
  64.     /// <summary>
  65.     /// (Used as thread) Listen external input from UDP and pass to <see cref="ReceiveData(IAsyncResult)"/>
  66.     /// </summary>
  67.     void ListeningData()
  68.     {
  69.         try
  70.         {
  71.             udpReceive.BeginReceive(new AsyncCallback(ReceiveData), null);
  72.         }
  73.         catch (Exception err)
  74.         {
  75.         }
  76.     }
  77.    
  78.     /// <summary>
  79.     /// Receive the data from clients and pass it to <see cref="HandleData(byte[])"/>
  80.     /// </summary>
  81.     /// <param name="res"></param>
  82.     void ReceiveData(IAsyncResult res)
  83.     {
  84.         IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, portToReceive);
  85.         byte[] received = udpReceive.EndReceive(res, ref RemoteIpEndPoint);
  86.         HandleData(received);
  87.         ListeningData(); // repeat
  88.     }
  89.    
  90.     /// <summary>
  91.     /// Handles what data means with <see cref="CalculatePhysics(byte)"/>
  92.     /// </summary>
  93.     /// <param name="data">data to pass to <see cref=""/></param>
  94.     void HandleData(byte[] data)
  95.     {
  96.         // Check if data is wrong
  97.         if (data.Length > 0 && data[0] > 0)
  98.             CalculatePhysics(data[0]);
  99.     }
  100.    
  101.     /// <summary>
  102.     /// Calculate physic movement based in external input and pass the movement to be sended with <see cref="SendCommand(byte[])"/>
  103.     /// </summary>
  104.     /// <param name="inputs">External input (client UDP)</param>
  105.     void CalculatePhysics(byte inputs)
  106.     {
  107.         byte[] data;
  108.         // calculate what we need to do with the player input
  109.         // ...
  110.         // transform the new position, rotation etc in byte array like this:
  111.         // index: 0 to 3 are values of the front, 4 to 7 are values of the side, 8 to 11 are up, 12 to 15 are x-axis, 16 to 19 are y-axis, 20 to 23 are z-axis,
  112.         // 24 to 27 are board port, 28 to 31 are starboard, 32 to 35 are wing angle, and 36 to 39 are sail angle
  113.         // ...
  114.         // Calls send command
  115.        
  116.         SendCommand(data);
  117.     }
  118.  
  119.     /// <summary>
  120.     /// Sends data for client in <see cref="ipEndPointSend"/>
  121.     /// </summary>
  122.     /// <param name="data">data to send</param>
  123.     void SendCommand(byte[] data)
  124.     {
  125.         try
  126.         {
  127.             udpSend.Send(data, data.Length, ipEndPointSend);
  128.         }
  129.         catch (Exception err)
  130.         {
  131.             print(err.ToString());
  132.         }
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement