服务端:
/// <summary> /// 清理所有正在使用的资源。 /// </summary> /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) { try { s.Close(); th.Abort(); } catch { } if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } /*********************************************************************** * 服务端: * (1)用指定的端口和IP建立EndPoint对象 * (2)建立一个Socket对象 * (3)用Socket对象的Bind()方法绑定IPEndPoint * (4)用Socket对象的Listen()方法开始监听 * (5)接收到客户端的连接,用Socket对象的Accept()方法创建新的Socket对象和请求的客户端进行通信 * (6)通信结束后,关闭Socket对象 ************************************************************************/ using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Net; using System.Net.Sockets; using System.Threading; namespace 同步 { public partial class Server : Form { string recvStr = ""; Socket s; string host; Thread th; public Server() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { th = new Thread(new ThreadStart(Receive)); th.Start(); } private string GetIP() { IPHostEntry ipe = Dns.GetHostByName(Dns.GetHostName()); return ipe.AddressList[0].ToString(); } private void Receive() { if ("" == textBox1.Text) { return; } int port = int.Parse(textBox2.Text.Trim()); host = textBox1.Text.Trim(); IPAddress ip = IPAddress.Parse(host);//把IP地址字符串转换为IPAdderess类型 IPEndPoint ipe = new IPEndPoint(ip, port);//用IP和port初始化IPEndPoint类的新实例 //创建一个socket对像,如果用udp协议,则要用SocketType.Dgram类型的套接字 s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); s.Bind(ipe);//为新建的Socket实例绑定IPEndPonit对象 while (true) { try { s.Listen(5);//开始监听 Socket temp = s.Accept();//为新建的连接创建Socket对象 byte[] recvBytes = new byte[1024]; int bytes; bytes = temp.Receive(recvBytes, recvBytes.Length, 0);//从客户端接收信息 recvStr = Encoding.ASCII.GetString(recvBytes, 0, bytes);//把byte[]转换成字符串 string msg = DateTime.Now.ToShortTimeString(); msg += "server get message from" + temp.RemoteEndPoint.ToString(); msg +="Message:"+ recvStr; //textBox3.Text = msg; listBox1.Items.Add(msg); string sendStr = "ok!Client send message successful"; byte[] bs = Encoding.ASCII.GetBytes(sendStr);//把字符串转换成byte[] temp.Send(bs, bs.Length, 0);//返回信息给客户端 temp.Close(); } catch (System.Exception ex) { this.label4.Text =ex.Message .ToString (); } } } private void Server_Load(object sender, EventArgs e) { textBox1.Text = GetIP(); } } }
客户端:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Net; using System.Net.Sockets; namespace Clinet { public partial class Clinet : Form { public Clinet() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Send(); } private void Send() { int port = int.Parse(this.txtPort.Text.Trim()); string host = this.txtIP.Text.Trim(); IPAddress ip = IPAddress.Parse(host);//将Ip地址由字符串转换为IPAddress类型 IPEndPoint ipe = new IPEndPoint(ip, port);//用指定的IP和端口初始化IPEndPoint类的新实例 //建立Socket对象 Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); c.Connect(ipe);//连接服务器 string sendStr = ""; sendStr = textBox3.Text.Trim (); byte[] bs = Encoding.ASCII.GetBytes(sendStr);//把字符串转换为byte类型 c.Send(bs, bs.Length, 0);//向服务器发送信息 string recvStr = ""; byte[] recvBytes = new byte[1024]; int bytes; bytes = c.Receive(recvBytes, recvBytes.Length, 0);//接收服务器返回的信息 recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes); this.label4.Text = "Clinet get message:" + recvStr; c.Close(); } } }