Solved

Structs & Sockets

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How do I test for current date? 9 102
What is sub-make ? 2 58
delete-remove 14 84
Resolve Dependency Issues 4 64
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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
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 nested-loops in the C programming language.

813 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

15 Experts available now in Live!

Get 1:1 Help Now