diff --git a/ruby-sockets/marco-polo/cs/MarcoPoloClient/MarcoPoloClient.sln b/ruby-sockets/marco-polo/cs/MarcoPoloClient/MarcoPoloClient.sln new file mode 100644 index 0000000..7e0fe01 --- /dev/null +++ b/ruby-sockets/marco-polo/cs/MarcoPoloClient/MarcoPoloClient.sln @@ -0,0 +1,20 @@ + +Microsoft Visual Studio Solution File, Format Version 11.00 +# Visual Studio 2010 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MarcoPoloClient", "MarcoPoloClient\MarcoPoloClient.csproj", "{6728C605-D295-48C7-8634-C9D06A3325F0}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x86 = Debug|x86 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {6728C605-D295-48C7-8634-C9D06A3325F0}.Debug|x86.ActiveCfg = Debug|x86 + {6728C605-D295-48C7-8634-C9D06A3325F0}.Debug|x86.Build.0 = Debug|x86 + {6728C605-D295-48C7-8634-C9D06A3325F0}.Release|x86.ActiveCfg = Release|x86 + {6728C605-D295-48C7-8634-C9D06A3325F0}.Release|x86.Build.0 = Release|x86 + EndGlobalSection + GlobalSection(MonoDevelopProperties) = preSolution + StartupItem = MarcoPoloClient\MarcoPoloClient.csproj + EndGlobalSection +EndGlobal diff --git a/ruby-sockets/marco-polo/cs/MarcoPoloClient/MarcoPoloClient/.gitignore b/ruby-sockets/marco-polo/cs/MarcoPoloClient/MarcoPoloClient/.gitignore new file mode 100644 index 0000000..ae3c172 --- /dev/null +++ b/ruby-sockets/marco-polo/cs/MarcoPoloClient/MarcoPoloClient/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/ruby-sockets/marco-polo/cs/MarcoPoloClient/MarcoPoloClient/AssemblyInfo.cs b/ruby-sockets/marco-polo/cs/MarcoPoloClient/MarcoPoloClient/AssemblyInfo.cs new file mode 100644 index 0000000..543896a --- /dev/null +++ b/ruby-sockets/marco-polo/cs/MarcoPoloClient/MarcoPoloClient/AssemblyInfo.cs @@ -0,0 +1,27 @@ +using System.Reflection; +using System.Runtime.CompilerServices; + +// Information about this assembly is defined by the following attributes. +// Change them to the values specific to your project. + +[assembly: AssemblyTitle("MarcoPoloClient")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("")] +[assembly: AssemblyCopyright("me")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". +// The form "{Major}.{Minor}.*" will automatically update the build and revision, +// and "{Major}.{Minor}.{Build}.*" will update just the revision. + +[assembly: AssemblyVersion("1.0.*")] + +// The following attributes are used to specify the signing key for the assembly, +// if desired. See the Mono documentation for more information about signing. + +//[assembly: AssemblyDelaySign(false)] +//[assembly: AssemblyKeyFile("")] + diff --git a/ruby-sockets/marco-polo/cs/MarcoPoloClient/MarcoPoloClient/Main.cs b/ruby-sockets/marco-polo/cs/MarcoPoloClient/MarcoPoloClient/Main.cs new file mode 100644 index 0000000..d212af4 --- /dev/null +++ b/ruby-sockets/marco-polo/cs/MarcoPoloClient/MarcoPoloClient/Main.cs @@ -0,0 +1,25 @@ +using System; +using System.Net.Sockets; +using System.Net; +using System.Threading; +using System.Text; + +namespace MarcoPoloClient +{ + class MainClass + { + 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(); + } + } +} diff --git a/ruby-sockets/marco-polo/cs/MarcoPoloClient/MarcoPoloClient/MarcoPoloClient.csproj b/ruby-sockets/marco-polo/cs/MarcoPoloClient/MarcoPoloClient/MarcoPoloClient.csproj new file mode 100644 index 0000000..458c3f2 --- /dev/null +++ b/ruby-sockets/marco-polo/cs/MarcoPoloClient/MarcoPoloClient/MarcoPoloClient.csproj @@ -0,0 +1,43 @@ + + + + Debug + x86 + 10.0.0 + 2.0 + {6728C605-D295-48C7-8634-C9D06A3325F0} + Exe + MarcoPoloClient + MarcoPoloClient + v4.0 + + + true + full + false + bin\Debug + DEBUG + prompt + 4 + x86 + true + + + none + false + bin\Release + prompt + 4 + x86 + true + + + + + + + + + + + \ No newline at end of file diff --git a/ruby-sockets/marco-polo/cs/MarcoPoloClient/MarcoPoloClient/SocketClient.cs b/ruby-sockets/marco-polo/cs/MarcoPoloClient/MarcoPoloClient/SocketClient.cs new file mode 100644 index 0000000..2db8c44 --- /dev/null +++ b/ruby-sockets/marco-polo/cs/MarcoPoloClient/MarcoPoloClient/SocketClient.cs @@ -0,0 +1,109 @@ +using System; +using System.Net.Sockets; +using System.Net; +using System.Threading; +using System.Text; + +namespace MarcoPoloClient +{ + public class SocketClient + { + Socket _socket = null; + static ManualResetEvent _clientDone = new ManualResetEvent (false); + const int TIMEOUT_MILLISECONDS = 5000; + const int MAX_BUFFER_SIZE = 2048; + + public SocketClient () + { + } + + public string Connect (string hostName, int portNumber) + { + string result = string.Empty; + DnsEndPoint hostEntry = new DnsEndPoint (hostName, portNumber); + _socket = new Socket ( + AddressFamily.InterNetwork, + SocketType.Stream, + ProtocolType.Tcp + ); + SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs (); + socketEventArg.RemoteEndPoint = hostEntry; + + socketEventArg.Completed += new EventHandler (delegate(object s, SocketAsyncEventArgs e) + { + 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 (); + }); + + byte[] payload = Encoding.UTF8.GetBytes (data); + socketEventArg.SetBuffer (payload, 0, payload.Length); + + _clientDone.Reset (); + _socket.SendAsync (socketEventArg); + _clientDone.WaitOne (TIMEOUT_MILLISECONDS); + } else { + response = "Socket is not initialized"; + } + return response; + } + + 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 (); + } + _clientDone.Set (); + }); + + _clientDone.Reset (); + _socket.ReceiveAsync (socketEventArg); + _clientDone.WaitOne (TIMEOUT_MILLISECONDS); + } else { + response = "Socket is not initialized"; + } + + return response; + } + + public void Close () + { + if (_socket != null) { + _socket.Close (); + } + } + } +}