Ejemplo n.º 1
0
 /// <summary>
 /// 开启和选中项的通话
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 void listUsers_MouseDoubleClick(object sender, MouseEventArgs e)
 {
     if (listUsers.SelectedItem == null) return;
     var user = listUsers.SelectedItem as User;
     Socket socket=new Socket(AddressFamily.InterNetworkV6,SocketType.Stream,ProtocolType.Tcp);
     try
     {
         socket.Connect(new IPEndPoint(IPAddress.Parse(user.IP), serverPort));
         CommunicationFrm clientFrm = new CommunicationFrm(socket);
         clientFrm.Text = "客户端:" + socket.ToString();
         clientFrm.Show();
     }
     catch (Exception err)
     {
         MessageBox.Show("服务器未启动:"+err.Message);
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// 开始处理回话请求
        /// </summary>
        private void StartProcessRequest()
        {
            //服务端套字创建
            serverSocket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
            IPAddress ip = Dns.GetHostAddresses(Dns.GetHostName()).FirstOrDefault();
            serverSocket.Bind(new IPEndPoint(ip, serverPort));

            //开启侦听
            serverSocket.Listen(10);

            //处理请求
            ThreadPool.QueueUserWorkItem((obj) =>
            {

                while (true)
                {
                    //1处理连接请求获取代理套接字
                    var proxySocket = serverSocket.Accept();

                    //2创建与客户端通信的窗体
                    CommunicationFrm proxyServerFrm = new CommunicationFrm(proxySocket);
                    proxyServerFrm.Text = "服务端:"+proxySocket.ToString();
                    Application.Run(proxyServerFrm);

                    //这个地方用proxyServerFrm.ShowDialog()也可以,本质和上面区别不大
                }

            }, null);
        }