c# 服务端向客户端发送命令
服务端一直监听客户端,有这样的代码:publicstaticvoidStartListening(){IPEndPointlocalEndPoint=newIPEndPo...
服务端一直监听客户端,有这样的代码:
public static void StartListening()
{IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Parse(IP), int.Parse(PORT));
Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
try
{
listener.Bind(localEndPoint);
listener.Listen(5000);
while (true)
{
allDone.Reset();
listener.BeginAccept(new AsyncCallback(AcceptCallback), listener);
allDone.WaitOne();
}
}
public static void AcceptCallback(IAsyncResult ar)
{
try
{allDone.Set();
Socket listener = (Socket)ar.AsyncState;
Socket handler = listener.EndAccept(ar);
StateObject state = new StateObject();
state.workSocket = handler;
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);
public static void ReadCallback(IAsyncResult ar)
{
StateObject state = (StateObject)ar.AsyncState;
Socket handler = state.workSocket;
try
{
int bytesRead = handler.EndReceive(ar);
state.sb.Append(Encoding.Unicode.GetString(state.buffer, 0, bytesRead));
content = state.sb.ToString();
AnswerRequst(content,handler);
}
}
private static void AnswerRequst(string content, Socket handler)
{
string cmd;
if (strTemp.Length > 0)
cmd = strTemp[0];
switch(cmd)
{
case "GetUserInfo"://登录后的信息
Send(handler, GetUserInfo(strTemp[1]));
break;
case "GetTargetInfo": //获取数据
Send(handler, GetTInfo(strTemp[1]));
break;
case "Stop": // 获取客户端结束
Send(handler, Stop(strTemp[1]));
break;
case "Null":
Send(handler,"0|接受消息为空");
break;
客户端向服务端发送相关信息,服务端接收到相关信息后发送命令。客户端接收到后做相应的处理。
现在的问题是,服务端点击一个按钮要主动向所有在线客户端发送命令。这么解决?
我增加了一个带str参数的。
public static void StartListening(string str)
{
}
却报错,告知端口只能使用一次。谢谢,非常感谢! 展开
public static void StartListening()
{IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Parse(IP), int.Parse(PORT));
Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
try
{
listener.Bind(localEndPoint);
listener.Listen(5000);
while (true)
{
allDone.Reset();
listener.BeginAccept(new AsyncCallback(AcceptCallback), listener);
allDone.WaitOne();
}
}
public static void AcceptCallback(IAsyncResult ar)
{
try
{allDone.Set();
Socket listener = (Socket)ar.AsyncState;
Socket handler = listener.EndAccept(ar);
StateObject state = new StateObject();
state.workSocket = handler;
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);
public static void ReadCallback(IAsyncResult ar)
{
StateObject state = (StateObject)ar.AsyncState;
Socket handler = state.workSocket;
try
{
int bytesRead = handler.EndReceive(ar);
state.sb.Append(Encoding.Unicode.GetString(state.buffer, 0, bytesRead));
content = state.sb.ToString();
AnswerRequst(content,handler);
}
}
private static void AnswerRequst(string content, Socket handler)
{
string cmd;
if (strTemp.Length > 0)
cmd = strTemp[0];
switch(cmd)
{
case "GetUserInfo"://登录后的信息
Send(handler, GetUserInfo(strTemp[1]));
break;
case "GetTargetInfo": //获取数据
Send(handler, GetTInfo(strTemp[1]));
break;
case "Stop": // 获取客户端结束
Send(handler, Stop(strTemp[1]));
break;
case "Null":
Send(handler,"0|接受消息为空");
break;
客户端向服务端发送相关信息,服务端接收到相关信息后发送命令。客户端接收到后做相应的处理。
现在的问题是,服务端点击一个按钮要主动向所有在线客户端发送命令。这么解决?
我增加了一个带str参数的。
public static void StartListening(string str)
{
}
却报错,告知端口只能使用一次。谢谢,非常感谢! 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询