Valhalla Legends Archive

Programming => Battle.net Bot Development => Topic started by: mentalCo. on June 09, 2004, 11:18 PM

Title: [c++] problem with sending data
Post by: mentalCo. on June 09, 2004, 11:18 PM
ok im connected and in a channel... when i send this exact packet: "FF 0E 09 41 41 41 41 41 00" nothing happens... theoretically it should send 5 'A's to the channel... am i doing this wrong?
Title: Re:[c++] problem with sending data
Post by: Eli_1 on June 09, 2004, 11:25 PM
I believe your packet header is wrong.

It goes like this:

Byte 1 = FF
Byte 2 = Packet ID (0E in your example)
Bytes 3-4 (WORD) = Packet size including this header

Then your null-terminated string.

[Edit]
The dump of your packet shoud actually look like this:
FF 0E 00 09 41 41 41 41 41 00       ÿ...AAAAA.

(I think)
Title: Re:[c++] problem with sending data
Post by: mentalCo. on June 09, 2004, 11:29 PM
size is two bytes? how does that work?  just the size as first byte then 00 as second?
Title: Re:[c++] problem with sending data
Post by: Eli_1 on June 09, 2004, 11:36 PM
Yes, two bytes - allowing for a much larger packet.

[Edit]
Assuming you're using a packetbuffer class, it probably adds the header on for you.


PacketBuffer.InsertNTString "AAAAA"
PacketBuffer.SendPacket &H0E
Title: Re:[c++] problem with sending data
Post by: mentalCo. on June 09, 2004, 11:40 PM
damn this is complicated... how would i put together a packet then lol.  can i put all the bytes into a string then put that into a char then send it.  no that wouldnt work... ::pulls hair out::
Title: Re:[c++] problem with sending data
Post by: Eli_1 on June 09, 2004, 11:41 PM
I forgot this was C++ - oops.
Title: Re:[c++] problem with sending data
Post by: Maddox on June 10, 2004, 12:06 AM
Should be FF 0E 0A 00 41 41 41 41 41 00

The size is an unsigned short (WORD) in little endian byte order.
Title: Re:[c++] problem with sending data
Post by: Moonshine on June 10, 2004, 12:11 AM
You're going to want to store the packet size in a 2 byte (16-bit) variable.  In C++ this means (assuming you're using win32 or linux, and a fairly recent processor/compiler) an  unsigned short variable.

So you'll want something like this:

FF 0E 0A 00 41 41 41 41 41 00      ÿ...AAAAA.

Note that it's not 00 0A, eli, since we're using little endian here.

Here's a quick example I whipped up to show you the basics of forming a bnet packet in C++:


unsigned short PacketSize = 10;

unsigned char szBuffer[MAXSIZE] = ""; // initialize a canned buffer (unsigned char since we're dealing with >127 ascii character values)
szBuffer[0] = 0xFF; // insert magic&packetID
szBuffer[1] = 0x0E;
*(unsigned short *)(szBuffer + 2) = PacketSize; // insert packet size (ushort) starting at buffer[2]
strcpy((char *)szBuffer + 4, "AAAAA"); // copy the string onto the end of the buffer (just past the header)

/* do error checking here */  send(mysock, szBuffer, PacketSize, 0); // Send data off to bnet


Also note putting regular packet construction routines in a class would be highly recommended (similar to DM's packet buffer thingy (tm))

Edit: maddox correct eli at the same time, sorry for the repeat
Title: Re:[c++] problem with sending data
Post by: Eli_1 on June 10, 2004, 12:16 AM
Thanks for correcting me, I thought I might have had it wrong.  :-\

Blah, I also just realized I wasn't counting the null character, which is why I had 9 and not 10.
Title: Re:[c++] problem with sending data
Post by: Moonshine on June 10, 2004, 12:18 AM
An honest mistake   :)
Title: Re:[c++] problem with sending data
Post by: mentalCo. on June 10, 2004, 12:23 AM

