private void chat_button_Click(object sender, EventArgs e)          //找朋友
        {
            String    name = userdata_listView.SelectedItems[0].Text;
            IPAddress IP   = IPAddress.Parse(userdata_listView.SelectedItems[0].SubItems[2].Text);

/*            while(TCP_concect.checkport(port))
 *          {
 *              port++;
 *          }*/
            IPEndPoint point       = new IPEndPoint(IP, testlisport);
            Socket     socket_peer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            if (!TCP_concect.tcp_connect(socket_peer, point))
            {
                MessageBox.Show("Connect fail!", "Error", MessageBoxButtons.OK);
            }
            else
            {
                socket_peer.Send(Encoding.UTF8.GetBytes(thisname));
                Thread server_client_connection = new Thread(() => Application.Run(new chat_private(name, socket_peer)));
                server_client_connection.SetApartmentState(System.Threading.ApartmentState.STA);//单线程监听控制
                server_client_connection.Start();
                //chat_private p = new chat_private(name, socket_peer);
                //p.Show();
            }


            /*chat_private p = new chat_private(name, IP);
             * p.Show();*/

            /*Console.WriteLine(userdata_listView.SelectedItems[0].Text);
             * Console.WriteLine(userdata_listView.SelectedItems[0].SubItems[2].Text);*/
        }
Beispiel #2
0
 public Form1()
 {
     TCP_concect.tcpClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
     if (TCP_concect.tcp_connect() == false)
     {
         MessageBox.Show("Connect fail!", "Error", MessageBoxButtons.OK);
     }
     InitializeComponent();
 }
        private void listen()               //監聽
        {
            if (TCP_concect.checkport(lisport))
            {
                MessageBox.Show("Listen port has been used!", "Error", MessageBoxButtons.OK);
            }
            IPEndPoint point     = new IPEndPoint(selfIP, lisport);
            Socket     newsocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            newsocket.Bind(point);          //綁定IP和端口
            newsocket.Listen(20);           //最大連接數為20
            asynaccept(newsocket);
            Console.WriteLine("start_listen:");
        }
        private void group_chat_button_Click(object sender, EventArgs e)                //發起群聊
        {
            foreach (ListViewItem item in userdata_listView.Items)
            {
                Console.WriteLine(item.SubItems[1].Text);
                if (item.Selected == true && item.SubItems[1].Text != "online")
                {
                    MessageBox.Show("your friend is not online", "error", MessageBoxButtons.OK);
                    return;
                }
            }

            string allname = thisname;

            Socket[] allsocket = new Socket[userdata_listView.SelectedItems.Count];
            foreach (ListViewItem item in userdata_listView.SelectedItems)
            {
                allname = allname + " " + item.SubItems[0].Text;
            }

            int        i = 0;
            IPAddress  IP;
            IPEndPoint point;
            int        number = userdata_listView.SelectedItems.Count;

            foreach (ListViewItem item in userdata_listView.SelectedItems)
            {
                IP    = IPAddress.Parse(item.SubItems[2].Text);
                point = new IPEndPoint(IP, testlisport2[i]);
                Socket newsocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                TCP_concect.tcp_connect(newsocket, point);
                newsocket.Send(Encoding.UTF8.GetBytes("--group-" + allname));
                allsocket[i] = newsocket;
                i++;
            }
            Thread server_client_connection = new Thread(() => Application.Run(new chat_group(allname, allsocket, number, thisname)));

            server_client_connection.SetApartmentState(System.Threading.ApartmentState.STA);//单线程监听控制
            server_client_connection.Start();
        }