C#在新客户端连接时使用事件构建简单服务器
我尝试用c语言构建一个简单的服务器,它等待来自客户端的连接。如果一个新的客户机连接,它将创建一个新线程,在该线程中,它从客户机读取数据并向客户机发送一条消息,表明服务器收到了来自客户机的消息。我的问题是,当一个新客户端连接到服务器时,我试图引发一个事件,但我没有得到我应该订阅的内容 这是我的密码:C#在新客户端连接时使用事件构建简单服务器,c#,C#,我尝试用c语言构建一个简单的服务器,它等待来自客户端的连接。如果一个新的客户机连接,它将创建一个新线程,在该线程中,它从客户机读取数据并向客户机发送一条消息,表明服务器收到了来自客户机的消息。我的问题是,当一个新客户端连接到服务器时,我试图引发一个事件,但我没有得到我应该订阅的内容 这是我的密码: namespace ChatServer { using System; using System.Collections.Generic; using System.Linq; using Syst
namespace ChatServer
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Threading;
class Program
{
public static event EventHandler<NewClientConnectedEventArgs>NewClientConnected;
public static void Main(string[] args)
{
TcpListener listener = new TcpListener(IPAddress.Any, 80);
listener.Start();
while (true)
{
TcpClient client = listener.AcceptTcpClient();
//listener += NewClientConnected(null, new NewClientConnectedEventArgs(client));
//FireNewClientHasConnected(null, eventargs);
}
}
public static void MakeNewConnection(TcpClient client)
{
Thread thread = new Thread(new ParameterizedThreadStart(NewClient));
thread.Start(client);
}
public static void NewClient(object data)
{
TcpClient client = (TcpClient)data;
string adress = client.Client.AddressFamily.ToString();
Console.WriteLine("{0} has connected!", adress);
NetworkStream ns = client.GetStream();
while (true)
{
byte[] receivedbuffer = new byte[8192];
int receivedbytes;
receivedbytes = ns.Read(receivedbuffer, 0, receivedbuffer.Length);
string message = Encoding.UTF8.GetString(receivedbuffer, 0, receivedbytes);
string newmessage = "The server received: " + message;
byte[] sendBuffer = Encoding.UTF8.GetBytes(newmessage);
ns.Write(sendBuffer, 0, sendBuffer.Length);
}
}
protected static void FireNewClientHasConnected(object sender, NewClientConnectedEventArgs args)
{
if (NewClientConnected != null)
{
MakeNewConnection(args.Client);
}
}
}
名称空间聊天服务器
{
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
Net系统;
使用System.Net.Sockets;
使用系统线程;
班级计划
{
公共静态事件EventHandlerNewClientConnected;
公共静态void Main(字符串[]args)
{
TcpListener侦听器=新的TcpListener(IPAddress.Any,80);
listener.Start();
while(true)
{
TcpClient client=listener.AcceptTcpClient();
//listener+=NewClientConnected(空,NewClientConnectedEventTargets(客户端));
//FireNewClientAsConnected(null,eventargs);
}
}
公共静态void MakeNewConnection(TcpClient客户端)
{
线程线程=新线程(新参数化线程启动(新客户端));
线程启动(客户端);
}
公共静态void NewClient(对象数据)
{
TcpClient客户端=(TcpClient)数据;
字符串地址=client.client.AddressFamily.ToString();
WriteLine(“{0}已连接!”,地址);
NetworkStream ns=client.GetStream();
while(true)
{
字节[]receivedbuffer=新字节[8192];
int接收字节;
receivedbytes=ns.Read(receivedbuffer,0,receivedbuffer.Length);
string message=Encoding.UTF8.GetString(receivedbuffer,0,receivedbytes);
string newmessage=“服务器收到:”+消息;
byte[]sendBuffer=Encoding.UTF8.GetBytes(newmessage);
ns.Write(sendBuffer,0,sendBuffer.Length);
}
}
受保护的静态无效FireNewClientAsConnected(对象发送方,NewClientConnectedEventArgs参数)
{
if(NewClientConnected!=null)
{
MakeNewConnection(args.Client);
}
}
}
我遇到问题的部分是我注释掉的部分。提前感谢!调用侦听器。AcceptCpcClient()会一直阻塞,直到客户端连接。因此,只要在侦听器接受客户端后触发事件即可
class Program
{
public static void Main(string[] args)
{
var listener = new TcpListener(IPAddress.Any, 80);
listener.Start();
while (true)
{
TcpClient client = listener.AcceptTcpClient();
MakeNewConnection(client);
}
}
public static void MakeNewConnection(TcpClient client)
{
var thread = new Thread(NewClient);
thread.Start(client);
}
public static void NewClient(object data)
{
var client = (TcpClient)data;
string adress = client.Client.AddressFamily.ToString();
Console.WriteLine("{0} has connected!", adress);
NetworkStream ns = client.GetStream();
while (true)
{
byte[] receivedbuffer = new byte[8192];
int receivedbytes = ns.Read(receivedbuffer, 0, receivedbuffer.Length);
string message = Encoding.UTF8.GetString(receivedbuffer, 0, receivedbytes);
string newmessage = "The server received: " + message;
byte[] sendBuffer = Encoding.UTF8.GetBytes(newmessage);
ns.Write(sendBuffer, 0, sendBuffer.Length);
}
}
}
所以我不需要任何东西来订阅该事件?我也不需要检查该事件是否为null,因为没有订阅任何内容?我有点困惑。只要您不想将客户端公开给程序以外的其他类,就不要使用该事件,直接调用MakeNewConnection()