/// <summary> /// Sends a reply to client /// </summary> /// <param name="client"></param> private void SendReply(ConnectedObject client) { if (client == null) { Debug.WriteLine(@"[Server] 无法回应: client null"); return; } Debug.Write(@"[Server] 发送回应: "); // Create reply client.CreateOutgoingMessage(@"消息收到"); var byteReply = client.OutgoingMessageToBytes(); // Listen for more incoming messages try { client.Socket.BeginSend(byteReply, 0, byteReply.Length, SocketFlags.None, SendReplyCallback, client); } catch (SocketException) { // Client was forcibly closed on the client side CloseClient(client); } catch (Exception ex) { Debug.WriteLine(ex.Message); } }
/// <summary> /// 断开某个客户端 /// </summary> /// <param name="client">客户端</param> private void CloseClient(ConnectedObject client) { Debug.WriteLine($@"[Server] {client.GetRemoteEndPoint()} 断开连接"); client.Dispose(); if (_clients.Contains(client)) { _clients.Remove(client); } }
public void Connect(EndPoint ipe) { // 尝试重连 while (!_disposed) { // 创建新的 socket client = CreateNewSocket(); // 连接次数 var attempts = 0; // 不停尝试连接,直到连接到服务器 while (!client.Socket.Connected) { try { ++attempts; Debug.WriteLine($@"[{ClientName}] 尝试第 {attempts} 次连接"); // 尝试连接 client.Socket.Connect(ipe); } catch (SocketException) { //ignored } } // 显示连接状态 IsConnected = true; Debug.WriteLine($@"[{ClientName}] 已连接到 {client.Socket.RemoteEndPoint}"); // 启动接收线程 var receiveThread = new Task(Receive); receiveThread.Start(); // 与服务器断开连接 Task.WaitAll(receiveThread); IsConnected = false; } Debug.WriteLine($@"[{ClientName}] 已取消连接到 {client.Socket.RemoteEndPoint}"); }
/// <summary> /// Checks IAsyncResult for null value /// </summary> /// <param name="ar"></param> /// <param name="err"></param> /// <param name="client"></param> /// <returns></returns> private static bool CheckState(IAsyncResult ar, out string err, out ConnectedObject client) { // Initialise client = null; err = string.Empty; // Check ar if (ar == null) { err = @"Async result null"; return(false); } // Check client client = (ConnectedObject)ar.AsyncState; if (client == null) { err = @"Client null"; return(false); } return(true); }
private void AcceptCallback(IAsyncResult ar) { Debug.WriteLine(@"[Server] 有客户端连接"); _connected.Set(); // Accept new client socket connection var socket = _server.EndAccept(ar); Debug.WriteLine($@"[Server] {socket.RemoteEndPoint} 连接成功"); // Create a new client connection object and store the socket var client = new ConnectedObject { Socket = socket }; // Store all clients _clients.Add(client); CheckClientStatus(client); // Begin receiving messages from new connection try { client.Socket.BeginReceive(client.Buffer, 0, client.BufferSize, SocketFlags.None, ReceiveCallback, client); } catch (SocketException) { // Client was forcibly closed on the client side CloseClient(client); } catch (Exception ex) { Debug.WriteLine(ex.Message); } }
private void CheckClientStatus(ConnectedObject client) { Task.Run(() => { var edp = client.GetRemoteEndPoint(); try { while (client.Socket.IsConnected(1000)) { Task.Delay(1000).Wait(); } CloseClient(client); } catch (ObjectDisposedException) { Debug.WriteLine($@"[Server] {edp} 已断开"); } finally { Debug.WriteLine($@"[Server] {edp} 检测退出"); } }); }