0

Below program recieves/captures the packet from the internet and writes into a file (logfile). In my other program i use read() func instead of recvfrom(),but just sending the the buffer doesn't process the packets correctly. Hence i thought of sending two more fields which are in the recvfrom function

int main()
{
int saddr_size,data_size;
struct sockaddr saddr;

unsigned char *buffer = (unsigned char *) malloc(1024);    
   while(1)
   { 

    saddr_size = sizeof saddr;
    //Receive a packet      
    data_size = recvfrom(sock_raw , buffer , 1024, 0 , &saddr ,(socklen_t*)&saddr_size);
    int cont= write(logfile,buffer,data_size);            

   }        
return 0;
}

In the above program, i need to define structure to &saddr and (socklen_t*)&saddr_size). that is instead of sending just the buffer i need to send value of &saddr and (socklen_t*)&saddr_size). How can i do it?i mean to say how to define struct to &sddr and (socklen_t*)&saddr_size)?? please somebody guide me.

Beginner
  • 286
  • 4
  • 17
  • 1
    [Please don't cast the return value of `malloc()` in C](http://stackoverflow.com/a/605858/28169). – unwind Nov 19 '13 at 11:04
  • 1
    never do `(socklen_t*)&saddr_size`; declare `socklen_t saddr_size`. Your problem will have undefined behavior else when `socklen_t` and `int` have a different internal representation. – ensc Nov 19 '13 at 11:08

1 Answers1

1

No you don't send those fields, those are automatically filled in by the recvfrom function from the information in the received packet.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621