/// <summary> /// Server Reciving client data /// </summary> /// <param name="asyncronousResult"></param> private void ReceiveCallback(IAsyncResult asyncronousResult) { Socket current = (Socket)asyncronousResult.AsyncState; int received; try { received = current.EndReceive(asyncronousResult); } catch (SocketException) // Connection broken/error { KeyController.logThis("Conexión perdida: " + current.RemoteEndPoint.ToString()); current.Close(); socketList.Remove(current); return; } byte[] recBuf = new byte[received]; Array.Copy(_buffer, recBuf, received); string text = Encoding.ASCII.GetString(recBuf); Console.WriteLine("Texto recebido: " + text); if (text == "pause") { Send2All(); // pause requested } else { //get name } current.BeginReceive(_buffer, 0, _BUFFER_SIZE, SocketFlags.None, ReceiveCallback, current); }
private void ReceiveCallback(IAsyncResult asyncronousResult) { int received; try { received = socketClient.EndReceive(asyncronousResult); } catch (SocketException) { //Console.WriteLine("Conection lost: " + socketClient.RemoteEndPoint.ToString()); KeyController.logThis("Error - Servidor no accesible"); socketClient.Close(); return; } byte[] recBuf = new byte[received]; Array.Copy(_buffer, recBuf, received); string text = Encoding.ASCII.GetString(recBuf); Console.WriteLine("Texto recebido: " + text); KeyController.SendKey2Process(Program.myForm.getProcessName()); socketClient.BeginReceive(_buffer, 0, _BUFFER_SIZE, SocketFlags.None, ReceiveCallback, socketClient); }
public void TrySend(string msg = "pause") { try { byte[] data = Encoding.ASCII.GetBytes(msg); socketClient.Send(data); } catch (SocketException) { KeyController.logThis("Error - no se pudo enviar/conectar."); return; } }
public void TryConnect(string ip) { try { //if(socketClient.Connected) socketClient.Connect(IPAddress.Parse(ip), Server.PORT); socketClient.BeginReceive(_buffer, 0, _BUFFER_SIZE, SocketFlags.None, ReceiveCallback, socketClient); KeyController.logThis("Conectado Bien " + Environment.UserName); } catch { KeyController.logThis("Error al conectar"); } }
/// <summary> /// Accepts a new client /// </summary> /// <param name="asyncronousResult"></param> private void AcceptCallback(IAsyncResult asyncronousResult) { Socket socket; try { socket = _serverSocket.EndAccept(asyncronousResult); } catch (ObjectDisposedException) { return; } socketList.Add(socket); socket.BeginReceive(_buffer, 0, _BUFFER_SIZE, SocketFlags.None, ReceiveCallback, socket); KeyController.logThis("New Client: " + socket.RemoteEndPoint.ToString()); _serverSocket.BeginAccept(AcceptCallback, null); }