一、服务端代码
using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Net.Sockets;using System.Text;using System.Threading;using System.Threading.Tasks;using System.Windows.Forms;namespace 网络编程.SockteHandel{/// <summary>/// 服务端处理类/// </summary>publicclassServerHandle{/// <summary>/// 端口号/// </summary>publicstaticstringPoint{ get;set;}/// <summary>/// IP地址/// </summary>publicstaticstringIpSite{ get;set;}/// <summary>/// 服务端信息/// </summary>publicstaticTextBoxServerMsg{ get;set;}/// <summary>/// 接收到客户端的消息/// </summary>publicstaticTextBoxReceiveMsg{ get;set;}/// <summary>/// 发送消息/// </summary>publicstaticTextBoxSendMessage{ get;set;}/// <summary>/// 负责通信的Socket/// </summary>privateSocketConnectionSocket;/// <summary>/// 监听端口/// </summary>publicvoidWatchPort(){//在服务器端创建一个负责监听ID跟端口号 的SocketSocket socketWatch =newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);//IPAddress ip = IPAddress.Any; //监听IPAddress ip =IPAddress.Parse(IpSite);//创建端口号对象IPEndPoint pointObj =newIPEndPoint(ip,Convert.ToInt32(Point));//监听socketWatch.Bind(pointObj);ServerMsg.Text+="监听成功······ ";//设定最多十个排队连接请求socketWatch.Listen(10);//开启一个新线程Thread thread =newThread(Listen);thread.IsBackground=true;thread.Start(socketWatch);}/// <summary>/// 等待客户端连接,并且创建与之通信的Socket/// </summary>/// <param name="obj"></param>voidListen(object obj){Socket soketlisten = obj asSocket;while(true){//等待客户端连接,并创建一个负责通信的SocketSocket socketSend = soketlisten.Accept();ServerMsg.Text+="连接成功·······"+ socketSend.RemoteEndPoint.ToString()+" ";//开启一个新线程不停接收客户端发来的消息Thread reciveThread =newThread(Recive);reciveThread.IsBackground=true;reciveThread.Start(socketSend);}}/// <summary>/// 服务器端不停接收客户端发来的消息/// </summary>/// <param name="obj"></param>voidRecive(object obj){//接收消息ConnectionSocket= obj asSocket;if(ConnectionSocket==null){return;}while(true){//接收客户端发来信息byte[] buffer =newbyte[1024*1024*2];//实际接收的有效字节数int receiveIndex =ConnectionSocket.Receive(buffer);//以实际接收有效字节数来判断客户端是否下线了if(receiveIndex ==0){break;}string str =Encoding.UTF8.GetString(buffer,0, buffer.Length);ReceiveMsg.Text+= str +" ";}}/// <summary>/// 服务端发送消息/// </summary>publicvoidServerSendMessage(){byte[] buffer =Encoding.UTF8.GetBytes(SendMessage.Text);int connectionIndex =ConnectionSocket.Send(buffer);}}}
二、客户端代码
using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Net.Sockets;using System.Text;using System.Threading;using System.Threading.Tasks;using System.Windows.Forms;namespace 网络编程.SockteHandle{/// <summary>/// 客户端/// </summary>publicclassClientHandle{/// <summary>/// IP地址/// </summary>publicstaticstringConnectionIp{ get;set;}/// <summary>/// 端口号/// </summary>publicstaticstringPoint{ get;set;}//发送消息publicTextBoxSendMsg{ get;set;}/// <summary>/// 接收消息/// </summary>publicTextBoxReciveMsg{ get;set;}/// <summary>/// 客户端Socket对象/// </summary>publicSocket socketSend =null;/// <summary>/// 创建负责通信的Socket/// </summary>publicvoidCreateClientSocket(){socketSend =newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);IPAddress ip =IPAddress.Parse(ConnectionIp);IPEndPoint endPoint =newIPEndPoint(ip,Convert.ToInt32(Point));socketSend.Connect(endPoint);ReciveMsg.Text+="连接到服务器";//创建一个线程来接收服务器端数据Thread reciveThread =newThread(ReciveServerMessage);reciveThread.IsBackground=true;reciveThread.Start(socketSend);}/// <summary>/// 接收服务端信息/// </summary>voidReciveServerMessage(object obj){Socket reciveSocket = obj asSocket;while(true){byte[] buffer =newbyte[1024*1024*2];int reciveIndex = reciveSocket.Receive(buffer);if(reciveIndex ==0){break;}ReciveMsg.Text+=" "+Encoding.UTF8.GetString(buffer)+" ";}}/// <summary>/// 发送消息/// </summary>publicvoidSendMessage(){byte[] buffer =Encoding.UTF8.GetBytes(SendMsg.Text);int sendIndex = socketSend.Send(buffer);}}}