box-o-sand/ruby-sockets/marco-polo/MarcoPoloClient/MarcoPoloClient/SocketClient.cs

113 lines
3.7 KiB
C#

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