Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 424
  • Last Modified:

C program to print octets from receiving from a socket

Hi,

Can someone please tell me how to print the octets (in hex format) of data received from socket in linux using C programming?

the data received is stored in pipebuf.

Thanks
read(pipe_fd, &rgptr, sizeof(rgptr));
memcpy(pipebuf, rgptr, config.buffer_size);

Open in new window

0
kamalkgarg
Asked:
kamalkgarg
  • 3
1 Solution
 
sunnycoderCommented:
for (i=0; i<config.buffer_size; i++)
{
   int j = rgptr[i];
   printf("%x ",j);
}
0
 
sunnycoderCommented:
It would make more sense to print the number of bytes read instead of entire buffer.
0
 
null0bjectCommented:
make sure your buffer is unsigned, or you may have your hex results mangled with memory addresses.

unsigned char buf[2048];
int recvLen;
int i;

//*initialize socket

recvLen = recv(socket,buf,sizeof[buf]);

for (i=0; i < recvLen; i++) {
     printf("%02x ", buf[i]);   //*space after the 'x' to make readable.
}
0
 
sunnycoderCommented:
>make sure your buffer is unsigned, or you may have your hex results mangled with memory addresses.
why would that happen?
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now