C# socket 实现客户端连续发送数据
服务器端:
namespace SocketTest { class Program { public static void SendMessage() { Socket socket = serverSocket.Accept(); Console.WriteLine("Connected a client:{0}",socket.RemoteEndPoint); socket.Send(Encoding.ASCII.GetBytes("welcome to server")); //Thread thread = new Thread(ReceiveMessage); // thread.Start(); } public static void ReceiveMessage(object obj) { Socket socket = (Socket)obj; byte[] data = new byte[1024]; int len = socket.Receive(data); string dataString = Encoding.ASCII.GetString(data, 0, len); Console.WriteLine("Receive Data:{0} from {1}", dataString,socket.RemoteEndPoint); //Thread thread = new Thread(SendMessage); //thread.Start(socket); } static Socket serverSocket; static void Main(string[] args) { //定义接收数据长度变量 int recv; //定义接收数据的缓存 byte[] data = new byte[1024]; //定义侦听端口 IPEndPoint ipEnd = new IPEndPoint(IPAddress.Any, 5566); //定义套接字类型 serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //连接 serverSocket.Bind(ipEnd); //开始侦听 serverSocket.Listen(10); //控制台输出侦听状态 Console.Write("Waiting for a client"); //Socket client; while (true) { //一旦接受连接,创建一个客户端 Socket client = serverSocket.Accept(); //获取客户端的IP和端口 IPEndPoint ipEndClient = (IPEndPoint)client.RemoteEndPoint; //输出客户端的IP和端口 Console.WriteLine("Connect with {0} at port {1}", ipEndClient.Address, ipEndClient.Port); //定义待发送字符 string welcome = "Welcome to my server"; //数据类型转换 data = Encoding.ASCII.GetBytes(welcome); while (true) { try { //发送 client.Send(data, data.Length, SocketFlags.None); //接收数据可以用线程也可以不用 //ReceiveMessage(client); Thread thread = new Thread(ReceiveMessage); thread.Start(client); ////对data清零 //data = new byte[1024]; ////获取收到的数据的长度 //recv = client.Receive(data); ////如果收到的数据长度为0,则退出 //if (recv == 0) // break; ////输出接收到的数据 //Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv)); //将接收到的数据再发送出去 // client.Send(data, recv, SocketFlags.None); } catch (Exception) { client.Close(); serverSocket.Close(); } } client.Close(); } //Console.WriteLine("Disconnect form{0}", ipEndClient.Address); serverSocket.Close(); } } }
客户端:
namespace Client { class Program { public static void sendMessage(object obj) { Socket socket = (Socket)obj; string input = Console.ReadLine(); byte[] data = Encoding.ASCII.GetBytes(input); socket.Send(data, data.Length, SocketFlags.None); //Thread thread = new Thread(new ParameterizedThreadStart(ReceiveMessage)); //thread.Start(socket); } public static void ReceiveMessage(object obj) { Socket socket = (Socket)obj; byte[] data = new byte[1024]; int len = socket.Receive(data); string dataString = Encoding.ASCII.GetString(data, 0, len); Console.WriteLine("Receive Data:{0} from {1}", dataString, socket.RemoteEndPoint); //Thread thread = new Thread(new ParameterizedThreadStart(SendMessage)); //thread.Start(socket); } static void Main(string[] args) { //定义发送数据缓存 byte[] data = new byte[1024]; //定义字符串,用于控制台输出或输入 string input, stringData; //定义主机的IP及端口 IPAddress ip = IPAddress.Parse("127.0.0.1"); IPEndPoint ipEnd = new IPEndPoint(ip, 5566); //定义套接字类型 Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //尝试连接 try { socket.Connect(ipEnd); } //异常处理 catch (SocketException e) { Console.WriteLine("Fail to connect server"); Console.WriteLine(e.ToString()); return; } //定义接收数据的长度 int recv = socket.Receive(data); //将接收的数据转换成字符串 stringData = Encoding.ASCII.GetString(data, 0, recv); //控制台输出接收到的数据 Console.WriteLine(stringData); while(true) { //Thread thread = new Thread(sendMessage); //thread.Start(socket); //定义从键盘接收到的字符串 input = Console.ReadLine(); if (input == "exit") { break; } //将从键盘获取的字符串转换成整型数据并存储在数组中 data = Encoding.ASCII.GetBytes(input); //发送该数组 socket.Send(data, data.Length, SocketFlags.None); ////如果字符串是"exit",退出while循环 //if (input == "exit") //{ // break; //} ////对data清零 //data = new byte[1024]; ////定义接收到的数据的长度 //recv = socket.Receive(data); ////将接收到的数据转换为字符串 //stringData = Encoding.ASCII.GetString(data, 0, recv); ////控制台输出字符串 //Console.Write(stringData); ////发送收到的数据 //socket.Send(data, recv, 0); } Console.WriteLine("disconnect from server"); socket.Shutdown(SocketShutdown.Both); socket.Close(); } } }