473,726 Members | 2,262 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Collecti ons.Generic;
using System.Text;
using System.Net.Sock ets;

namespace DTuppeny.PokerB ot
{
public class Bot
{
TcpClient tcpClient = new TcpClient();

public void Run()
{
tcpClient.Conne ct("hilo.cs.ual berta.ca", 55006);

Write("0020");
Write("0025");
Write("TestBot\ 0TestBot\01\0Te stBot");

string response = Response();
Console.WriteLi ne(response);

Console.ReadLin e();
}

private void Write(string message)
{
System.Text.ASC IIEncoding en = new System.Text.ASC IIEncoding();

byte[] WriteBuffer = new byte[1024];
WriteBuffer = en.GetBytes(mes sage);

NetworkStream stream = tcpClient.GetSt ream();
stream.Write(Wr iteBuffer, 0, WriteBuffer.Len gth);

System.Diagnost ics.Trace.Write ("WRITE:" + message);
}

private string Response()
{
System.Text.ASC IIEncoding enc = new System.Text.ASC IIEncoding();
byte[] serverbuff = new Byte[1024];
StringBuilder retval = new StringBuilder() ;
NetworkStream stream = tcpClient.GetSt ream();
int count = 0;
while (true)
{
byte[] buff = new Byte[2];
int bytes = stream.Read(buf f, 0, 1);
if (bytes == 1)
{
serverbuff[count] = buff[0];
count++;

if (count >= 1024)
{
retval.Append(e nc.GetString(se rverbuff, 0, count));
count = 0;
}

if (buff[0] == '\n')
{
break;
}
}
else
{
break;
}
}

retval.Append(e nc.GetString(se rverbuff, 0, count));
System.Diagnost ics.Trace.Write ("READ:" + retval.ToString ());
return retval.ToString ();
}

}

}
Nov 17 '05 #1
3 4685
"Danny Tuppeny" <gr****@dannytu ppeny.commmmmm> wrote in message
news:43******** *************** @ptn-nntp-reader02.plus.n et...
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****@dannytu ppeny.commmmmm> wrote in message
news:43******** *************** @ptn-nntp-reader03.plus.n et...

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.Sock ets;
using System.Threadin g;
namespace DTuppeny.PokerB ot
{
public class Bot
{

TcpClient tcpClient = new TcpClient();
Stream stream;

public void Run()
{
tcpClient.Conne ct("hilo.cs.ual berta.ca", 55006);

stream = tcpClient.GetSt ream();

Write(20);
Write(28);
Write("TestBot" );
WriteByte(0);
Write("TestBot" );
WriteByte(0);
Write(1);
Write("TestBot" );
WriteByte(0);
stream.Flush();
Response();
Console.WriteLi ne("end");
Console.ReadLin e();
}
private void Write(string message)
{

byte[] bytes = Encoding.ASCII. GetBytes(messag e);
stream.Write(by tes, 0, bytes.Length);
}
private void Write(int numid)
{

numid = IPAddress.HostT oNetworkOrder(n umid);
stream.Write(Bi tConverter.GetB ytes(numid), 0, 4);
}
private void WriteByte(byte b)
{
stream.WriteByt e(b);

}
private void Response()
{

byte[] buff = new Byte[4];

while (tcpClient.Avai lable < 4)
Thread.Sleep(10 0);
stream.Read(buf f, 0, 4);
int resultId = IPAddress.Netwo rkToHostOrder(B itConverter.ToI nt32(buff, 0));
Console.WriteLi ne("Result: " + resultId);
}
}
}
Nov 17 '05 #4

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

Similar topics

4
8200
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 TcpClient, NetworkStream and StreamWriter classes. but with low level socket it doesn't work (When using the Socket class Send method).
7
5562
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 the best way to move the data from an XmlDocument to a Byte array? Currently I paste the xml into a textbox and send it with the following code: Dim tcpClient As New System.Net.Sockets.TcpClient tcpClient.Connect("127.0.0.1", 10000) Dim...
1
1840
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 getCsv() is started (this receives the file) The file however isn't received completely (most of the time) this is my servercode:
4
2246
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 using base64. When i run it it sends the email ok, but and attchment doesn't work i get an error when i run it. The attchment seems to be bigger (20k) then the orginal (16k) i've included the source which i have been using. Can someone tell me what...
5
9247
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 which printer the label is printed on (ie. which IP address the ASCII string / stream is sent to). How can I send an ASCII string / stream to an IP address??
0
1320
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
4309
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 receiver is running within Process1. If I move the receiver into Process2 then its fast. Please can someone explain.
0
1885
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 have 3 Windows Forms. 1-FrmInquiry 2-FrmSale 3-FrmBalance When i send and receive data from FrmInquiry to server it works fine but My problem is when i go to any other form(ex FrmSale) from FrmInquiry and send data from it to server, it gives...
10
5087
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> <zone>BOXING</zone> <status>Running</status> <job>1000139233</job>
0
9401
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9179
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9116
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8099
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6011
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3228
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2637
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.