From 46def9b2f60ba87e7e8b23c8496267fa89df8f6b Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sun, 17 Jun 2012 23:40:40 -0400 Subject: [PATCH] looping it up and cleaning up whitespace, too --- .../MarcoPoloClient/MarcoPoloClient/Main.cs | 27 +++++---- .../MarcoPoloClient/SocketClient.cs | 57 ++++++++++--------- 2 files changed, 47 insertions(+), 37 deletions(-) diff --git a/ruby-sockets/marco-polo/cs/MarcoPoloClient/MarcoPoloClient/Main.cs b/ruby-sockets/marco-polo/cs/MarcoPoloClient/MarcoPoloClient/Main.cs index d212af4..41b2c65 100644 --- a/ruby-sockets/marco-polo/cs/MarcoPoloClient/MarcoPoloClient/Main.cs +++ b/ruby-sockets/marco-polo/cs/MarcoPoloClient/MarcoPoloClient/Main.cs @@ -8,18 +8,25 @@ namespace MarcoPoloClient { class MainClass { - const int MARCO_POLO_PORT = 22000; - const string MARCO_POLO_SERVER = "127.0.0.1"; - + const int MARCO_POLO_PORT = 22000; + const string MARCO_POLO_SERVER = "127.0.0.1"; + public static void Main (string[] args) { - SocketClient client = new SocketClient(); - string result = client.Connect(MARCO_POLO_SERVER, MARCO_POLO_PORT); - result = client.Send("polo doreen 25 75\n"); - Console.WriteLine(String.Format("'polo' got back: '{0}'", result)); - string response = client.Receive(); - Console.WriteLine(String.Format("'polo' response: '{0}'", response)); - client.Close(); + while (true) + { + SocketClient client = new SocketClient(); + + string result = client.Connect(MARCO_POLO_SERVER, MARCO_POLO_PORT); + result = client.Send("polo doreen 25 75\n"); + Console.WriteLine(String.Format("'polo' got back: '{0}'", result)); + string response = client.Receive(); + Console.WriteLine(String.Format("'polo' response: '{0}'", response)); + client.Close(); + + Console.WriteLine("sleeping for half a second..."); + Thread.Sleep(500); + } } } } diff --git a/ruby-sockets/marco-polo/cs/MarcoPoloClient/MarcoPoloClient/SocketClient.cs b/ruby-sockets/marco-polo/cs/MarcoPoloClient/MarcoPoloClient/SocketClient.cs index 2db8c44..a29ebe1 100644 --- a/ruby-sockets/marco-polo/cs/MarcoPoloClient/MarcoPoloClient/SocketClient.cs +++ b/ruby-sockets/marco-polo/cs/MarcoPoloClient/MarcoPoloClient/SocketClient.cs @@ -28,38 +28,40 @@ namespace MarcoPoloClient ); SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs (); socketEventArg.RemoteEndPoint = hostEntry; - + socketEventArg.Completed += new EventHandler (delegate(object s, SocketAsyncEventArgs e) - { - result = e.SocketError.ToString (); - _clientDone.Set (); - }); - + { + result = e.SocketError.ToString (); + _clientDone.Set (); + } + ); + _clientDone.Reset (); _socket.ConnectAsync (socketEventArg); _clientDone.WaitOne (TIMEOUT_MILLISECONDS); - + return result; } public string Send (string data) { string response = "Operation Timeout"; - + if (_socket != null) { SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs (); socketEventArg.RemoteEndPoint = _socket.RemoteEndPoint; socketEventArg.UserToken = null; - + socketEventArg.Completed += new EventHandler (delegate(object s, SocketAsyncEventArgs e) - { - response = e.SocketError.ToString (); - _clientDone.Set (); - }); - + { + response = e.SocketError.ToString (); + _clientDone.Set (); + } + ); + byte[] payload = Encoding.UTF8.GetBytes (data); socketEventArg.SetBuffer (payload, 0, payload.Length); - + _clientDone.Reset (); _socket.SendAsync (socketEventArg); _clientDone.WaitOne (TIMEOUT_MILLISECONDS); @@ -72,30 +74,31 @@ namespace MarcoPoloClient public string Receive () { string response = "Operation Timeout"; - + if (_socket != null) { SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs (); socketEventArg.RemoteEndPoint = _socket.RemoteEndPoint; - + socketEventArg.SetBuffer (new Byte[MAX_BUFFER_SIZE], 0, MAX_BUFFER_SIZE); socketEventArg.Completed += new EventHandler (delegate(object s, SocketAsyncEventArgs e) - { - if (e.SocketError == SocketError.Success) { - response = Encoding.UTF8.GetString (e.Buffer, e.Offset, e.BytesTransferred); - response = response.Trim ('\0'); - } else { - response = e.SocketError.ToString (); + { + if (e.SocketError == SocketError.Success) { + response = Encoding.UTF8.GetString (e.Buffer, e.Offset, e.BytesTransferred); + response = response.Trim ('\0'); + } else { + response = e.SocketError.ToString (); + } + _clientDone.Set (); } - _clientDone.Set (); - }); - + ); + _clientDone.Reset (); _socket.ReceiveAsync (socketEventArg); _clientDone.WaitOne (TIMEOUT_MILLISECONDS); } else { response = "Socket is not initialized"; } - + return response; }