Winfrom 基于TCP的Socket 编程
基于TCP的Socket基础例子
服务端的代码
1 public partial class Form1 : Form 2 { 3 public Form1() 4 { 5 InitializeComponent(); 6 } 7 Socket clientSocket = null; 8 int tiaoshu = 1; 9 10 private void Form1_Load(object sender, EventArgs e) 11 { 12 try 13 { 14 this.txtPort.Text = "6666"; 15 this.txtSend.Text = "服务端发送"; 16 17 } 18 catch { } 19 } 20 21 //创建并监听 22 private void listen() 23 { 24 try 25 { 26 //获取服务器IP 27 // clientSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); 28 29 //因为用的一台机器测试所以服务器的IP设置成了127.0.0.1,这里填写的是服务端的IP地址如:192.168.1.110 30 IPAddress HostIp = IPAddress.Parse("127.0.0.1"); 31 32 //创建一个网络端点 33 IPEndPoint iep = new IPEndPoint(HostIp, Int32.Parse(this.txtPort.Text.Trim())); 34 35 //创建服务端服务端套接字 36 Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 37 38 //将套接字与网络端点绑定 39 serverSocket.Bind(iep); 40 41 serverSocket.Listen(10); 42 43 clientSocket = serverSocket.Accept(); 44 45 if(!clientSocket.Connected) 46 { 47 clientSocket=null; 48 } 49 if(clientSocket!=null) 50 { 51 tiaoshu = 1; 52 } 53 54 55 } 56 catch { } 57 58 } 59 Thread threadList; 60 // Socket clientSocket=null; 61 private void btnStart_Click(object sender, EventArgs e) 62 { 63 try 64 { 65 if (this.btnStart.Text == "启动服务") 66 { 67 68 this.btnStart.Text = "停止服务"; 69 70 threadList = new Thread(new ThreadStart(listen)); 71 72 threadList.Start(); 73 74 timer1.Enabled = true; 75 76 //this.lblState.Text = "连接成功!"; 77 } 78 else { 79 this.btnStart.Text = "启动服务"; 80 81 timer1.Enabled = false; 82 83 threadList.Abort(); 84 clientSocket.Shutdown(SocketShutdown.Both); 85 clientSocket.Close(); 86 87 //this.lblState.Text = "连接失败!"; 88 89 90 } 91 } 92 catch { } 93 } 94 95 private void timer1_Tick(object sender, EventArgs e) 96 { 97 try 98 { 99 if (clientSocket != null) 100 { 101 if (clientSocket.Connected) 102 { 103 this.lblState.Text = "连接成功!"; 104 105 byte[] receiveBytes = new byte[1024]; 106 107 if (clientSocket.Poll(100, SelectMode.SelectRead)) 108 { 109 int successReceiveBytes = clientSocket.Receive(receiveBytes); 110 this.txtReceive.Text = "客户端" + clientSocket.RemoteEndPoint.ToString() 111 + "\r\n" + "接收" + System.Text.Encoding.UTF8.GetString(receiveBytes, 0, successReceiveBytes); 112 this.txtReceive.SelectionLength = txtReceive.Text.Length; 113 this.txtReceive.ScrollToCaret(); 114 } 115 116 } 117 else 118 { 119 this.lblState.Text = "连接失败!"; 120 } 121 } 122 123 } 124 catch { } 125 } 126 127 private void btnSend_Click(object sender, EventArgs e) 128 { 129 try 130 { 131 if(clientSocket==null) 132 { 133 this.lblState.Text = "连接失败"; 134 }else 135 { 136 string sendMessage = this.txtSend.Text.Trim(); 137 byte[] message = System.Text.Encoding.UTF8.GetBytes(sendMessage); 138 int successSendMessage = clientSocket.Send(message,message.Length,SocketFlags.None); 139 140 } 141 142 } 143 catch { } 144 } 145 }
客户端的代码
1 public partial class Form1 : Form 2 { 3 public Form1() 4 { 5 InitializeComponent(); 6 } 7 8 private void Form1_Load(object sender, EventArgs e) 9 { 10 this.txtIP.Text = "127.0.0.1";//服务端的IP地址,这里因为是服务端是本机写127.0.0.1,如服务端是192.168.1.110,这里应该写192.168.1.110 11 this.txtPort.Text = "6666";//服务端发布的端口号 12 this.txtSend.Text = "客户端发送"; 13 } 14 15 Socket clientSocket; 16 17 private void btnStart_Click(object sender, EventArgs e) 18 { 19 try 20 { 21 IPAddress ip = IPAddress.Parse(this.txtIP.Text.Trim()); 22 23 IPEndPoint iep = new IPEndPoint(ip,int.Parse(this.txtPort.Text.Trim())); 24 25 clientSocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp); 26 27 clientSocket.Connect(iep); 28 29 clientSocket.ReceiveTimeout = 3000; 30 31 clientSocket.SendTimeout = 3000; 32 33 this.lblState.Text = "连接成功"; 34 35 timer1.Enabled = true; 36 37 } 38 catch { 39 } 40 } 41 42 private void timer1_Tick(object sender, EventArgs e) 43 { 44 try 45 { 46 if (clientSocket != null) 47 { 48 if (clientSocket.Connected) 49 { 50 byte[] receiveSocket=new byte[1024]; 51 if(clientSocket.Poll(100,SelectMode.SelectRead)) 52 { 53 int successSocket = clientSocket.Receive(receiveSocket); 54 if(successSocket!=0) 55 { 56 this.txtReceive.Text = "服务端" + System.Text.Encoding.UTF8.GetString(receiveSocket,0,successSocket); 57 this.txtReceive.SelectionStart = this.txtReceive.Text.Length; 58 this.txtReceive.ScrollToCaret(); 59 } 60 } 61 } 62 } 63 else { 64 this.lblState.Text = "连接失败!"; 65 } 66 67 68 } 69 catch { } 70 } 71 72 private void btnSend_Click(object sender, EventArgs e) 73 { 74 try 75 { 76 if(clientSocket!=null) 77 { 78 if(clientSocket.Connected) 79 { 80 string sendMessage = this.txtSend.Text.Trim(); 81 byte[] sendBytes = System.Text.Encoding.UTF8.GetBytes(sendMessage); 82 int successSendBytes = clientSocket.Send(sendBytes,sendBytes.Length,SocketFlags.None); 83 84 } 85 86 }else{ 87 this.lblState.Text="连接失败!"; 88 } 89 90 } 91 catch { } 92 } 93 }
展示效果如下