Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C sockets and structures 1

Status
Not open for further replies.

nphani

Technical User
Feb 10, 2002
104
US
Hi,
I want to create a client server program which passes structures between them. That is, the server should pass a structure and the client should read it. Both the server and client know the contents of the structure and it's size, so they know how much to write and how much to read.
Can we pass structures through sockets?
If yes, will the reading client, read it as a set of characters (or buffer) or as structure itself? Can we associate the content read with some structure?

Any help would be highly appreciated.
Phani
 
Yes and No. Structures are really just a block of memory and if you cast the address of the structure to a void* then it's just that.

However if a member of the structure is a pointer to another block of memory then you must account for it's data as well.

Also numeric types like int, long, float, double have byte ordering that are different on various platforms. float and double are even more complex in that they are complex types. Byte ordering functions are supplied by most TCP/IP library implementations to help with the byte ordering issue but you must understand the problem so you can address it correctly.

With that said, find some resources on the net that explain these issues and contain sample code for your platforms. If your using any Windows platforms look at
So t he bottom line is that you can read and write structures across socket connections but there are some things to consider based on the contents of your structure and the platforms you are using.


-pete
 
Yea,

Just cast it to void * at the server / sender end and at the receiver /Client end receive it as a structure * .

Make sure there are no underflows or overflows. Just use a simple program to decipher what you receive is what you need and typecast it if necessary.

-Sesha
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top