服务器端的代码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net; using System.Net.Sockets; using System.Threading; using System.IO; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void tbPortNum_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar > 31) if (e.KeyChar < 48 || e.KeyChar > 57) { MessageBox.Show("输入非法字符!", "警告!"); tbPortNum.Text = ""; } if (tbPortNum.Text != "") { btCreServer.Enabled = true; } } // private void bgWorker_DoWork(object sender, DoWorkEventArgs e) //{ //} //获取本机的IP //获取本机的IP public Boolean SocketIsConnected(Socket soc) { Boolean isconnected = false; try { soc.Send(System.Text.Encoding.Default.GetBytes("hello!")); isconnected = true; } catch { isconnected = false; } return isconnected; } public string GetLocalIP() { IPAddress[] AddressList = Dns.GetHostAddresses(Dns.GetHostName()); return AddressList[1].ToString(); } Socket sock; //定义一个Socket类的对象 (默认为protected) Thread th; //定义一个Thread类的对象 Socket newSock; //BackgroundWorker bgw = new BackgroundWorker(); private void BeginListen() //Socket监听函数, 等下作为创建新线程的参数 { IPAddress serverIp = IPAddress.Parse(GetLocalIP()); //调用本类静态函数GetLocalIP得到本机IPAddress. IPEndPoint iep = new IPEndPoint(serverIp, Convert.ToInt32(tbPortNum.Text)); //本地终结点 sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //实例化内成员sock // Boolean BuildSocketAccept=false ; // bgw.DoWork += new DoWorkEventHandler(bgWorker_DoWork); //bgw.RunWorkerAsync(bs); //this.label1.Text = iep.ToString(); sock.Bind(iep); //Socket类的一个重要函数, 绑定一个IP, while (true) //这里弄了个死循环来监听端口, 有人会问死循环了,那程序不卡住了, 注意这只是个类, 这里还没有main函数呢. { try { sock.Listen(10); //好了,sock绑定了本地终结点就可以开始监听了,5表示最大连接数为5 newSock = sock.Accept(); //这里又有Socket类的一个重要的方法:Accept, 该方法接受来自外面的Socket连接请求, 并返回一个Socket套接字, 这个套接字就开始处理这一个client与Server之间的对话 BackgroundWorker bgwClintIP = new BackgroundWorker(); bgwClintIP.DoWork += new DoWorkEventHandler(bgWorker_DoWorkClintIP); //bgwClintIP.RunWorkerAsync(System.Text.Encoding.Default.GetBytes(newSock.RemoteEndPoint.ToString())); if (!SocketIsConnected(newSock)) { MessageBox.Show("连接已断开!"); break; } while (true) { try { Byte[] byteMessage = new Byte[100]; //存放消息的字节数组缓冲区, 注意数组表示方法,和C不同的. newSock.Receive(byteMessage); //接受client发送过来的数据保存到缓冲区. BackgroundWorker bgw = new BackgroundWorker(); bgw.DoWork += new DoWorkEventHandler(bgWorker_DoWork); bgw.RunWorkerAsync(byteMessage); } catch { } } } catch (SocketException se) //捕捉异常, { MessageBox.Show(se.ToString()); //lbState.Text = se.ToString(); //将其显示出来, 在此亦可以自定义错误. } } } private void bgWorker_DoWorkClintIP(object sender, DoWorkEventArgs e) { byte[] bs = (byte[])e.Argument; string r1 = Encoding.Default.GetString(bs); //RevDataList.BeginInvoke(new System.EventHandler(ShowGetByte), r1); this.BeginInvoke(new System.EventHandler(AddClintIPPage), r1); } private void AddClintIPPage(object o, System.EventArgs e) { lbClintIP.Text = o.ToString(); lbState.Text = "已建立连接"; tbRecMSG.AppendText(" " + "建立连接:" + o.ToString()); } private void bgWorker_DoWork(object sender, DoWorkEventArgs e) { byte[] bs = (byte[])e.Argument; string r1 = Encoding.Default.GetString(bs); //RevDataList.BeginInvoke(new System.EventHandler(ShowGetByte), r1); this.BeginInvoke(new System.EventHandler(ShowGetByte), r1); } private void ShowGetByte(object o, System.EventArgs e) { tbRecMSG.AppendText(" " + lbClintIP.Text + "客户端:" + o.ToString()); } private void btCreServer_Click(object sender, EventArgs e) { if (btCreServer.Text == "设立服务器") { btCreServer.Text = "关闭服务器"; tbPortNum.Enabled = false; lbLocalIP.Text = "本地IP:" + GetLocalIP(); try { th = new Thread(new ThreadStart(BeginListen)); //创建一个新的线程专门用于处理监听,这句话可以分开写的,比如: ThreadStart ts=new ThreadStart(BeginListen); th=new Thread (ts); 不过要注意, ThreadStart的构造函数的参数一定要是无参数的函数. 在此函数名其实就是其指针, 这里是委托吗? th.Start(); //启动线程 lbState.Text = "状态:正在监听"; } catch (SocketException se) //处理异常 { MessageBox.Show(se.Message, "出现问题", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (ArgumentNullException ae) //参数为空异常 { lbState.Text = "参数错误"; MessageBox.Show(ae.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } else { btCreServer.Text = "建立服务器"; tbPortNum.Enabled = true; sock.Close(); //关闭套接字 th.Abort(); //终止监听线程 lbState.Text = "状态:停止监听"; } } private void btSendMSG_Click(object sender, EventArgs e) { if (newSock == null) MessageBox.Show("尚未建立连接,发送失败!"); else if (tbSendMSG.Text == "") MessageBox.Show("输入为空,发送失败!"); else { try { newSock.Send(System.Text.Encoding.Default.GetBytes(tbSendMSG.Text)); tbRecMSG.AppendText(" " + "服务器端:" + tbSendMSG.Text); } catch (ArgumentNullException ae) { MessageBox.Show(ae.Message, "参数为空", MessageBoxButtons.OKCancel, MessageBoxIcon.Information); } catch (SocketException se) { MessageBox.Show(se.Message, "出现问题", MessageBoxButtons.OK, MessageBoxIcon.Information); } } tbSendMSG.Text = ""; } private void tbPortNum_TextChanged(object sender, EventArgs e) { } private void tbRecMSG_TextChanged(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { tbRecMSG.Text = ""; } private void Form1_Load(object sender, EventArgs e) { } } }
客户端的代码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net.Sockets; using System.Threading; using System.Net; namespace WindowsFormsApplication2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public Boolean SocketIsConnected(Socket soc) { Boolean isconnected = false; try { soc.Send(System.Text.Encoding.Default.GetBytes("$")); isconnected = true; } catch { isconnected = false; } return isconnected; } private Socket sock; //定义一个Socket类的对象 (默认为protected) Thread th; //定义一个Thread类的对象 //Socket newSock; private void BeginListen() //Socket监听函数, 等下作为创建新线程的参数 { int Port = int.Parse(textPort.Text); IPAddress ip = IPAddress.Parse(textIp.Text); //调用本类静态函数GetLocalIP得到本机IPAddress. IPEndPoint ipp = new IPEndPoint(ip, Port); //IPEndPoint iep = new IPEndPoint(serverIp, Convert.ToInt32(tbPortNum.Text)); //本地终结点 sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //实例化内成员sock sock.Connect(ipp); //连接服务器 // if (!SocketIsConnected(sock)) //{ // lbState.Text="连接已断开!"; // return; //} //else //{ // //lbState.Text = "也连接到服务器!"; //} do { Byte[] byteMessage = new Byte[100]; //存放消息的字节数组缓冲区, 注意数组表示方法,和C不同的. sock.Receive(byteMessage); //接受Server发送过来的数据保存到缓冲区. BackgroundWorker bgw = new BackgroundWorker(); bgw.DoWork += new DoWorkEventHandler(bgWorker_DoWork); bgw.RunWorkerAsync(byteMessage); }while(true); } private void bgWorker_DoWork(object sender, DoWorkEventArgs e) { byte[] bs = (byte[])e.Argument; string r1 = Encoding.Default.GetString(bs); //RevDataList.BeginInvoke(new System.EventHandler(ShowGetByte), r1); this.BeginInvoke(new System.EventHandler(ShowGetByte), r1); } private void ShowGetByte(object o, System.EventArgs e) { //textRec.AppendText(" " + lbClintIP.Text + 'Server:' + o.ToString()); textRec.AppendText(" " + "服务器端:" + o.ToString()); } //当点击连接服务器的时候 private void btConnectServer_Click(object sender, EventArgs e) { if (btConnectServer.Text == "连接服务器") { btConnectServer.Text = "断开连接"; textIp.Enabled = false; textPort.Enabled = false; //lbLocalIP.Text = "本地IP:" + GetLocalIP(); try { th = new Thread(new ThreadStart(BeginListen)); //创建一个新的线程专门用于处理监听,这句话可以分开写的,比如: ThreadStart ts=new ThreadStart(BeginListen); th=new Thread (ts); 不过要注意, ThreadStart的构造函数的参数一定要是无参数的函数. 在此函数名其实就是其指针, 这里是委托吗? th.Start(); //启动线程 } catch (SocketException se) //处理异常 { MessageBox.Show(se.Message, "出现问题", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (ArgumentNullException ae) //参数为空异常 { lbState.Text = "参数错误"; MessageBox.Show(ae.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } else { btConnectServer.Text = "连接服务器"; textIp.Enabled = true; textPort.Enabled = true; sock.Close(); //关闭套接字 th.Abort(); //终止监听线程 } } private void btSend_Click(object sender, EventArgs e) { if (sock == null) MessageBox.Show("尚未建立连接,发送失败!"); else if (textSend.Text == "") MessageBox.Show("输入为空,发送失败!"); else { try { sock.Send(System.Text.Encoding.Default.GetBytes(textSend.Text)); textRec.AppendText(" " + "客户端:" + textSend.Text); } catch (ArgumentNullException ae) { MessageBox.Show(ae.Message, "参数为空", MessageBoxButtons.OKCancel, MessageBoxIcon.Information); } catch (SocketException se) { MessageBox.Show(se.Message, "出现问题", MessageBoxButtons.OK, MessageBoxIcon.Information); } } textSend.Text = ""; } private void textIp_TextChanged(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { textRec.Text = ""; } } }