示例#1
0
 private void ReadBytes(object sender, ElapsedEventArgs e)
 {
     byte[] bytes = Read();
     if (bytes.Length != 0)
     {
         RecEvent?.Invoke(bytes);
     }
 }
    void ClientListen()
    {
        try
        {
            while (!master.Connected)
            {
                master = new TcpClient();
                master.Connect(IPAddress.Parse(MasterIp), Port);
            }
            Debug.Log("Connected to Master");

            buffer = new byte[2048]; var stream = master.GetStream();

            buffer = new byte[2048];
            while (true)
            {
                if (!stream.CanRead)
                {
                    Debug.Log("Client Cannot Read");
                    continue;
                }

                if (stream.DataAvailable)
                {
                    int l = stream.Read(buffer, 0, buffer.Length);
                    Debug.Log("Client read " + l + "Bytes");
                    var rec = NetworkUtility.FromNetwork(Encoding.ASCII.GetString(buffer));

                    OnRecieve.Invoke(rec);
                }
            }
        }
        catch (SocketException)
        {
            Debug.Log("Client Socket Error");
        }
    }
    void MasterListen()
    {
        try
        {
            tcpListener = new TcpListener(IPAddress.Any, Port);
            tcpListener.Start();
            Debug.Log("Server is listening on " + MasterIp + ":" + Port);

            client = tcpListener.AcceptTcpClient();
            var stream = client.GetStream();

            buffer = new byte[2048];

            while (true)
            {
                if (!stream.CanRead)
                {
                    Debug.Log("Master Cannot Read");
                    continue;
                }
                if (stream.DataAvailable)
                {
                    int l = stream.Read(buffer, 0, buffer.Length);
                    Debug.Log("Master read " + l + "Bytes");
                    var rec = NetworkUtility.FromNetwork(Encoding.ASCII.GetString(buffer));


                    OnRecieve.Invoke(rec);
                }
            }
        }
        catch (SocketException)
        {
            Debug.Log("Master Network error");
        }
    }
示例#4
0
 private void UART_Win_Driver_ReceivedEvent(object sender, byte[] bytes)
 {
     RecEvent?.Invoke(bytes);
 }