when i compile i get error that send() cant use unisigned char, it needs just char.  what to do?
Title: Re:[c++] problem with sending data
Post by: Moonshine on June 10, 2004, 12:31 AM
Quote from: mentalCo. on June 10, 2004, 12:23 AM

when i compile i get error that send() cant use unisigned char, it needs just char.  what to do?

Oh sorry, i forgot to account for the needed typecast (I believe it's actually a void * argument in send() on linux?)

so you'll need to put  send(mysock, (char *) szBuffer, PacketSize, 0);

Edit: Yes, on linux it's const void* :


*nix/bsd:

ssize_t
    send(int s, const void *msg, size_t len, int flags);

VS.

win32:
int send(
 SOCKET s,
 const char* buf,
 int len,
 int flags
);
Title: Re:[c++] problem with sending data
Post by: mentalCo. on June 10, 2004, 12:33 AM
oy... ok the screenname im using is mentalco.  when i send text all that shows up in channel is "<mentalco.> "  with no text.
Title: Re:[c++] problem with sending data
Post by: Moonshine on June 10, 2004, 01:13 AM
Quote from: mentalCo. on June 10, 2004, 12:33 AM
oy... ok the screenname im using is mentalco.  when i send text all that shows up in channel is "<mentalco.> "  with no text.

Are you sure you're changing the PacketSize variable to account for your different lengthed text messages? If you have "VERY LONG MESSAGE HERE," you'll definitely need to increase the size of PacketSize accordingly.
Title: Re:[c++] problem with sending data
Post by: mentalCo. on June 10, 2004, 02:53 AM
do i have to convert packetsize to a hex value?

ok i can get it to say a message 5 letters long... but if i type, for example, "hellohello" it will replace the first 5 letters with "?"...  so it will look like "<mentalco.>?hello" heres the code...



void SEND(char szText[500])
{
unsigned short PacketSize = strlen(szText)+5;
szText[strlen(szText)]='\n';
unsigned char szBuffer[MAXSIZE] = ""; // initialize a canned buffer (unsigned char since we're dealing with >127 ascii character values)
szBuffer[0] = 0xFF; // insert magic&packetID
szBuffer[1] = 0x0E;
*(unsigned short *)(szBuffer + 2) = PacketSize; // insert packet size (ushort) starting at buffer[2]
strcpy((char *)szBuffer + 4, szText); // copy the string onto the end of the buffer (just past the header)
//if((send(sockfd, (char *)szBuffer, PacketSize, 0))==-1){cout<<"disconnected";}

}


Title: Re:[c++] problem with sending data
Post by: UserLoser. on June 10, 2004, 08:52 AM
Quote from: mentalCo. on June 09, 2004, 11:18 PM
ok im connected and in a channel... when i send this exact packet: "FF 0E 09 41 41 41 41 41 00" nothing happens... theoretically it should send 5 'A's to the channel... am i doing this wrong?

Remember that Battle.net doesn't echo back your own chat messages, so you would have to display your name and your message without an event being triggered
Title: Re:[c++] problem with sending data
Post by: mentalCo. on June 10, 2004, 02:16 PM
ya...

my only problem now is that it takes the first 5 letters of whatever i send and replaces them with a question mark followed by the rest of the text.  has this happened to anyone?
edit

i fixed it heres my code


void SEND(char szText[500])
{

unsigned short PacketSize = strlen(sBuf);
szText[strlen(sBuf)]='\n';
unsigned char szBuffer[MAXSIZE] = ""; // initialize a canned buffer (unsigned char since we're dealing with >127 ascii character values)
szBuffer[0] = 0xFF; // insert magic&packetID
szBuffer[1] = 0x0E;
*(unsigned short *)(szBuffer + 2) = PacketSize; // insert packet size (ushort) starting at buffer[2]
sprintf((char *)szBuffer + 3, szText); // copy the string onto the end of the buffer (just past the header)
if((send(sockfd, (char *)szBuffer, PacketSize+5, 0))==-1){cout<<"ERRRRRRRROOOOR\n\nasdfasdfasd";}

}