Link to home
Start Free TrialLog in
Avatar of kamalkgarg
kamalkgarg

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of sunnycoder
sunnycoder
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It would make more sense to print the number of bytes read instead of entire buffer.
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.
}
>make sure your buffer is unsigned, or you may have your hex results mangled with memory addresses.
why would that happen?