Question

I'm using the SDL_net sockets API to create a server and client. I can easily read a string buffer, but when I try to send hexadecimal data, recv gets the length, but I cannot seem to be a able to read the buffer contents.

IPaddress ip;
TCPsocket server,client;
int bufSize = 1024;
char message[bufSize];
int len;

server = SDLNet_TCP_Open(&ip);
client = SDLNet_TCP_Accept(server);
len = SDLNet_TCP_Recv(client,message,bufSize);

Here's a snippet. the buffer length "len" is set (i.e. message length) but I can't get to the data contents in the message buffer. Some sample bind_transmitter PDU data was sent by a random client to the server at that port. I can't read the PDU (SMPP).

Was it helpful?

Solution

My suggestion would be to first check what's going on on the wire with a sniffer like tcpdump or wireshark. Check that the bytes sent conform to any SMPP PDU. If that is OK, then dump the buffer read with SDLNet_TCP_Recv() using the hexdump(3) and see if it matches.

Some notes on the code:

  • I don't see ip initialized anywhere, but I'm guessing you just skipped that part when pasting the code.
  • int bufSize = 1024; char message[bufSize]; is only valid in C99. Assuming GCC, always compile with at least -Wall -pedantic to catch all warnings.
  • The buffer is on the stack, so if you pass it to any function up the call chain, the result is Undefined Behavior.

I would also try reproducing this with plain Berkeley sockets, which are not that much more difficult then SDL, but much more fun :)

OTHER TIPS

use this snippet in c++ for an unformatted dump of a hex stream, previously saved to a buffer

#include <ctype.h>
#include <stdio.h>
void hexdumpunformatted(void *ptr, int buflen) {
  unsigned char *buf = (unsigned char*)ptr;
  int i, j;
  for (i=0; i<buflen; i++) {
    printf("%02x ", buf[i]);
    printf(" ");
  }
}

call with

hexadumpunformatted(buffer, bytecount);

adapted from epatel's excellent snippet (this one gives you a full formatted dump).

I had the exact same issue as you this week. I have been testing a server/client combo for essentially sending text messages. I figured I would do it this way before moving to hex, just to test the waters so to speak.

But moving to binary was most definitely a pain. What worked best for me was a buffer like this.(Just for recv(ing), I used another buffer to combine all the recv(ed) pieces together)

unsigned char Buffer[data_size];
memset(Buffer, 0, data_size);

Doing it this way worked very well for my purposes, a single byte of ff would print out nicely

0xff

as opposed to only being a buffer of signed chars which would leave me with

0xffffffff

So in terms of minimizing code, it was nice not having to finagle any formatting.

The trick is that you also have to be transmitting in binary. You can't expect to send text and just look at it's binary form on the receiving end. It definitely takes some experimentation, but I was finally able to send a document from my client to server just now.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top