/// <summary>
        /// Returns a raw stream which can be SSL encoded and the original
        /// Network stream so both are accessible. Use the raw stream
        /// for read/write and the Network Stream for checking data
        /// availability
        /// </summary>
        /// <param name="secure"></param>
        /// <returns></returns>
        WebRequestContext OpenConnection(bool secure = false)
        {
            WebRequestContext requestContext = new WebRequestContext();

            try
            {
                requestContext.Connection = TcpServer.AcceptTcpClient();
                requestContext.Connection.ReceiveTimeout = 3000;
                requestContext.Connection.SendTimeout    = 3000;

                requestContext.NetworkStream = requestContext.Connection.GetStream();

                if (secure)
                {
                    requestContext.Stream = new SslStream(requestContext.NetworkStream);
                }
                else
                {
                    requestContext.Stream = requestContext.NetworkStream;
                }
            }
            catch
            {
                return(null);
            }

            return(requestContext);
        }
Exemple #2
0
        /// <summary>
        /// 等待处理新的连接
        /// </summary>
        private void GetAcceptTcpClient()
        {
            try
            {
                semap.WaitOne();
                TcpClient               _tclient          = TcpServer.AcceptTcpClient();
                Socket                  _socket           = _tclient.Client;
                NetworkStream           _stream           = new NetworkStream(_socket, true); //承载这个Socket
                TcpClientConnectSession _connectedSession = new TcpClientConnectSession(_tclient.Client.RemoteEndPoint as IPEndPoint, _tclient, _stream);
                _connectedSession.NewClientFlag = true;
                AddTcpClientConnecedSession(_connectedSession);
                _connectedSession.SkStream.BeginRead(recBuffer, 0, recBuffer.Length, new AsyncCallback(EndReader), _connectedSession);

                if (_stream.CanWrite)
                {
                }

                semap.Release();
            }
            catch (Exception ex)
            {
                semap.Release();
                RaiseDataReceivedEvent(TcpOperateEventCode.NewClientConnectError, null, ex, null, null);
            }
        }
    private void Connect()
    {
        TcpListener server = null;

        try
        {
            Int32     port      = 13000;
            IPAddress localAddr = IPAddress.Parse("127.0.0.1");
            server = new TcpServer();

            server.Start();
            byte[] bytes = new byte[BUFFER];

            // uses "*" as seperation, since the vectors are in the form of (x,y,z), in which "," is already used.
            string lidarData = String.Join("*", lidarReadings);

            while (true)
            {
                Debug.log("Waiting for a connection...");


                TcpClient client = server.AcceptTcpClient();

                NetworkStream stream = client.GetStream();

                int    i;
                string req;
                while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                {
                    req = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                    byte[] msg;

                    if (req.Equals("Lidar"))
                    {
                        msg = System.Text.Encoding.ASCII.GetBytes(lidarData);
                        stream.Write(msg, 0, msg.Length);
                    }
                    else if (req.Equals("GPS"))
                    {
                        msg = System.Text.ASCII.GetBytes(rover.transform.position.toString()); // ?? this may not be correct
                    }
                }
            }
            // Close everything.
            stream.Close();
            client.Close();
        }
        catch (Exception e)
        {
            Debug.Log("Exception: {0}, retyring in 3 seconds.", e);
            Thread.Sleep(3000);
            Connect();
        }
    }