LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 06-07-2007, 02:31 PM   #1
kpachopoulos
Member
 
Registered: Feb 2004
Location: Athens, Greece
Distribution: Gentoo,FreeBSD, Debian
Posts: 705

Rep: Reputation: 30
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
 
Old 06-07-2007, 05:48 PM   #2
traene
Member
 
Registered: Jan 2005
Distribution: Archlinux, Debian, Centos
Posts: 219

Rep: Reputation: 33
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.
 
Old 06-07-2007, 06:01 PM   #3
kpachopoulos
Member
 
Registered: Feb 2004
Location: Athens, Greece
Distribution: Gentoo,FreeBSD, Debian
Posts: 705

Original Poster
Rep: Reputation: 30
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.
 
Old 06-07-2007, 06:06 PM   #4
traene
Member
 
Registered: Jan 2005
Distribution: Archlinux, Debian, Centos
Posts: 219

Rep: Reputation: 33
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);
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the difference between the free buffer and buffer in the buffer hash queue? Swagata Linux - Enterprise 0 05-26-2006 12:57 AM
fseek on a socket descriptor to discard socket buffer? Thinking Programming 1 12-06-2005 10:15 PM
socket buffer TCP lucs Slackware 0 05-05-2005 08:26 AM
Squid: "Socket error... (55) no buffer space available J_Szucs Linux - Networking 1 08-30-2004 09:10 AM
Socket buffer size problem slowman Programming 2 06-08-2003 03:06 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:24 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration