I am trying to send struct over tcp socket. I am a newbie in socket programming, I did try the options suggested here already but those did not serve my purpose. Could someone pls help?
I have written Server.cpp and Client.cpp and both are compiling properly. However, when I am executing my Server to listen to the Client, I am not sure if the Server is able to recieve the structure from Client or not. Also, how can I read this structure once it is received?
Server.cpp
#include<iostream>
#include <cstring>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<string>
#include<stdio.h>
using namespace std;
int main()
{
        struct UE
        {
        string Net;
        int imsi;
        } ;
        UE UE2;
        //cout<<UE1.imsi<<"\n"<<UE1.Net<<"\n";
        int sock, cli, receive;
        sockaddr_in server, client;
        unsigned int len;
        if((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
        {
                perror("Socket:");
                exit(-1);
        }
        server.sin_family = AF_INET;
        server.sin_port = htons(10000);
        //cout<<htons(10000);
        server.sin_addr.s_addr = INADDR_ANY;
        //cout<<INADDR_ANY;
        memset(&(server.sin_zero), '\0', 8);
        len = sizeof(struct sockaddr_in);
        if((bind(sock, (struct sockaddr *)&server, len)) == -1)
        {
                perror("Bind:");
                exit(-1);
        }
        if((listen(sock, 5)) == -1)
        {
                perror("Listen:");
                exit(-1);
        }
        while(1)
        {
                cli = accept(sock,(struct sockaddr *)&client, &len);
                if(cli == -1)
                {
                        perror("Accept");
                        exit(-1);
                }
                receive = recv(sock, (void*)&UE2, sizeof(UE2), NULL);
                cout<<UE2.imsi;
                //cout<<UE2.imsi<<"\n"<<UE2.Net;
                //int sent = send(cli, (const void*)&mesg, sizeof mesg, 0);
                //cout<<"Sent"<<sent<<" bytes to client :<<inet_ntoa(client.sin_addr)";
                close(cli);
        }
}
Client.cpp
#include <arpa/inet.h>
#include<iostream>
#include <cstring>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<string>
#include<stdio.h>
using namespace std;
int main(int argc, char *argv[])
{
        struct UE
        {
        string Net;
        int imsi;
        } ;
        UE UE1;
        UE1.Net = "4G";
        UE1.imsi = htons(8649);
        int sock, receive;
        struct sockaddr_in server;
        char mesg[200];
        if((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
        {
                perror("Socket:");
                exit(-1);
        }
        server.sin_family = AF_INET;
        server.sin_port = htons(atoi(argv[2]));
        //cout<<server.sin.port;
        server.sin_addr.s_addr = inet_addr(argv[1]);
        //cout<<server.sin_addr.s_addr;
        memset(&(server.sin_zero), '\0', 8);
        if(connect(sock, (struct sockaddr*)&server, sizeof(server)) == -1)
        {
                perror("Connect:");
                exit(-1);
        }
        int count = 0;
        while(count = 0)
        {
        send(sock, &UE1, sizeof(UE1), 0);
        //receive = recv(sock, (void*)&mesg, sizeof mesg, NULL);
        count++;
        }
        cout<<"Sent "<<UE1.imsi<<" and "<<UE1.Net<<" to Server \n";
        close(sock);
}
 
     
     
    