public void SendData(string dataString, CommandHandler.Commands command, Socket clientSocket) { //converts string into byte array byte[] data = Encoding.ASCII.GetBytes((int)command + " " + dataString); try { //sends the data to the client socket. clientSocket.Send(data); //begins recieving data back from the client (async). clientSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(Receive), clientSocket); } catch (Exception ex) when(ex.Message.Contains("Socket")) { //something went wrong client side -- disconnected?? //checks to see if list contains client socket. if (connections.Contains(clientSocket)) { //removes the socket from the list connections.Remove(clientSocket); //calls the connection changed event. onConnectionChanged(false, clientSocket); } } }
public void SendString(string dataString, CommandHandler.Commands command) { //gets bytes from data byte[] data = Encoding.ASCII.GetBytes((int)command + " " + dataString); try { //sends the data to the client socket. socket.Send(data); //begins recieving data back from the client (async). socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(Receive), socket); } catch { //something went wrong server side -- closed?? onConnectionChanged(false); } }