Hi all,
I'm trying to send a null character as a string delimiter through a
TcpClient (code below). It's to connect to this poker bot room:
http://games.cs.ualberta.ca/webgames/poker/bots.html
My code is as below, but I never get a response. I'm assuming my
transmission is ending at the null character (the debug statement certainly
only outputs up until the null character). Any ideas?
The code below will compile and run as a console app without modification =)
I've tried adding newlines and null chars between each line, and also just
to the end (the spec isn't very clear IMO, and I don't understand the Perl
source enough to know whether it's sending nulls/newlines with ->send())
If anyone can modify what's below to get a response from the server (21, 22
and 24 are valid responses), I'll be very grateful! :)
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
namespace DTuppeny.PokerBot
{
public class Bot
{
TcpClient tcpClient = new TcpClient();
public void Run()
{
tcpClient.Connect("hilo.cs.ualberta.ca", 55006);
Write("0020");
Write("0025");
Write("TestBot\0TestBot\01\0TestBot");
string response = Response();
Console.WriteLine(response);
Console.ReadLine();
}
private void Write(string message)
{
System.Text.ASCIIEncoding en = new System.Text.ASCIIEncoding();
byte[] WriteBuffer = new byte[1024];
WriteBuffer = en.GetBytes(message);
NetworkStream stream = tcpClient.GetStream();
stream.Write(WriteBuffer, 0, WriteBuffer.Length);
System.Diagnostics.Trace.Write("WRITE:" + message);
}
private string Response()
{
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
byte[] serverbuff = new Byte[1024];
StringBuilder retval = new StringBuilder();
NetworkStream stream = tcpClient.GetStream();
int count = 0;
while (true)
{
byte[] buff = new Byte[2];
int bytes = stream.Read(buff, 0, 1);
if (bytes == 1)
{
serverbuff[count] = buff[0];
count++;
if (count >= 1024)
{
retval.Append(enc.GetString(serverbuff, 0, count));
count = 0;
}
if (buff[0] == '\n')
{
break;
}
}
else
{
break;
}
}
retval.Append(enc.GetString(serverbuff, 0, count));
System.Diagnostics.Trace.Write("READ:" + retval.ToString());
return retval.ToString();
}
}
}