Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining structure in c socket program

Tags:

c

sockets

data_size = recvfrom(sock_raw , buffer , 1024, 0 , &saddr ,
                                           (socklen_t*)&saddr_size);

here is a recvfrom function, where i want to create a struct to 'saddr' and 'saddr_size' which is declared in the main() as follows

struct sockaddr saddr;
int saddr_size;
saddr_size = sizeof saddr;

and instead of buffer mentioned in the below write() api i have to use the struct variable.

int cont= write(logfile,buffer,data_size);

My question is: Below i have used structure.Is this the way to define struct for the following fields? Is it declared correctly? if not please somebody guide me to correct it.

struct data{ 
  unsigned char buffer[1024];
  unsigned long int saddr; 
  //   struct sockaddr saddr;
  int saddr_size;
}; 
like image 270
Beginner Avatar asked Nov 20 '13 06:11

Beginner


1 Answers

struct data { 
  unsigned char buffer[1024];
  struct sockaddr saddr;
  socklen_t saddr_size;
};
like image 72
abasterfield Avatar answered Oct 11 '22 15:10

abasterfield