示例#1
0
        private void button8_Click(object sender, EventArgs e)
        {
            if (this.send_result_listView.Items.Count == 0)
            {
                MessageBox.Show("发送地址为空!");
                return;
            }

            foreach (ListViewItem item in this.send_result_listView.Items)
            {
                this.send_result_listView.BeginUpdate();
                IpAddr ip = new IpAddr();
                ip.IpAddrs = item.Text;
                ip.Port    = item.SubItems[1].Text;
                SocketUtil sktutil      = new SocketUtil();
                Socket     clientSocket = sktutil.getConnection(ip.IpAddrs, ip.Port);
                if (clientSocket != null)
                {
                    bool success = sktutil.sendMessage(clientSocket, this.message);
                    if (success)
                    {
                        string response = sktutil.receiveMessage(clientSocket);
                        //MessageBox.Show(response);

                        item.SubItems.Add("回应:" + response);
                    }
                    else
                    {
                        item.SubItems.Add("失败");
                    }

                    sktutil.disConnection(clientSocket);
                }
                else
                {
                    item.SubItems.Add("无法连接");
                }
                this.send_result_listView.EndUpdate();
            }
        }
示例#2
0
        private void button1_Click(object sender, EventArgs e)
        {
            if (this.connecionstate == false)
            {
                if (this.rec_ipBox1.Text.Equals(""))
                {
                    MessageBox.Show("请输入接收IP地址!");
                    return;
                }
                this.rec_ipAddress.IpAddrs = this.rec_ipBox1.Text;
                if (this.rec_port_textbox.Text.Equals(""))
                {
                    MessageBox.Show("请输入接收端口号!");
                    return;
                }
                this.rec_ipAddress.Port = this.rec_port_textbox.Text;

                this.statuslabel.Text = "正在连接";

                this.connect_btn.Enabled = false;

                backgroundWorker1.RunWorkerAsync(this.rec_ipAddress);
            }
            else
            {
                if (receive_socket != null)
                {
                    sktUtil.disConnection(receive_socket);
                    this.connect_btn.Text         = "连接并接收命令";
                    this.statuslabel.Text         = "未连接";
                    this.rec_ipBox1.Enabled       = true;
                    this.rec_port_textbox.Enabled = true;
                    this.connecionstate           = false;
                }
            }
        }