客户服务器套接字C#
我正在使用socket C#。 我已经使用socket实现了客户端服务器应用程序,但问题是客户端没有收到服务器发送的所有数据。
这是客户端应用程序代码。 我应该怎么做才能收到服务器发送的所有数据?
strRecieved = ""; Socket soc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9001); soc.Connect(endPoint); byte[] msgBuffer = Encoding.Default.GetBytes(path); soc.Send(msgBuffer, 0, msgBuffer.Length, 0); byte[] buffer = new byte[2000]; int rec = soc.Receive(buffer); strRecieved = String.Format(Encoding.Default.GetString(buffer));
首先。 如果您正在实现某种流function(tcp / udp / file),您应该考虑使用某种协议 。
什么是协议? 这只是流式传输数据时使用的方案。 例:
[4Bytes – length] [lengthBytes – message] [1Byte – 终止指标]
知道协议,您可以简单地读取所有传入的字节:
byte[] buffer = new byte[4]; stream.ReadBytes(buffer, 0, 4); // cast that to int and read the rest int packetLen = BitConverter.ToInt32(buffer, 0); buffer = new byte[packetLen]; stream.ReadBytes(buffer, 0, buffer.Length); // all bytes that was sent
请记住,在发送消息之前,必须减去长度中的4个字节。
编辑:
关于如何使用共享协议发送和接收数据的简单示例。
// sender.cs string _stringToSend = "some fancy string"; byte[] encodedString = Encoding.UTF8.GetBytes(_stringToSend); List buffer = new List (); buffer.AddRange(BitConverter.GetBytes(encodedString.Length)); buffer.AddRange(encodedString); netStream.WriteBytes(buffer.ToArray(), 0, buffer.Count); // netStream sent message in protocol [@LEN - 4Bytes][@MSG - @LENBytes] // simply speaking something like: 5ABCDE // receiver.cs byte[] buffer = new byte[sizeof(int)]; netStream.ReadBytes(buffer, 0, buffer.Length); // receiver got the length of the message eg. 5 int dataLen = BitConverter.ToInt32(buffer, 0); buffer = new byte[dataLen]; // now we can read an actual message because we know it's length netStream.ReadBytes(buffer, 0, buffer.Length); string receivedString = Encoding.UTF8.GetString(buffer); // received string is equal to "some fancy string"
使它更简单
此技术强制您使用所需的协议,在此示例中将是:
前4个字节sizeof(int)
表示传入数据包的长度每个字节都是你的数据包,直到结束。
所以你现在应该制作ProtocolHelper
对象:
public static class ProtocolHelper { public byte[] PackIntoProtocol(string message) { List result = new List (); byte[] messageBuffer = Encoding.UTF8.GetBytes(message); result.AddRange(BitConverter.GetBytes(messageBuffer.Length), 0); // this is the first part of the protocol ( length of the message ) result.AddRange(messageBuffer); // this is actual message return result.ToArray(); } public string UnpackProtocol(byte[] buffer) { return Encoding.UTF8.GetString(buffer, 0, buffer.Length); } }
现在(取决于您选择从网络中读取的方法),您必须发送和接收您的消息。
// sender.cs string meMessage = "network message 1"; byte[] buffer = ProtocolHelper.PackIntoProtocol(meMessage); socket.Send(buffer, 0, buffer.Length, 0); // receiver.cs string message = string.Empty; byte[] buffer = new byte[sizeof(int)]; // or simply new byte[4]; int received = socket.Receive(buffer); if(received == sizeof(int)) { int packetLen = BitConverter.ToInt32(buffer);// size of our message buffer = new byte[packetLen]; received = socket.Receive(buffer); if( packetLen == received ) // we have full buffer { message = PacketHelper.UnpackProtocol(buffer); } } Console.WriteLine(message); // output: "network message 1"