C# TCP socket发送大数据包时,接收端和发送端数据不一致 服务端接收Receive不完全
服务器端:
port为监听端口
byte[] bs 一般声明一个较大的数组接收
int ilen 为接收到的数据包长度,根据它从byte[] bs里截取就好
IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port); Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); listener.Bind(localEndPoint); listener.Listen(30); while (true) { Socket sock = listener.Accept(); byte[] bs = new byte[1024*1024]; int ilen = sock.Receive(bs); }
客户端:
很简单,连接成功后发送数据就好。
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(ip), iPort);//IP和端口 Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); sock.Connect(ipep); sock.Send(Sendbytes, Sendbytes.Length, SocketFlags.None);//发送信息