c - manage pointer to socket files descriptor -


i wondering if it's possible manage pointer socket udp file descriptor cause have sending data send() 2 different socket simultaneously.

i have function creates 2 sockets (socket() setsocket() bind() connect()... ) , 1 changes socket (if 1 condition true , dind't write part)

  void set_socket(){                                         socket00 = socket(af_inet, sock_dgram, 0);     socket01 = socket(af_inet, sock_dgram, 0);     ...     setsocket();     setsocket();     ....     ....   }     int change_socketfd (int fd) {         if (fd == socket01 ) {                   return ( socket00 );     }     else if (fd == socket00 ) {         return ( socket01 );     }   } 

can have pointer this?

    int *psock;     psock=&change_socketfd(socket00);     ris = send(*psock, datagram, sizeof(*datagram)+1 , 0); 

what need change dynamically socketfd , code gives segmentation fault i'm not sure if method correct or possible . suggests

the way you've done not work, because &change_socketfd(socket00) giving address of return value, exists temporarily. don't see why wouldn't want:

int psock; psock = change_socketfd(socket00); ris = send(psock, datagram, sizeof(*datagram)+1 , 0); 

if need pointer, though, try this:

int *psock = malloc(sizeof(int*)); *psock = change_socketfd(socket00); ris = send(*psock, datagram, sizeof(*datagram)+1 , 0);  ... //when you're finished psock free(psock); 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -