• Welcome to Valhalla Legends Archive.
 

[c++] problem with sending data

Started by mentalCo., June 09, 2004, 11:18 PM

Previous topic - Next topic

mentalCo.

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?

Eli_1

#1
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)

mentalCo.

size is two bytes? how does that work?  just the size as first byte then 00 as second?

Eli_1

#3
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

mentalCo.

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::

Eli_1


Maddox

Should be FF 0E 0A 00 41 41 41 41 41 00

The size is an unsigned short (WORD) in little endian byte order.
asdf.

Moonshine

#7
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

Eli_1

#8
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.

Moonshine


mentalCo.

#10

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

Moonshine

#11
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
);

mentalCo.

#12
oy... ok the screenname im using is mentalco.  when i send text all that shows up in channel is "<mentalco.> "  with no text.

Moonshine

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.

mentalCo.

#14
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";}

}