473,386 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Sending null character via TcpClient?

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

}

}
Nov 17 '05 #1
3 4648
"Danny Tuppeny" <gr****@dannytuppeny.commmmmm> wrote in message
news:43***********************@ptn-nntp-reader02.plus.net...
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*
Nov 17 '05 #2
"Danny Tuppeny" <gr****@dannytuppeny.commmmmm> wrote in message
news:43***********************@ptn-nntp-reader03.plus.net...

I missed the Main() again:

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

;o)
Nov 17 '05 #3
<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);
}
}
}
Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: yaron | last post by:
Hi, I have a problem when sending data over TCP socket from c# client to java server. the connection established ok, but i can't send data from c# client to java server. it's work ok with...
7
by: Bob Garbados | last post by:
I need to construct an xml document, send it to a service over tcp/ip to a specified port, receive the xml response, and process the xml response. I can create the xml document to send, but what's...
1
by: Henk | last post by:
Hi, I have a problem with sending a file. I have a client application that connects to a server and sends the string "data". Then it sends a file. If the server receives "data", the method...
4
by: Robert McNally | last post by:
Hello, I'm currently learning c# and have been trying to write a simple program with sockets. The problem is i'm trying to send an email with an attachment, (which is the program itself) by...
5
by: Paul Aspinall | last post by:
Hi I want to send an ASCII character string / stream to an IP address. I basically have 6 barcode printers, and a web interface. Depending on what is entered on the web page, will determine...
0
by: Lee | last post by:
Hi, I am trying to send and receive a file (it is actually an xml file, however any file may possibly be sent) using;- sending ..... public void send() {
3
by: BuddyWork | last post by:
Hello, Could someone please explain why the Socket.Send is slow to send to the same process it sending from. Eg. Process1 calls Socket.Send which sends to the same IP address and port, the...
0
by: danishce | last post by:
Hello All: I am developing a client/server application using vb.net winsock programming. I am sending data to Server and at the same time receive the incoming data from the server. My application...
10
by: Markgoldin | last post by:
I am sending an XML data from not dontnet process to a .Net via socket listener. Here is a data sample: <VFPData> <serverdata> <coderun>updateFloor</coderun> <area>MD2</area>...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.