Skip to content

Instantly share code, notes, and snippets.

@vermie
Created October 13, 2010 23:35
Show Gist options
  • Save vermie/625192 to your computer and use it in GitHub Desktop.
Save vermie/625192 to your computer and use it in GitHub Desktop.
namespace RAConsole
{
class Program
{
static void Main(string[] args)
{
TcpClient raClient = new TcpClient(new IPEndPoint(IPAddress.Any, 0));
raClient.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 3443));
NetworkStream stream = raClient.GetStream();
Console.Write(stream.RARead());
string command;
while ((command = Console.ReadLine()) != "quit")
{
stream.RAWrite(command);
Console.Write(stream.RARead());
}
raClient.Close();
Console.Write("Press any key to close.");
Console.ReadKey(true);
}
}
public static class RASocketReader
{
public static string RARead(this NetworkStream stream)
{
StringBuilder builder = new StringBuilder();
builder.Append((char)stream.ReadByte());
while (stream.DataAvailable)
{
int currentByte;
if ((currentByte = stream.ReadByte()) == -1)
return builder.ToString();
if (currentByte == 0)
return builder.ToString();
builder.Append((char)currentByte);
}
return builder.ToString();
}
public static void RAWrite(this NetworkStream stream, string data)
{
foreach (char c in data)
stream.WriteByte((byte)c);
stream.WriteByte((byte)'\n');
stream.Flush();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment