Solved

Structs & Sockets

Posted on 1997-04-29
1
318 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

The problems with reply email signatures

Do you wish that you could place an email signature under a reply? Well, unfortunately, you can't. That great Exchange/Office 365 signature you've created will just appear at the bottom of an email chain. What a pain! Is there really no way to solve this? Well, there might be...

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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 opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

914 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

18 Experts available now in Live!

Get 1:1 Help Now