1.创建2个控制台程序
2.服务端代码
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; namespace 服务端 { class Program { private static byte[] result = new byte[1024]; private static int myProt = 8889; //端口 static Socket serverSocket; static void Main(string[] args) { //IPAddress ip = IPAddress.Parse("127.0.0.1"); IPAddress ip = IPAddress.Parse("192.168.187.1"); serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); serverSocket.Bind(new IPEndPoint(ip, myProt));//绑定IP地址:端口 serverSocket.Listen(10); //设定最多10个排队连接请求 Console.WriteLine("启动监听{0}成功", serverSocket.LocalEndPoint.ToString()); //通过Clientsoket发送数据 Thread myThread = new Thread(ListenClientConnect); myThread.Start(); Console.ReadLine(); } /// <summary> /// 监听客户端连接 /// </summary> private static void ListenClientConnect() { while (true) { Socket clientSocket = serverSocket.Accept(); //Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); Thread receiveThread = new Thread(ReceiveMessage); clientSocket.Send(Encoding.GetEncoding("GBK").GetBytes("00000124SX27|1529")); receiveThread.Start(clientSocket); } } /// <summary> /// 接收消息 /// </summary> /// <param name="clientSocket"></param> private static void ReceiveMessage(object clientSocket) { Socket myClientSocket = (Socket)clientSocket; while (true) { try { //通过clientSocket接收数据 int receiveNumber = myClientSocket.Receive(result); Console.WriteLine("接收客户端{0}消息{1}", myClientSocket.RemoteEndPoint.ToString(), Encoding.GetEncoding("GBK").GetString(result, 0, receiveNumber)); Console.WriteLine("返回数据"); } catch (Exception ex) { Console.WriteLine(ex.Message); myClientSocket.Shutdown(SocketShutdown.Both); myClientSocket.Close(); break; } } } } }
3.客户端代码
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading.Tasks; namespace 客户端 { class Program { static void Main(string[] args) { while (true) { getServerMessage(Console.ReadLine()); //Console.ReadLine(); } } private static void getServerMessage(string customerId) { //设定服务器IP地址 //var serverIP = "127.0.0.1"; var serverIP = "192.168.187.1"; //var serverPort = "8888"; IPAddress ip = IPAddress.Parse(serverIP); Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { //服务器连接成功 clientSocket.Connect(new IPEndPoint(ip, 8889)); //配置服务器IP与端口 } catch { Console.WriteLine("连接服务器失败"); //return "连接服务器失败!"; } var recStr = ""; try { //发送数据 var sendCustomerId = customerId; byte[] sendBytes = Encoding.UTF8.GetBytes(sendCustomerId); clientSocket.Send(sendBytes); //接收数据, 注意编码格式 byte[] resBytes = new byte[1024]; int bytes = clientSocket.Receive(resBytes, resBytes.Length, 0); recStr += Encoding.GetEncoding("GBK").GetString(resBytes, 0, bytes); } catch { clientSocket.Shutdown(SocketShutdown.Both); clientSocket.Close(); recStr = "接收消息失败"; } Console.WriteLine(recStr); //返回数据 //return recStr; } } }