Beispiel #1
0
        public void StartListening()
        {
            // Bind socket
            try
            {
                ThreadHelper.SetNetworkConnectionStatus("Listening on port " + listen_port.ToString());

                server = new TcpListener(IPAddress.Loopback, listen_port);
                server.Start();

                Byte[] bytes = new byte[1024];
                string data  = null;

                while (true)
                {
                    Debug.Log("Waiting for a connection....");
                    TcpClient client = server.AcceptTcpClient();
                    connected_clients.Add(client);
                    connectionStatus = ConnectionStatus.Connected;

                    Debug.Log("A client connected");

                    ThreadHelper.SetNetworkConnectionStatus("Client connected");

                    data = null;

                    NetworkStream stream = client.GetStream();
                    int           i;
                    while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                    {
                        data = Encoding.ASCII.GetString(bytes, 0, i);
                        string[] buffer = data.Split('\n');

                        // Send to subscribers here...
                        foreach (string b in buffer)
                        {
                            if (b.Length > 0)
                            {
                                if (b.StartsWith("{") && b.EndsWith("}"))
                                {
                                    Parse(b);
                                    DeliverSubscriptions();
                                }
                            }
                        }
                        buffer = null;
                    }

                    connected_clients.Remove(client);
                    client.Close();
                }
            }
            catch (Exception e)
            {
                Debug.Log(e.ToString());
            }
        }
Beispiel #2
0
        protected override bool DisconnectProcedure()
        {
            foreach (TcpClient client in connected_clients)
            {
                client.Close();
            }

            connected_clients.Clear();
            server.Stop();
            ThreadHelper.SetNetworkConnectionStatus("Disconnected");
            connectionStatus = ConnectionStatus.Disconnected;
            return(true);
        }