我查看了很多与我的问题相关的问题/答案,但我无法修复它。
我的问题是通过套接字发送数据(本例中的字符串)。
我有一个Server
课程:
class Server
{
Socket sock;
Socket serverSock;
byte[] buffer;
string serverMsg;
string clientMsg;
int bytesRead;
public void start()
{
buffer = new byte[1024];
sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sock.Bind(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 7777));
sock.Listen(5);
Console.WriteLine("Listening ---------------------");
serverSock = sock.Accept();
Console.WriteLine("Client accepted--------------------");
//welcome message
serverMsg = "WELCOME TO THE SERVER";
buffer = ASCIIEncoding.Default.GetBytes(serverMsg);
serverSock.Send(buffer);
Console.WriteLine("Server message: " + ASCIIEncoding.Default.GetString(buffer));
Array.Clear(buffer, 0, buffer.Length);
//response message
while ((bytesRead = serverSock.Receive(buffer)) > 0)
{
clientMsg += ASCIIEncoding.Default.GetString(buffer).Trim('\0');
Array.Clear(buffer, 0, buffer.Length);
}
Console.WriteLine("Client response: " + clientMsg);
//Begin receiving user input from client
while (true)
{
while ((bytesRead = serverSock.Receive(buffer)) > 0)
{
clientMsg += ASCIIEncoding.Default.GetString(buffer).Trim('\0');
Array.Clear(buffer, 0, buffer.Length);
}
Console.WriteLine("Client message: " + clientMsg);
//server response to user input
serverMsg = clientMsg.Replace('b', 'p');
buffer = ASCIIEncoding.Default.GetBytes(serverMsg);
serverSock.Send(buffer);
Console.WriteLine("Server message: " + ASCIIEncoding.Default.GetString(buffer));
Array.Clear(buffer, 0, buffer.Length);
}
}
}
和client
班级:
class Client
{
Socket socketClient;
string serverMsg;
string clientMsg;
byte[] buffer;
int bytesRead;
public void start()
{
buffer = new byte[1024];
socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socketClient.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 7777));
Console.WriteLine("Client connected-------------------");
//receive welcome message from server
while ((bytesRead = socketClient.Receive(buffer)) > 0)
{
serverMsg += ASCIIEncoding.Default.GetString(buffer).Trim('\0');
Array.Clear(buffer, 0, buffer.Length);
}
Console.WriteLine("Server message: " + serverMsg);
//response message
clientMsg = "THANK YOU FOR YOUR GREETINGS";
buffer = ASCIIEncoding.Default.GetBytes(clientMsg);
socketClient.Send(buffer);
Console.WriteLine("Client message: " + ASCIIEncoding.Default.GetString(buffer));
Array.Clear(buffer, 0, buffer.Length);
//Begin sending user input
while (true)
{
clientMsg = Console.ReadLine();
buffer = ASCIIEncoding.Default.GetBytes(clientMsg);
socketClient.Send(buffer);
Console.WriteLine("Client message: " + ASCIIEncoding.Default.GetString(buffer));
Array.Clear(buffer, 0, buffer.Length);
while ((bytesRead = socketClient.Receive(buffer)) > 0)
{
serverMsg += ASCIIEncoding.Default.GetString(buffer).Trim('\0');
Array.Clear(buffer, 0, buffer.Length);
}
Console.WriteLine("Server response: "+serverMsg);
Array.Clear(buffer, 0, buffer.Length);
}
}
}
当我运行程序时,我得到以下输出:
****服务器输出:** **
> Listening ---------------------
> Client accepted--------------------
> Server message: WELCOME TO THE SERVER
****客户输出:** **
> Client connected-------------------
我知道我的客户端无限期地陷入了接收缓冲区的循环中。
*那么我怎样才能打破这个循环,确保收到完整的消息?
*是否有更好的(更通用的)方法通过套接字发送/接收不同的数据类型?
谢谢