Пример #1
0
        private void FriendList_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            string[] status = FriendList.SelectedItem.ToString().Split(new Char[] { '(', ')' });
            if (status[1] == "离线")
            {
                MessageBox.Show("好友不在线,无法聊天!", "会话失败");
            }
            else
            {
                string sendStr = "q" + status[0];
                string recvStr = "";
                byte[] bs      = Encoding.ASCII.GetBytes(sendStr); //把字符串编码为字节
                s.Send(bs, bs.Length, 0);                          //发送信息
                recvStr = "";
                byte[] recvBytes = new byte[1024];
                int    bytes;
                bytes    = s.Receive(recvBytes, recvBytes.Length, 0);     //从服务器端接受返回信息
                recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes); //对方IP
                // public Connect(int m_num, bool m_state, bool m_positive, string m_IP_client, string m_name_client)
                current_num = Frmcnct.Getcurrent_num();
                // Connect C1 = new Connect(user,current_num, true, true, recvStr,status[0]);
                bool ACK = true;    //主动发起

                Form3 f3 = new Form3(user, current_num, true, true, recvStr, status[0], 0, ACK);
                if (Frmcnct.reply == false)
                {
                    f3.Close();
                    Frmcnct.reply = true;
                }
                else
                {
                    f3.Show();
                }
            }
        }
Пример #2
0
        private void button3_Click(object sender, EventArgs e)
        {
            int m_num = Frmcnct.Getcurrent_num();

            Form5 f5 = new Form5(usernanme, m_num, m_state, m_positive, m_IP_client, m_name_client, group_num);

            f5.Show();
            this.Close();
        }
Пример #3
0
        private void MainServer()
        {
            string serverHost = GetLocalIP();                                                       //ipEntry.AddressList[1].ToString();//转换为string类
            ///创建终结点(EndPoint)
            IPAddress  ip  = IPAddress.Parse(serverHost);                                           //把ip地址字符串转换为IPAddress类型的实例
            IPEndPoint ipe = new IPEndPoint(ip, SERVERPOST);                                        //用指定的端口和ip初始化IPEndPoint类的新实例
            //创建socket并开始监听
            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //创建一个socket对像,如果用udp协议,则要用SocketType.Dgram类型的套接字

            s.Bind(ipe);                                                                            //绑定EndPoint对像(2000端口和ip地址)
            while (true)
            {
                s.Listen(1000);                //开始监听
                ///接受到client连接,为此连接建立新的socket,并接受信息
                Socket temp      = s.Accept(); //为新建连接创建新的socket
                string recvStr   = "";
                byte[] recvBytes = new byte[1024];
                int    bytes;
                bytes    = temp.Receive(recvBytes, recvBytes.Length, 0); //从客户端接受信息
                recvStr += Encoding.UTF8.GetString(recvBytes, 0, bytes); //2012011447+num
                string[] str = recvStr.Split(new Char[] { '+' });
                current_num = Frmcnct.Getcurrent_num();
                //  string IP_client = ((IPEndPoint)(s.RemoteEndPoint)).Address.ToString();
                IP_client   = str[2];
                num_client  = int.Parse(str[1]);
                positive    = false;
                name_client = str[0];
                string type = str[3];
                //public Connect(int m_num, bool m_state, bool m_positive, string m_IP_client,  string m_name_client,int m_num_client,ACK)
                // Connect C0 = new Connect(user,current_num, true, false, IP_client,  str[0],num_client,);
                if (str[3] == "Single")
                {
                    if (MessageBox.Show("是否同意来自" + str[0] + "的会话请求?", "会话请求", MessageBoxButtons.OKCancel) == DialogResult.OK)
                    {
                        ACK = true;
                        temp.Close();
                    }
                    else
                    {
                        ACK = false;
                        temp.Close();
                    }
                    MethodInvoker mi = new MethodInvoker(this.ShowForm);
                    this.BeginInvoke(mi);
                }
                else
                {
                    if (MessageBox.Show("是否同意来自" + str[0] + "发起的群聊请求?", "群聊请求", MessageBoxButtons.OKCancel) == DialogResult.OK)
                    {
                        ACK = true;
                        temp.Close();
                    }
                    else
                    {
                        ACK = false;
                        temp.Close();
                    }
                    mygroupnum = int.Parse(str[4]);
                    groupnum   = int.Parse(str[5]);
                    for (int j = 1; j < groupnum; j++)
                    {
                        name_group[j] = str[j + 5];
                    }
                    name_group[0] = str[0];
                    MethodInvoker mi = new MethodInvoker(this.ShowForm_group);
                    this.BeginInvoke(mi);
                }
            }
            s.Close();
        }