seeing if there are any idiosynchracies involved with C# <-> Ruby socket communication

This commit is contained in:
Dan Buch 2012-06-17 23:18:24 -04:00
parent 0b509b19f0
commit 7b6392468b
6 changed files with 225 additions and 0 deletions

View File

@ -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

View File

@ -0,0 +1 @@
/bin/

View File

@ -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("")]

View File

@ -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();
}
}
}

View File

@ -0,0 +1,43 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
<ProductVersion>10.0.0</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{6728C605-D295-48C7-8634-C9D06A3325F0}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>MarcoPoloClient</RootNamespace>
<AssemblyName>MarcoPoloClient</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug</OutputPath>
<DefineConstants>DEBUG</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<PlatformTarget>x86</PlatformTarget>
<Externalconsole>true</Externalconsole>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<DebugType>none</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Release</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<PlatformTarget>x86</PlatformTarget>
<Externalconsole>true</Externalconsole>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
</ItemGroup>
<ItemGroup>
<Compile Include="Main.cs" />
<Compile Include="AssemblyInfo.cs" />
<Compile Include="SocketClient.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>

View File

@ -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<SocketAsyncEventArgs> (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<SocketAsyncEventArgs> (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<SocketAsyncEventArgs> (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 ();
}
}
}
}