?
Solved

Structs & Sockets

Posted on 1997-04-29
1
Medium Priority
?
326 Views
Last Modified: 2010-04-15
I want to write a struct to a socket and read it into an
identical struct on the other end. How can I achieve this?

It is a simple struct containing only two int vars and one long variable.

I am using Solaris 2.5.1 and SOCK_STREAM/AF_INET sockets
0
Comment
Question by:wcarson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 1

Accepted Solution

by:
prc earned 400 total points
ID: 1250170
Use send and recv as follows:

-- shared structures --

struct Fred
{
  int x;
  int y;
  long l;
};

int s;  /* My socket */
struct Fred fred;

-- at the sending end --

...
  int result = send(s, &fred, sizeof(fred), 0);

  if (result != sizeof(fred))
  {
    ... it failed ...
  }
...

-- at the other end --

...
  int result = recv(s, &fred, sizeof(fred), MSG_WAITALL);
  /* MSG_WAITALL forces it to fill 'fred' before returning */

  if (result != sizeof(fred))
  {
    ... it failed ...
  }
...
-- ends --

These assume that you are using blocking sockets (the usual sort), so that recv doesn't return until it has something to give you.    You can also use your normal file 'read' and 'write' calls in Unix, if this makes you feel happier - just use the socket as a file handle.

But !!BEWARE!!  This is a dangerous road to go down, in the grand scheme of things.  It's all very well if you're talking to another identical machine, or within the same machine, but talk to another type of machine, or something running Windows, or whatever, and the way it represents the structure is going to be different.  The bytes of the long might be in a different order, the length of an 'int' might be different, the structure might be packed differently into memory, etc.etc.  The only reliable way is to encode it into something portable like normal text (using printf, scanf) which is what most Internet protocols do, or some portable binary encoding system like ASN.1.  This is a big subject, and probably more than you were looking for, so I'll leave it as a pointer to think about.

Cheers

Paul
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question