예제 #1
0
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            // Получаем guid приложения
            string guid = Marshal.GetTypeLibGuidForAssembly(Assembly.GetExecutingAssembly()).ToString();
            // Создаём мьютекс и узнаём у него, запущен ли хоть один процесс с таким guid
            Mutex mutexObj = new Mutex(true, guid, out bool existed);

            // Если запущен, этот экземпляр программы необходимо закрыть
            if (!existed)
            {
                MessageBox.Show("Программа уже запущена");
                mutexObj.Dispose();
                return;
            }
            // Настраиваем сервер
            SocketHelper.ConfigureListener();
            // Запускаем пргограмму
            Application.Run(new MainForm());
            // При выходе из программы нужно отключить всех клиентов
            if (SocketHelper.Status)
            {
                SocketHelper.ChangeStatus();
            }
        }
예제 #2
0
        public void createListener()
        {
            // Create an instance of the TcpListener class.
            TcpListener tcpListener = null;
            IPAddress   ipAddress   = Dns.GetHostEntry("localhost").AddressList[0];

            try
            {
                // Set the listener on the local IP address
                // and specify the port.
                tcpListener = new TcpListener(ipAddress, 13);
                tcpListener.Start();
                output = "Waiting for a connection...";
            }
            catch (Exception e)
            {
                output = "Error: " + e.ToString();
                MessageBox.Show(output);
            }
            while (true)
            {
                // Always use a Sleep call in a while(true) loop
                // to avoid locking up your CPU.
                Thread.Sleep(10);
                // Create a TCP socket.
                // If you ran this server on the desktop, you could use
                // Socket socket = tcpListener.AcceptSocket()
                // for greater flexibility.
                TcpClient tcpClient = tcpListener.AcceptTcpClient();
                // Read the data stream from the client.
                byte[]        bytes  = new byte[256];
                NetworkStream stream = tcpClient.GetStream();
                stream.Read(bytes, 0, bytes.Length);
                SocketHelper helper = new SocketHelper();
                helper.processMsg(tcpClient, stream, bytes);
            }
        }
예제 #3
0
파일: Server.cs 프로젝트: myrdev/SampleCode
 public void createListener()
 {
     // Create an instance of the TcpListener class.
     TcpListener tcpListener = null;
     IPAddress ipAddress = Dns.GetHostEntry("localhost").AddressList[0];
     try
     {
         // Set the listener on the local IP address
         // and specify the port.
         tcpListener = new TcpListener(ipAddress, 13);
         tcpListener.Start();
         output = "Waiting for a connection...";
     }
     catch (Exception e)
     {
         output = "Error: " + e.ToString();
         MessageBox.Show(output);
     }
     while (true)
     {
         // Always use a Sleep call in a while(true) loop
         // to avoid locking up your CPU.
         Thread.Sleep(10);
         // Create a TCP socket.
         // If you ran this server on the desktop, you could use
         // Socket socket = tcpListener.AcceptSocket()
         // for greater flexibility.
         TcpClient tcpClient = tcpListener.AcceptTcpClient();
         // Read the data stream from the client.
         byte[] bytes = new byte[256];
         NetworkStream stream = tcpClient.GetStream();
         stream.Read(bytes, 0, bytes.Length);
         SocketHelper helper = new SocketHelper();
         helper.processMsg(tcpClient, stream, bytes);
     }
 }