www.pudn.com > tcpip.rar > Form1.cs, change:2011-09-12,size:5991b


using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
using System.Runtime.InteropServices; 
using System.Net.Sockets; 
using System.Net; 
using System.Threading; 
namespace tcpip 
{ 
 
    public partial class Form1 : Form 
    { 
        private System.ComponentModel.Container components = null; 
        private bool check = true; 
        private TcpClient tclient; 
        private NetworkStream netStream; 
 
        public Form1() 
        { 
            InitializeComponent(); 
        } 
 
 
 
        private void Form1_Load(object sender, EventArgs e) 
        { 
             
 
        } 
        private void ReceMsg() 
        { 
            try 
            { 
                while (check) 
                { 
 
                    netStream = tclient.GetStream(); 
 
                    byte[] read = new byte[1024]; 
                    netStream.Read(read, 0, read.Length); 
                    //n = System.Convert.ToInt32(netStream.Length); 
                    //byte[] read1 = new byte[n]; 
                    //read1 = read; 
 
 
 
                    //string Recemsg = Encoding.Unicode.GetBytes(read ); 
                    // string Recemsg1 = Encoding.ASCII.GetString(read, 0, read.Length); 
                    this.Invoke((EventHandler)(delegate 
                 { 
                     //this.richTextBoxout.Text += Recemsg; 
                     //this.textBox1.Text += Recemsg1; 
                     string s100 = byteToHexStr(read);//字节数组转为十六进制字符串 
                     //string s1 = s100.Replace("0",""); 
                     int s1 = s100.IndexOf("0000"); 
                     string s2 = s100.Substring(0, s1); 
                     this.textBox2.Text += s2; 
 
                 })); 
 
                } 
            } 
            catch 
            { 
                MessageBox.Show("服务器连接关闭"); 
            } 
        } 
        public static string byteToHexStr(byte[] bytes)//字节数组转为十六进制字符串 
        { 
            string returnStr = ""; 
            if (bytes != null) 
            { 
                for (int i = 0; i < bytes.Length; i++) 
                { 
                    returnStr += bytes[i].ToString("X2"); 
                } 
            } 
            return returnStr; 
        } 
         
 
         
 
        private void button1_Click(object sender, EventArgs e) 
        { 
            
            try 
            { 
                IPAddress ipRemote = IPAddress.Parse(this.textBoxip.Text.Trim()); //ip地址 
                 
 
                int port=System.Convert.ToInt32(this.textBoxport.Text);//端口号 
                IPEndPoint iep = new IPEndPoint(ipRemote,port); //连接服务器 
                tclient = new TcpClient(); 
                 
                //tclient.Connect(this.textBoxip.Text.Trim(),port); 
                tclient.Connect(iep);//连接 
               // tclient.Connect("192.192.1.45",port); 
                //tclient.Connect("192.192.1.45",6666); 
                 
 
                Thread threadread = new Thread(new ThreadStart(ReceMsg)); 
                threadread.Start(); 
                this.butConn.Enabled = false; 
                this.butDisconn.Enabled = true; 
                this.butSendMsg.Enabled = true; 
                MessageBox.Show("连接成功!"); 
                
            } 
            catch 
            { 
                MessageBox.Show("无法连接!"); 
            } 
            finally 
            { } 
                
         } 
 
        private void button2_Click(object sender, EventArgs e)//停止连接 
        { 
            try 
            { 
                tclient.Close(); 
            } 
            catch (Exception ex) 
            { 
                MessageBox.Show(ex.Message); 
            } 
            this.butConn.Enabled = true; 
            this.butDisconn.Enabled = false; 
            this.butSendMsg.Enabled = false; 
 
 
        } 
 
        private void button3_Click(object sender, EventArgs e)//发送 
        { 
            byte[] write = new byte[1024]; 
            netStream = tclient.GetStream(); 
            string Sendmsg = this.richTextBoxinput.Text; 
            //write = Encoding.Unicode.GetBytes(Sendmsg.ToCharArray()); 
            write = strToToHexByte(Sendmsg); 
            netStream.Write(write,0,write.Length); 
            netStream.Flush(); 
 
        } 
        public static string ByteArrayToHexString(byte[] data)//字节数组转为十六进制字符串 
        { 
            StringBuilder sb = new StringBuilder(data.Length * 3); 
            foreach (byte b in data) 
                sb.Append(Convert.ToString(b, 16).PadLeft(2, '0').PadRight(3, ' ')); 
            //sb.Append(b.ToString("2X")+ ""); 
            return sb.ToString().ToUpper(); 
        } 
 
        private void button4_Click(object sender, EventArgs e) 
        { 
            this.textBox2.Text = ""; 
        } 
        private static byte[] strToToHexByte(string hexString)//字符串转化成十六进制字节数组 
        { 
            hexString = hexString.Replace(" ", ""); 
            if ((hexString.Length % 2) != 0) 
                hexString += " "; 
            byte[] returnBytes = new byte[hexString.Length / 2]; 
            for (int i = 0; i < returnBytes.Length; i++) 
                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16); 
            return returnBytes; 
        } 
 
        private void button1_Click_1(object sender, EventArgs e) 
        { 
            this.richTextBoxinput.Text = ""; 
        } 
 
        private void textBoxip_TextChanged(object sender, EventArgs e) 
        { 
 
        } 
 
        private void textBoxport_TextChanged(object sender, EventArgs e) 
        { 
 
        } 
 
 
 
        } 
        
 
 
 
 
  }