Example #1
0
        }         //修改FriendListView的方法

        private void AddFriendButton_Click(object sender, RoutedEventArgs e)
        {
            bool isFind = myfriendIDAndNames.Any <friendIDAndName>(p => p.friendID == addFriendOO.Text);

            if (isFind)
            {
                MessageBox.Show("好友已在列表中");
                return;
            }
            else
            {
                friendIDAndName IDAndName = new friendIDAndName();
                IDAndName.friendID   = addFriendOO.Text;
                IDAndName.friendName = addFriendOO.Text;
                myfriendIDAndNames.Add(IDAndName);
                return;
            }             //未找到该ip与端口号,需要增加

            TcpClient   tcpClient;
            StateObject stateObject;

            tcpClient             = new TcpClient();   //每次发送建立一个TcpClient类对象
            stateObject           = new StateObject(); ////每次发送建立一个StateObject类对象
            stateObject.tcpClient = tcpClient;
            //stateObject.buffer = SendMsg;
            //stateObject.friendIDAndName = friendIDAndName.friendID + ":" + friendIDAndName.friendName; //所选好友IP和端口号
            IMClassLibrary.SingleChatDataPackage chatData = new IMClassLibrary.SingleChatDataPackage(UserID, IPAndPort, "添加您为好友", UserID, addFriendOO.Text);
            stateObject.buffer = chatData.DataPackageToBytes();                                                   //buffer为发送的数据包的字节数组
            tcpClient.BeginConnect(LoginIP, int.Parse(LoginPort), new AsyncCallback(SentCallBackF), stateObject); //异步连接
            //发送添加好友信息
        }
        }         //修改FriendListView的方法

        private void AddFriendButton_Click(object sender, RoutedEventArgs e)
        {
            IPAddress myFriendIpAdress;

            if (IPAddress.TryParse(addFriendIPTextBox.Text, out myFriendIpAdress) == false)
            {
                MessageBox.Show("IP地址格式不正确!");
                return;
            }
            int myFriendPort;

            if (int.TryParse(addFriendPortTextBox.Text, out myFriendPort) == false)
            {
                MessageBox.Show("端口号格式不正确!");
                return;
            }
            else
            {
                if (myFriendPort < 1024 || myFriendPort > 65535)
                {
                    MessageBox.Show("端口号范围不正确!(1024-65535)");
                    return;
                }
            }
            FriendIPAndPort friendIPAndPort = new FriendIPAndPort();

            friendIPAndPort.friendIP   = addFriendIPTextBox.Text;           //IP字符串
            friendIPAndPort.friendPort = addFriendPortTextBox.Text;         //端口字符串
            int k = myFriendIPAndPorts.IndexOf(friendIPAndPort);

            if (k == -1)
            {
                myFriendIPAndPorts.Add(friendIPAndPort);
            }             //未找到该ip与端口号,需要增加
            else
            {
                MessageBox.Show("好友已在列表中");
                return;
            }

            TcpClient   tcpClient;
            StateObject stateObject;

            tcpClient             = new TcpClient();   //每次发送建立一个TcpClient类对象
            stateObject           = new StateObject(); ////每次发送建立一个StateObject类对象
            stateObject.tcpClient = tcpClient;
            //stateObject.buffer = SendMsg;
            stateObject.friendIPAndPort = friendIPAndPort.friendIP + ":" + friendIPAndPort.friendPort;                     //所选好友IP和端口号
            IMClassLibrary.SingleChatDataPackage chatData = new IMClassLibrary.SingleChatDataPackage(UserID, IPAndPort, "添加您为好友");
            stateObject.buffer = chatData.DataPackageToBytes();                                                            //buffer为发送的数据包的字节数组
            tcpClient.BeginConnect(friendIPAndPort.friendIP, myFriendPort, new AsyncCallback(SentCallBackF), stateObject); //异步连接
            //发送添加好友信息
        }
