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
Post a Comment