• C

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

kamalkgargAsked:
Who is Participating?
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.