Search Unity

Asynchronous socket and thread someone could help me

Discussion in 'Multiplayer' started by Rodrigo-Martins, Oct 15, 2014.

  1. Rodrigo-Martins

    Rodrigo-Martins

    Joined:
    Nov 18, 2013
    Posts:
    4
    Hello, I am trying to implement this UDP socket but I'm not having success, already researched a lot on the internet, but can not solve this problem.

    As I understand it the socket is not connected.

    How to step through the socket of the thread?
    What would be the way to implement this asynchronous socket for incoming calls and keep sending updates to all clients?

    I'm new to making and am trying to learn, I am grateful for the help.

    Code (CSharp):
    1. // Bibliotecas.
    2. using System;
    3. using System.IO;
    4. using System.Net;
    5. using System.Net.Sockets;
    6. using System.Threading;
    7.  
    8.  
    9. /**
    10. * @package Massive Game.
    11. * @version 0.1.0
    12. */
    13. namespace MassiveGame {
    14.     /**
    15.      * Estrutura de leitura e escrita.
    16.      */
    17.     internal class LinkData {
    18.         // Conector de rede.
    19.         internal static Socket socket = null;
    20.  
    21.         // Envia.
    22.         internal void AsyncSend(IAsyncResult iAR){
    23.             Socket send = (Socket)iAR.AsyncState;
    24.  
    25.             Console.WriteLine ("Send");
    26.         }
    27.  
    28.         // Recede.
    29.         internal void AsyncReceive(IAsyncResult iAR){
    30.             Socket receive = (Socket)iAR.AsyncState;
    31.             Console.WriteLine ("Receive");
    32.         }
    33.  
    34.         // Envio de messagens.
    35.         internal void Sending(){
    36.             while(true){
    37.                 byte[] buffer = new byte[124];
    38.  
    39.                 LinkData.socket.BeginSend(
    40.                     buffer,
    41.                     0,
    42.                     buffer.Length,
    43.                     SocketFlags.None,
    44.                     new AsyncCallback(AsyncSend),
    45.                     LinkData.socket
    46.                 );
    47.  
    48.                 Thread.Sleep((int)(1000 / Link.sendRate));
    49.             }
    50.         }
    51.  
    52.         // Recebimento de messagens.
    53.         internal void Receive(){
    54.             while(true){
    55.                 byte[] buffer = new byte[124];
    56.  
    57.                 socket.BeginReceive(
    58.                     buffer,
    59.                     0,
    60.                     buffer.Length,
    61.                     SocketFlags.None,
    62.                     new AsyncCallback(AsyncReceive),
    63.                     socket
    64.                 );
    65.  
    66.                 Thread.Sleep((int)(1000 / Link.sendRate));
    67.             }
    68.         }
    69.     }
    70.  
    71.     /**
    72.      * Gestor de conexão cliente servidor.
    73.      */
    74.     public class Link {
    75.         // Threads.
    76.         private static Thread sendingThread;
    77.         private static Thread receiveThread;
    78.  
    79.         // Taxa de envio.
    80.         public static int sendRate = 15;
    81.  
    82.  
    83.         // Fecha a conexão de rede.
    84.         public static void Disconnect(){
    85.             if(LinkData.socket != null){
    86.                 LinkData.socket.Shutdown(SocketShutdown.Both);
    87.                 LinkData.socket.Close();
    88.                 LinkData.socket = null;
    89.             }
    90.         }
    91.  
    92.         // Inicia conexão com o servidor.
    93.         public static void Connect(string host, int port){
    94.             if(LinkData.socket != null) return;
    95.  
    96.             try {
    97.                 IPAddress[] IpAddresses = Dns.GetHostAddresses(host);
    98.                 IPEndPoint  IpEndPoint = new IPEndPoint(IpAddresses[0], port);
    99.  
    100.                 LinkData.socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
    101.  
    102.                 LinkData linkData = new LinkData();
    103.  
    104.                 receiveThread = new Thread(new ThreadStart(linkData.Receive));
    105.                 receiveThread.IsBackground = true;
    106.                 receiveThread.Start();
    107.  
    108.                 Console.WriteLine ("Connection initialized and waiting...");
    109.             }
    110.             catch (SocketException s_ex){
    111.                 throw new ArgumentException(s_ex.ToString());
    112.             }
    113.             catch (Exception ex){
    114.                 throw new ArgumentException(ex.ToString());
    115.             }
    116.  
    117.             while(true){}
    118.         }
    119.  
    120.         // Inicia servidor de serviço.
    121.         public static void Listener(int port){
    122.             if(LinkData.socket != null) return;
    123.  
    124.             try {
    125.                 IPAddress[] IpAddresses = Dns.GetHostAddresses("127.0.0.1");
    126.                 IPEndPoint  IpEndPoint = new IPEndPoint(IpAddresses[0], port);
    127.  
    128.                 LinkData.socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
    129.                 LinkData.socket.Bind((EndPoint)IpEndPoint);
    130.  
    131.                 LinkData linkData = new LinkData();
    132.  
    133.                 receiveThread = new Thread(new ThreadStart(linkData.Receive));
    134.                 receiveThread.IsBackground = true;
    135.                 receiveThread.Start();
    136.  
    137.                 sendingThread = new Thread(new ThreadStart(linkData.Sending));
    138.                 sendingThread.IsBackground = true;
    139.                 sendingThread.Start();
    140.  
    141.                 Console.WriteLine ("Server initialized and waiting...");
    142.             }
    143.             catch (SocketException s_ex){
    144.                 throw new ArgumentException(s_ex.ToString());
    145.             }
    146.             catch (Exception ex){
    147.                 throw new ArgumentException(ex.ToString());
    148.             }
    149.  
    150.             while(true){}
    151.         }
    152.     }
    153. }
    154.  
     
  2. Rodrigo-Martins

    Rodrigo-Martins

    Joined:
    Nov 18, 2013
    Posts:
    4
    Finally managed to solve the problem. I'm very noob even ...

    To pass a variable to a thread used an instance of the class with the Linkdata socket.

    Code (CSharp):
    1. socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
    2. socket.Blocking = false;
    3. socket.Bind(IpEndPoint);
    4.  
    5. LinkData linkData = new LinkData(socket);
    6. receiveThread = new Thread(new ThreadStart(linkData.Receive));
    7. receiveThread.IsBackground = true;
    8. receiveThread.Start();
    9.  
    10. sendingThread = new Thread(new ThreadStart(linkData.Sending));
    11. sendingThread.IsBackground = true;
    12. sendingThread.Start();
    Code (CSharp):
    1. internal class LinkData {
    2.     // Conector de rede.
    3.     internal Socket socket = null;
    4.  
    5.     // Construtor.
    6.     internal LinkData(Socket socket){
    7.         this.socket = socket;
    8.     }
    9. }