Solved

Structs & Sockets

Posted on 1997-04-29
1
316 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
1 Comment
 
LVL 1

Accepted Solution

by:
prc earned 200 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now