C++ Socket Programming sending structure

Hi there!!
I was trying to send a structure through sockets and I was having these problems:

1. I was passing an int value of 5 but when it went to the server side, the value is changing (This is where I'm making the mistake I suppose)
2. Similarly if I want to pass a bool value , How can I do it?? I mean we are using htons for int, so similary how can I pass a boolean value?
3.At the end, after execution it is also giving a "Segmentation Error" at the end.


Here I'm including my code. Please go through this.

Client Side Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include<iostream>
#include <cstring>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<string>
#include<stdio.h>
#include<unistd.h>
#include<arpa/inet.h>
using namespace std;
int main(int argc, char *argv[])
{
        struct UE
        {
                string Net;
                int imsi;
        } ;
        UE UE1;
        UE1.Net = "4G";
        UE1.imsi = htons(5);
        int sock, receive;
        struct sockaddr_in server;
        char mesg[200];
        sock = socket(PF_INET, SOCK_STREAM, 0);
        perror("Socket:");

        server.sin_family = AF_INET;
        server.sin_port = htons(10001);
        server.sin_addr.s_addr = INADDR_ANY;
        memset(&(server.sin_zero), '\0', 8);

        connect(sock, (struct sockaddr*)&server, sizeof(server));
        perror("Connect:");

        int count = 0;
        send(sock, &UE1, sizeof(UE1), 0);
        perror("send");
        cout<<"Sent "<<UE1.imsi<<" and "<<UE1.Net<<" to Server \n";
        close(sock);
}



Server Side Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include<iostream>
#include <cstring>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<string>
#include<stdio.h>
#include<unistd.h>
using namespace std;
int main()
{
        struct UE
        {
                string Net;
                int imsi;
        } ;
        UE UE2;

        int sock, cli, receive;
        struct sockaddr_in server, client;
        unsigned int len;
        sock = socket(AF_INET, SOCK_STREAM, 0);
        perror("Socket:");

        server.sin_family = AF_INET;
        server.sin_port = htons(10001);
        server.sin_addr.s_addr = INADDR_ANY;
        memset(&(server.sin_zero), '\0', 8);

        len = sizeof(server);
        bind(sock, (struct sockaddr *)&server, len);
        perror("Bind:");

        listen(sock, 1);
        perror("Listen:");

        cli = accept(sock,(struct sockaddr *)&client, &len);
        perror("accept");
        receive = recv(cli, ( void*)&UE2, sizeof(UE2), 0);
        perror("recv");

        cout << "rec = "<<receive<<endl;
        cout<<UE2.imsi<<"\n";
        close(sock);
        perror("close");
}


Thanks
Last edited on
1
2
3
4
5
        struct UE
        {
                string Net;
                int imsi;
        } ;

A string contains a pointer to its data which is on the heap. When you send the UE structure, you're sending the pointer, which, on the server side, won't point to anything meaningful.

You need to serialize the structure. At work, we use XDR. It's old but fast and lightweight.
Whatever you use, I suggest that you write functions to serialize and deserialize the data. Then use those when sending and receiving.

we are using htons for int

Where? I don't see it in your code.

And shouldn't that be htonl? htons() converts a short.
Thanks dhayden for the reply.

I'll try to look into XDR, I'm fairly new to programming so I might ask some dumb questions.

Regarding the question Where am I using htons?
In Client Side code in line 21, UE1.imsi = htons(5);

Can you please give me an example of XDR or a link where I can see some examples and quickly understand the concept.

Thanks for your time.
The simpliest way to transmit the data is string using stringstream. See:

http://www.cplusplus.com/reference/sstream/stringstream/?kw=stringstream

It is slightly more data, but usually just a few bytes. The advantage is that you can treat a transmission like a file.
Topic archived. No new replies allowed.