当我通过 TCP 服务器将数据发送到客户端第一次检索全名时,当我再次发送另一个名称(如在 C 语言中检索为(OK)的书时)
客户端代码
sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 而(真){
IPEndPoint localEndPoint;
localEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.2"), 1);
try
{
sck.Connect(localEndPoint);
}
catch
{
Console.Write("Unable to connect to remote end point!\r\n");
);
}
Console.Write("Enter Text: ");
string text = Console.ReadLine();
byte[] data = Encoding.ASCII.GetBytes(text);
sck.Send(data);
Console.Write("Data Sent!\r\n");
sck.Close();
Console.ReadLine();
服务器端代码
while (true) { 接受的套接字 = sck.Accept();
Buffer = new byte[accepted.SendBufferSize];
int bytesRead = accepted.Receive(Buffer);
byte[] formatted = new byte[bytesRead];
for (int i = 0; i < bytesRead; i++)
{
formatted[i] = Buffer[i];
//Console.WriteLine(Buffer[i] + "\r\n");
}
//string strData = Encoding.ASCII.GetString(Buffer);
string strData = Encoding.ASCII.GetString(formatted);
Console.Write(strData + "\r\n");
accepted.Close();
}