looping it up and cleaning up whitespace, too
This commit is contained in:
parent
665d867bc4
commit
46def9b2f6
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -28,38 +28,40 @@ namespace MarcoPoloClient
|
||||
);
|
||||
SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs ();
|
||||
socketEventArg.RemoteEndPoint = hostEntry;
|
||||
|
||||
|
||||
socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs> (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<SocketAsyncEventArgs> (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<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 ();
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user