创建一个服务端
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void Button_Click(object sender, RoutedEventArgs e) {//创建TCP的服务端 Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); server.Bind(new IPEndPoint(IPAddress.Any, 10000));//IPAddress.Any-0000本机IP,10000为端口号 server.Listen(1000);//监听连接的数量 this.txtResult.AppendText("服务已经启动!"); Thread t = new Thread(Execute); t.IsBackground=true; t.Start(server); } //线程Execute private void Execute( Object obj) { Socket server =obj as Socket; while(true) { Socket client = server.Accept(); Thread thread = new Thread(SingleExecute); thread.IsBackground=true; thread.Start(client); } } //线程SingleExecute private void SingleExecute(Object obj) { Socket client = obj as Socket; //只做接收 while (true) { byte[] buffer_len = new byte[4]; int current_len = 0; do { byte[] temp = new byte[buffer_len.Length-current_len]; int count = client.Receive(temp);//接收到的数据传入temp中,返回收到的字节数count if (count > 0) { Array.Copy(temp,0,buffer_len, current_len, temp.Length);//把temp从索引0开始复制到buffer_len从current_len开始,长度为temp.Length current_len += count; } }while(current_len < buffer_len.Length);//确保收到包头(4个字节) int len = 0; len += (buffer_len[0] << 24); len += (buffer_len[1] << 16); len += (buffer_len[2] << 8); len += (buffer_len[3] ); try { byte[] data = new byte[len]; current_len = 0; do { byte[] buffer = new byte[data.Length - current_len]; int count = client.Receive(buffer); if (count > 0) { Array.Copy(buffer, 0, data, current_len, buffer.Length); current_len += count; } } while (current_len < data.Length); string msg = Encoding.UTF8.GetString(data); output(msg);//输出 client.Send(Encoding.UTF8.GetBytes("1234"));//数据发送到Socket } catch (Exception e) { MessageBox.Show(e.Message); } } } private delegate void outputDelegate(string msg); private void output(string msg) { this.txtResult.Dispatcher.Invoke(new outputDelegate(outputAction), msg); } private void outputAction(string msg) { this.txtResult.AppendText(msg); this.txtResult.AppendText(" \n "); } }
创建一个客户端
private void MenuItem_Click_3(object sender, RoutedEventArgs e) { Socket socket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp); socket.Connect(new IPEndPoint(IPAddress.Parse("192.168.0.39"),10000)); byte[] data=Encoding.UTF8.GetBytes("连接成功!");//The connection was successful,中文太大会导致算术溢出。 int len = data.Length; byte[] data_len= new byte[4]; data_len[0] = (byte)((len >>8*3) & 0xFF); data_len[1] = (byte)((len >>8*2) & 0xFF); data_len[2] = (byte)((len >>8*1) & 0xFF); data_len[3] = (byte)(len & 0xFF); byte[] data_all = new byte[len + 4]; Array.Copy(data_len,0,data_all, 0, data_len.Length);//把头部data_len复制到data_all Array.Copy(data, 0, data_all, 4, data.Length); socket.Send(data_all); byte[] buffer = new byte[1024]; int count = socket.Receive(buffer,SocketFlags.None); if (count > 0) { string msg = Encoding.UTF8.GetString(buffer, 0, count); System.Windows.MessageBox.Show(msg); socket.Send(buffer, 0, count, SocketFlags.None); } }
标签:Socket,buffer,len,TCP,new,byte,data,连接 From: https://www.cnblogs.com/lin-07/p/17661503.html