Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
06-07-2007, 01:31 PM
|
#1
|
Member
Registered: Feb 2004
Location: Athens, Greece
Distribution: Gentoo,FreeBSD, Debian
Posts: 705
Rep:
|
C: convert char buffer received from socket to host endianess buffer
Hi,
i receive a buffer from a socket (a char one) and i want to process it. The buffer is probably in "network byte order". How do i change -if there is need to- its byte order? Is there something like htons and ntohs, but for the whole char buffer?
Thanks
|
|
|
06-07-2007, 04:48 PM
|
#2
|
Member
Registered: Jan 2005
Distribution: Archlinux, Debian, Centos
Posts: 222
Rep:
|
Good questions. Endian-ness is important for numbers. (maybe for multibyte character sequences also, i am not sure.) Can you tell what kind of data you are recieving? If it's just a character sequence you may give them out with printf and check.
|
|
|
06-07-2007, 05:01 PM
|
#3
|
Member
Registered: Feb 2004
Location: Athens, Greece
Distribution: Gentoo,FreeBSD, Debian
Posts: 705
Original Poster
Rep:
|
I am receiving a TFTP packet and i want to read its fields:
Code:
2 bytes string 1 byte string 1 byte
------------------------------------------------
| Opcode | Filename | 0 | Mode | 0 |
------------------------------------------------
For example i would like to read the filename.
|
|
|
06-07-2007, 05:06 PM
|
#4
|
Member
Registered: Jan 2005
Distribution: Archlinux, Debian, Centos
Posts: 222
Rep:
|
You may do it like this:
Code:
char buf[1024];
read(sockfd, buf, 1024);
char* pbuf=buf;
short hboOpcode=ntohs((short)pbuf);
pbuf+=2;
int lens1=strnlen(pbuf,MAX_LEN);
// copy the string..
pbuf+=lenst+1;
int lenst2=strnlen(pbuf,MAX_LEN);
|
|
|
All times are GMT -5. The time now is 08:01 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|