Sending null character via TcpClient?

D

Danny Tuppeny

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

}

}
 
D

Danny Tuppeny

The code below will compile and run as a console app without modification
=)
<snip>

Sorry Jon, I couldn't even get that right! I missed this bit:

static void Main(string[] args)
{
Bot b = new Bot();
b.Run();
}

*slaps wrist*
 
D

Danny Tuppeny

<snip>

Had some help from Oliver Sturm. I don't think more could've been wrong with
it!

I was sending the ints as strings, instead of proper 4-byte-ints. Then there
was using network byte order, flushing the stream, and a few other things...
Here's some complete code Oliver sent back that gets a response from the
server =)

using System;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace DTuppeny.PokerBot
{
public class Bot
{

TcpClient tcpClient = new TcpClient();
Stream stream;

public void Run()
{


tcpClient.Connect("hilo.cs.ualberta.ca", 55006);

stream = tcpClient.GetStream();



Write(20);


Write(28);


Write("TestBot");


WriteByte(0);


Write("TestBot");


WriteByte(0);


Write(1);


Write("TestBot");


WriteByte(0);


stream.Flush();


Response();
Console.WriteLine("end");
Console.ReadLine();
}


private void Write(string message)
{

byte[] bytes = Encoding.ASCII.GetBytes(message);


stream.Write(bytes, 0, bytes.Length);
}


private void Write(int numid)
{

numid = IPAddress.HostToNetworkOrder(numid);


stream.Write(BitConverter.GetBytes(numid), 0, 4);
}


private void WriteByte(byte b)
{


stream.WriteByte(b);

}


private void Response()
{

byte[] buff = new Byte[4];

while (tcpClient.Available < 4)


Thread.Sleep(100);


stream.Read(buff, 0, 4);


int resultId = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(buff, 0));


Console.WriteLine("Result: " + resultId);
}
}
}
 
D

Danny Tuppeny

I missed the Main() again:

static void Main(string[] args)
{
Bot b = new Bot();
b.Run();
}

;o)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top