Example #3
0
        public void SendMessageTo(string IP, string port, string message)
        {
            TcpClient   tcpClient;
            StateObject stateObject;

            tcpClient             = new TcpClient();   //每次发送建立一个TcpClient类对象
            stateObject           = new StateObject(); ////每次发送建立一个StateObject类对象
            stateObject.tcpClient = tcpClient;
            //stateObject.buffer = SendMsg;
            stateObject.friendIPAndPort = IP + ":" + port;             //所选好友IP和端口号
            var chatData = new IMClassLibrary.SingleChatDataPackage("Server", "Server", message, "Server", "Server");

            stateObject.buffer = chatData.DataPackageToBytes();             //buffer为发送的数据包的字节数组
            tcpClient.BeginConnect(IP, int.Parse(port), new AsyncCallback(SentCallBackF), stateObject);
        }
Example #4
0
        public delegate void ReadDataF(TcpClient tcpClient);                                          //代表无返回值 Tcpclient参数方法

        private void SendMessageButton_Click(object sender, RoutedEventArgs e)
        {
            if (SendMessageTextBox.Text == "")
            {
                MessageBox.Show("发送信息不能为空");
                return;
            }             //是否输入了发送信息
            if (FriendListView.SelectedItems.Count == 0)
            {
                MessageBox.Show("请选择好友OO号");
                return;
            }             //是否选择发送好友
            friendIDAndName[] myIPAndPorts = new friendIDAndName[FriendListView.SelectedItems.Count];
            for (int i = 0; i < myIPAndPorts.Length; i++)
            {
                myIPAndPorts[i] = (friendIDAndName)FriendListView.SelectedItems[i];
            }             //得到所有要发送的好友IP和端口号
            //string s = IPAndPort + "说:" + SendMessageTextBox.Text; //要发送的字符串
            //UnicodeEncoding ascii = new UnicodeEncoding(); //以下将字符串转换为字节数组
            //int n = (ascii.GetBytes(s)).Length;
            //byte[] SendMsg = new byte[n];
            //SendMsg = ascii.GetBytes(s);
            string      ip   = null;  //记录好友端IP
            int         port = 0;     //记录好友端端口号
            TcpClient   tcpClient;
            StateObject stateObject;

            IMClassLibrary.SingleChatDataPackage chatData = null;
            for (int i = 0; i < myIPAndPorts.Length; i++)
            {
                tcpClient             = new TcpClient();     //每次发送建立一个TcpClient类对象
                stateObject           = new StateObject();   ////每次发送建立一个StateObject类对象
                stateObject.tcpClient = tcpClient;
                //stateObject.buffer = SendMsg;
                //ip = myIPAndPorts[i].friendID; //所选好友IP地址字符串
                ip = LoginIP;
                //port = int.Parse(myIPAndPorts[i].friendName); //所选字符串好友端口号转换为数字
                port = int.Parse(LoginPort);
                stateObject.friendIDAndName = ip + ":" + port.ToString();                                               //所选好友IP和端口号
                chatData           = new IMClassLibrary.SingleChatDataPackage(UserID, IPAndPort, SendMessageTextBox.Text, UserID, myIPAndPorts[i].friendID);
                stateObject.buffer = chatData.DataPackageToBytes();                                                     //buffer为发送的数据包的字节数组
                tcpClient.BeginConnect(ip, port, new AsyncCallback(SentCallBackF), stateObject);                        //异步连接
            }                                                                                                           //给选定所有好友发信息
            FriendListBox.Items.Add(UserID + "(本机)(" + chatData.sendTime.ToString() + ")说:" + SendMessageTextBox.Text); //显示已发送的信息
        }
Example #5
0
        ArrayList list = new ArrayList();         //消息列表

        private void button_SendMessage_Click(object sender, RoutedEventArgs e)
        {
            IMClassLibrary.SingleChatDataPackage singleChatDataPackage = new IMClassLibrary.SingleChatDataPackage(UserID, "wuxia", ChatBox.Text); //初始化单人聊天数据包
            Byte[] sendBytes = singleChatDataPackage.DataPackageToBytes();                                                                        //单人聊天数据包转化为字节数组
        }