private void Form1_FormClosing(object sender, FormClosingEventArgs e)
 {
     if (server != null)
     {
         server.Close();
         server = null;
     }
     if (client != null)
     {
         client.Close();
         client = null;
     }
 }
Esempio n. 2
0
 void OnDestroy()
 {
     DCLog.Log("end main");
     mChannel.DisposeRes();
     mChannel.Close();
     mServer.DisposeRes();
     mServer.Close();
 }
Esempio n. 3
0
 public void StopListening()
 {
     if (!IsListening)
     {
         return;
     }
     IsListening = false;
     _listener.Close();
 }
        public void SocketCommunication_ServerSends_ClientReceives()
        {
            IPHostEntry host      = Dns.GetHostEntry(Dns.GetHostName());
            string      localhost = "";

            foreach (IPAddress ip in host.AddressList)
            {
                if (ip.AddressFamily.ToString() == "InterNetwork")
                {
                    localhost = ip.ToString();
                }
            }

            // Create message to send
            RegisterMessage expectedMessage = TestHelper.CreateRegisterMessage();
            string          messageStr      = expectedMessage.ToXmlString();

            // Create server
            IPAddress address = IPAddress.Parse(localhost);
            int       port    = 5555;

            NetworkServer server = new NetworkServer(address, port);

            server.Open();
            server.StartListeningForClients();

            // connect client
            NetworkClient client = new NetworkClient(address, port);

            client.Connect();

            client.Send(expectedMessage);

            ArrayList sockets = new ArrayList();

            while ((sockets = server.SelectForRead()).Count == 0)
            {
                ;
            }

            Socket  socket        = (Socket)sockets[0];
            Message actualMessage = server.Receive(socket);

            server.Close();
            client.Disconnect();

            Assert.AreEqual(expectedMessage, actualMessage);
        }
Esempio n. 5
0
 /// <summary>
 /// 关闭服务器(及其关联的客户端),如果不为服务器模式则不操作
 /// </summary>
 public static void AbortServer()
 {
     if (CurrentHostType.Equals(PHostType.Server))
     {
         if (NetworkClient != null)
         {
             NetworkClient.Stop();
             _NetworkClient = null;
         }
         if (NetworkServer != null)
         {
             NetworkServer.Close();
             _NetworkServer = null;
         }
         CurrentHostType = PHostType.NoType;
     }
 }
Esempio n. 6
0
        /**
         * Start as Server.
         */
        static void StartServer()
        {
            NetworkServer networkServer = new NetworkServer("127.0.0.1", 7788);

            Console.WriteLine("End");

            while (true)
            {
                string command = Console.ReadLine();

                if (command == String.Empty)
                {
                    break;
                }

                foreach (var serverClientKeyValuePair in networkServer.GetServerClients())
                {
                    byte[] messageBytes = Encoding.UTF8.GetBytes(command);
                    serverClientKeyValuePair.Value.SendMessage(messageBytes);
                }
            }

            networkServer.Close();
        }