Results 1 to 3 of 3
I have a simple question about passing character pointers through sockets.
example code/data
Code:
struct mystr
{
unsigned int one;
unsigned two two;
char *ch;
};
unsigned int strsize;//character array ...
- 12-14-2009 #1
Network Programming - Passing Character Pointers
I have a simple question about passing character pointers through sockets.
example code/data
Lets say I have a data structure like the above definition, the way I would pass this data set would be to:Code:struct mystr { unsigned int one; unsigned two two; char *ch; }; unsigned int strsize;//character array size struct mystr thestr; thestr.one = 1234; thestr.two = 5678; thestr.ch = (char*)malloc(strsize * sizeof(char)); ...copy data to new char pointer - thestr.ch
Now I'm able to recreate the data on the server side with no problems but is this the proper way of passing a structure that contains a character array?Code:write(serverfd, (char*)&thestr, 2 * sizeof(unsigned int));//write both unsigned int one and two write(serverfd, (char*)&strsize, sizeof(unsigned int));//send the size of the character array write(serverfd, thestr.ch, strlen(thestr.ch));//write the character array
The textbook I have has very info on this(none that I can find) and I can't find anything Googling.Make mine Arch Linux
- 12-16-2009 #2Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
This will work if (and only if) both systems (client and server) are running the same hardware and operating system. Typically, for network programming, non-character data, such as the integer parameters, are converted to/from network byte order. That way, a client can be a little endian system, such as Intel x86, and the server can be a big endian system, such as a Sun Sparc.
The following is the Linux man page for the functions that let you do this properly:
Code:BYTEORDER(3) Linux Programmer’s Manual BYTEORDER(3) NAME htonl, htons, ntohl, ntohs - convert values between host and network byte order SYNOPSIS #include <arpa/inet.h> uint32_t htonl(uint32_t hostlong); uint16_t htons(uint16_t hostshort); uint32_t ntohl(uint32_t netlong); uint16_t ntohs(uint16_t netshort); DESCRIPTION The htonl() function converts the unsigned integer hostlong from host byte order to network byte order. The htons() function converts the unsigned short integer hostshort from host byte order to network byte order. The ntohl() function converts the unsigned integer netlong from network byte order to host byte order. The ntohs() function converts the unsigned short integer netshort from net- work byte order to host byte order. On the i80x86 the host byte order is Least Significant Byte first, whereas the network byte order, as used on the Internet, is Most Significant Byte first. CONFORMING TO POSIX.1-2001. Some systems require the inclusion of <netinet/in.h> instead of <arpa/inet.h>. SEE ALSO gethostbyname(3), getservent(3) BSD 1993-04-15 BYTEORDER(3)Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 12-16-2009 #3
Thanks for the reply Rubberman. I almost(well I did) forgot about byte order.
So as far as I can tell passing pointers through a socket is a matter of a common data set between sender and receiver.i.e receiver expects a data set of a certain format and the sender is expected to send a data set in that 'certain' format.
Thanks again for the heads up on bytes order.Make mine Arch Linux


Reply With Quote