1

I have the following structure in my header file structure.h. Now i need to use this structure in my main.c file.

I need to fill this structure with some values and I need to send this from TCP/IP client to the TCP/IP server on the same system.

#ifndef STRUCTURE_H
#define STRUCTURE_H

typedef struct
{
    unsigned int variable3;
    char variable4[8];
}NUMBER_ONE,*PNUMBER_ONE;


typedef struct
{
    unsigned int variable5;
    char variable6[8];
}NUMBER_TWO,*PNUMBER_TWO;

typedef struct
{
    char name[32];
    unsigned int a;
    unsigned int b;
    NUMBER_ONE variable1;
    NUMBER_TWO variable2;
}NUMBER_THREE,*PNUMBER_THREE;

#endif

I have tried this but, I am not good in C, so please can anyone tell me how to do it, by taking the above structure as an example? Till socket connection establishment is ok for me, but after establishing connection, how do I send this structure from the client to the server?

I am doing this in my Linux Ubuntu 12.04 system.

2
  • 2
    I would not recommend doing that, since different machines can have different byte ordering (Big Endian/Little Endian), structure packing... etc, so the structure might not be correctly decoded on the other side. You should better use an interchange format, you can devise your own, or use one of the many existing formats (JSON, BSON, XML, AMF...) Commented Dec 16, 2013 at 11:03
  • Don't use structs as network protocols. You are introducing half a dozen or so dependencies. Define a wire protocol in octets, and write the code to send and receive it. Commented Dec 16, 2013 at 23:08

1 Answer 1

0

Short example with no error checking:

Server

struct sockaddr_in serv_addr; 

listenfd = socket(AF_INET, SOCK_STREAM, 0);

serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(5000); 
bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)); 

listen(listenfd, 10);

while(1)
{
    connfd = accept(listenfd, (struct sockaddr*)NULL, NULL); 
    snprintf(sendBuff, sizeof(sendBuff), "%.24s\r\n", ctime(&ticks));
    write(connfd, sendBuff, strlen(sendBuff)); 
    close(connfd);
    sleep(1);
 }

Client

 struct sockaddr_in serv_addr; 

 sockfd = socket(AF_INET, SOCK_STREAM, 0);

 serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(5000); 

 inet_pton(AF_INET, argv[1], &serv_addr.sin_addr); 
 connect(sockfd,
 (struct sockaddr *)&serv_addr, sizeof(serv_addr))

   while ( (n = read(sockfd, recvBuff, sizeof(recvBuff)-1)) > 0)
    {
        recvBuff[n] = 0;
        if(fputs(recvBuff, stdout) == EOF)
        {
            printf("\n Error : Fputs error\n");
        }
    }

Pitfalls

Is not "safe" so send plain struct. Sooner or later you will deal with endianness (byte order), packing (which can still be a problem even with #pragma pack) and sizes of types like 'int' that can vary between platforms

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.