public void Connect(TcpClient client, out SocketException ex) { ex = null; for (int i = 0; i < 10 && !ReadWriteClient.Connected; i++) { try { if (client.Equals(ReadWriteClient)) client.Connect(_readWriteEndpoint); if (client.Equals(ReadOnlyClient)) client.Connect(_readOnlyEndpoint); break; } catch (SocketException se) { ex = se; continue; } } }
public void DoWhenConnected(TcpClient client, Behavior behavior, SocketException se, out string result, string command = null) { result = null; if (client.Connected) { BinaryReader reader = null; BinaryWriter writer = null; // if the behavior is set to read it'll only read, if write it'll only write, if readwrite it'll do both if (behavior == Behavior.ReadOnly || behavior == Behavior.ReadWrite) reader = new BinaryReader(client.GetStream()); if (behavior == Behavior.Write || behavior == Behavior.ReadWrite) { writer = new BinaryWriter(client.GetStream()); writer.Write(command); } if (behavior == Behavior.ReadOnly || behavior == Behavior.ReadWrite) { try { result = reader.ReadString(); } catch (System.IO.IOException ioe) { Console.WriteLine("Server has closed, and lef this message: {0}", ioe.Message); } } if (client.Equals(ReadWriteClient)) { client.Close(); client = new TcpClient(); } } else throw se; }