Skip to content

Instantly share code, notes, and snippets.

@agesome
Created February 17, 2013 00:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save agesome/4969426 to your computer and use it in GitHub Desktop.
Save agesome/4969426 to your computer and use it in GitHub Desktop.
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace client
{
class ProcessingServerClient
{
Socket io = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
const uint bufferSize = 1024;
readonly byte[] goodbyeMagic = { 0x42 };
public void Connect ()
{
io.Connect ("localhost", 8080);
}
public void Disconnect ()
{
io.Send (goodbyeMagic);
io.Close ();
}
public string requestResponse (String str)
{
byte[] buf = new byte[str.Length * sizeof (char)];
System.Buffer.BlockCopy (str.ToCharArray (), 0, buf, 0, buf.Length);
io.Send (buf);
byte[] rbuf = new byte[bufferSize];
int size = io.Receive(rbuf);
byte[] response = new byte[size];
System.Buffer.BlockCopy (rbuf, 0, response, 0, size);
return System.Text.UnicodeEncoding.Unicode.GetString (response);
}
}
class MainClass
{
public static void Main (string[] args)
{
ProcessingServerClient io = new ProcessingServerClient ();
string r, request;
Console.WriteLine ("Enter 'c' to send current time or enter a format string");
r = Console.ReadLine ();
if (r == "c")
request = DateTime.Now.ToString ("yyyy-MM-d HH:mm:ss");
else
request = Console.ReadLine ();
io.Connect ();
Console.WriteLine ("Connected");
r = io.requestResponse (request);
io.Disconnect ();
Console.WriteLine ("Response: " + r);
Console.WriteLine ("Done");
Console.ReadLine ();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment