通过 NetworkStream 接收消息并向客户端发送消息
Receiving messages and sending messages over NetworkStream to a Client
我有一个关于在 C# 中通过网络流从 TCPListener 服务器向客户端发送和接收消息的问题。现在,我的 TCPListener 实例可以从客户端接收一条消息并将其写入服务器控制台,它可以接受一个输入字符串并将其发送回客户端。但我希望改进功能以接受来自客户端的多个连续消息并将多个连续响应发送回客户端。如果 NetworkStream 的 ReadAsync 和 WriteAsync 函数可以处理接收多个连续消息或发送多个连续消息,并且是否有更好的方法来实现这一点,是否有人碰巧有任何指示?此外,由于 Console.ReadLine 函数将在服务器从未从 ReadLine 接收到任何用户输入(并且没有按下 Enter 键)的情况下被阻止,有没有办法测试是否有来自键盘的可选用户输入?这样我就可以尝试仅在服务器从用户接收到某种控制台输入时才执行发送消息命令,否则我可以继续接收客户端消息。
public static async Task getMessage(TcpListener server)
{
byte[] bytes = new byte[256];
using (var theStream = await server.AcceptTcpClientAsync())
{
using (var tcpStream = theStream.GetStream())
{
await tcpStream.ReadAsync(bytes, 0, bytes.Length);
var msg = Encoding.UTF8.GetString(bytes);
Console.WriteLine(msg);
var payload = Console.ReadLine();
var bytes2 = Encoding.UTF8.GetBytes(payload);
await tcpStream.WriteAsync(bytes2, 0, bytes2.Length);
}
}
}
看到一个服务器实现专用于一个客户端是很有趣的!
TCP 是一种双向通信协议,所以是的,您当然可以在需要的时候发送您想要的内容,同时接收任何内容。
我试图在源代码中添加注释,让它自己解释。
但关键点是将 TcpClient
和 NetworkStream
实例声明为静态变量,以便主线程(从控制台读取并将有效负载发送到客户端的线程)可以访问他们
希望对您有所帮助。
public static async Task getMessage(TcpListener server)
{
byte[] bytes = new byte[256];
using (theStream = await server.AcceptTcpClientAsync())
{
using (tcpStream = theStream.GetStream())
{
// We are using an infinite loop which ends when zero bytes have been received.
// Receiving zero bytes means the transmission is over (client disconnected)
while (await tcpStream.ReadAsync(bytes, 0, bytes.Length) > 0)
{
var msg = Encoding.UTF8.GetString(bytes);
Console.WriteLine(msg);
}
Console.WriteLine("Client has disconnected");
}
}
}
/// <summary>
/// Transmists the payload to the client
/// </summary>
/// <param name="payload">The payload to transmit to the client</param>
/// <returns></returns>
static async Task Transmit(string payload)
{
var bytes2 = Encoding.UTF8.GetBytes(payload);
await tcpStream.WriteAsync(bytes2, 0, bytes2.Length);
}
// We are declaring the TcpClient and NetworkStream as static variables
// to be able to access them from all the threads.
private static TcpClient theStream;
public static NetworkStream tcpStream;
static void Main(string[] args)
{
TcpListener server = new TcpListener(IPAddress.Loopback, 9876);
server.Start();
// Start the task getMessage() to accept a client and receive
Task.Run(() => getMessage(server));
string payload;
while ((payload = Console.ReadLine()) != "exit")
{
// Check if the client has connected.
if (tcpStream != null)
{
// Check if they are still connected
if (theStream.Client.Connected)
{
Task.Run(() => Transmit(payload));
}
else
{
Console.WriteLine("the client connection is lost");
break;
}
}
else
{
Console.WriteLine("The client has not connected yet.");
}
}
Console.WriteLine("Stopping the server");
server.Stop();
}
我有一个关于在 C# 中通过网络流从 TCPListener 服务器向客户端发送和接收消息的问题。现在,我的 TCPListener 实例可以从客户端接收一条消息并将其写入服务器控制台,它可以接受一个输入字符串并将其发送回客户端。但我希望改进功能以接受来自客户端的多个连续消息并将多个连续响应发送回客户端。如果 NetworkStream 的 ReadAsync 和 WriteAsync 函数可以处理接收多个连续消息或发送多个连续消息,并且是否有更好的方法来实现这一点,是否有人碰巧有任何指示?此外,由于 Console.ReadLine 函数将在服务器从未从 ReadLine 接收到任何用户输入(并且没有按下 Enter 键)的情况下被阻止,有没有办法测试是否有来自键盘的可选用户输入?这样我就可以尝试仅在服务器从用户接收到某种控制台输入时才执行发送消息命令,否则我可以继续接收客户端消息。
public static async Task getMessage(TcpListener server)
{
byte[] bytes = new byte[256];
using (var theStream = await server.AcceptTcpClientAsync())
{
using (var tcpStream = theStream.GetStream())
{
await tcpStream.ReadAsync(bytes, 0, bytes.Length);
var msg = Encoding.UTF8.GetString(bytes);
Console.WriteLine(msg);
var payload = Console.ReadLine();
var bytes2 = Encoding.UTF8.GetBytes(payload);
await tcpStream.WriteAsync(bytes2, 0, bytes2.Length);
}
}
}
看到一个服务器实现专用于一个客户端是很有趣的!
TCP 是一种双向通信协议,所以是的,您当然可以在需要的时候发送您想要的内容,同时接收任何内容。
我试图在源代码中添加注释,让它自己解释。
但关键点是将 TcpClient
和 NetworkStream
实例声明为静态变量,以便主线程(从控制台读取并将有效负载发送到客户端的线程)可以访问他们
希望对您有所帮助。
public static async Task getMessage(TcpListener server)
{
byte[] bytes = new byte[256];
using (theStream = await server.AcceptTcpClientAsync())
{
using (tcpStream = theStream.GetStream())
{
// We are using an infinite loop which ends when zero bytes have been received.
// Receiving zero bytes means the transmission is over (client disconnected)
while (await tcpStream.ReadAsync(bytes, 0, bytes.Length) > 0)
{
var msg = Encoding.UTF8.GetString(bytes);
Console.WriteLine(msg);
}
Console.WriteLine("Client has disconnected");
}
}
}
/// <summary>
/// Transmists the payload to the client
/// </summary>
/// <param name="payload">The payload to transmit to the client</param>
/// <returns></returns>
static async Task Transmit(string payload)
{
var bytes2 = Encoding.UTF8.GetBytes(payload);
await tcpStream.WriteAsync(bytes2, 0, bytes2.Length);
}
// We are declaring the TcpClient and NetworkStream as static variables
// to be able to access them from all the threads.
private static TcpClient theStream;
public static NetworkStream tcpStream;
static void Main(string[] args)
{
TcpListener server = new TcpListener(IPAddress.Loopback, 9876);
server.Start();
// Start the task getMessage() to accept a client and receive
Task.Run(() => getMessage(server));
string payload;
while ((payload = Console.ReadLine()) != "exit")
{
// Check if the client has connected.
if (tcpStream != null)
{
// Check if they are still connected
if (theStream.Client.Connected)
{
Task.Run(() => Transmit(payload));
}
else
{
Console.WriteLine("the client connection is lost");
break;
}
}
else
{
Console.WriteLine("The client has not connected yet.");
}
}
Console.WriteLine("Stopping the server");
server.Stop();
}