예제 #1
0
 private void Receive()
 {
     _socket.BeginReceiveFrom(state.buffer, 0, bufSize, SocketFlags.None, ref epFrom, recv = (ar) =>
     {
         State so  = (State)ar.AsyncState;
         int bytes = _socket.EndReceiveFrom(ar, ref epFrom);
         _socket.BeginReceiveFrom(so.buffer, 0, bufSize, SocketFlags.None, ref epFrom, recv, so);
         Console.WriteLine("RECV: {0}: {1}, {2}", epFrom.ToString(), bytes, Encoding.ASCII.GetString(so.buffer, 0, bytes));
         string msg = Encoding.ASCII.GetString(so.buffer, 0, bytes);
         if (lastR == msg)
         {
             return;
         }
         lastR = msg;
         Debug.Log("RAW RECEIVE: " + msg);
         UDPEvent.Receive(msg);
         if (msg.StartsWith("SET"))
         {
             string[] args = msg.Split(' ');
             if (args.Length != 3)
             {
                 return;
             }
             if (Coffre.Existe(args[0]))
             {
                 Coffre.Vider(args[0]);
             }
             Coffre.Remplir(args[1], args[2]);
         }
     }, state);
 }
예제 #2
0
    public void Client(string address, int port)
    {
        new Thread(() =>
        {
            try
            {
                const int PORT_NO      = 27000;
                const string SERVER_IP = "127.0.0.1";
                //---data to send to the server---

                //---create a TCPClient object at the IP and port no.---
                TcpClient client       = new TcpClient(SERVER_IP, PORT_NO);
                NetworkStream nwStream = client.GetStream();

                while (true)
                {
                    this.client = client;
                    this.ns     = nwStream;

                    //---read back the text---
                    byte[] bytesToRead = new byte[client.ReceiveBufferSize];
                    int bytesRead      = nwStream.Read(bytesToRead, 0, client.ReceiveBufferSize);
                    string msg         = Encoding.ASCII.GetString(bytesToRead, 0, bytesRead);
                    string[] p         = msg.Split(';');
                    foreach (string m in p)
                    {
                        if (m == "")
                        {
                            continue;
                        }
                        Debug.Log("RAW RECEIVE: " + m);
                        UDPEvent.Receive(m);
                        if (m.StartsWith("SET"))
                        {
                            string[] args = m.Split(' ');
                            if (args.Length != 3)
                            {
                                return;
                            }
                            if (Coffre.Existe(args[0]))
                            {
                                Coffre.Vider(args[0]);
                            }
                            Coffre.Remplir(args[1], args[2]);
                        }
                    }
                }

                client.Close();
            }
            catch
            {}
        }).Start();
    }