Solved

Structs & Sockets

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

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them 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.

730 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