Hi i want to wait the first message of a client as a server with a UDP Protocol so i send a message when I create the client socket and I use a recvfrom to get the message and the adress of the client.It works pretty good I can read the message in my buffer but after this operation I try to connect the server to the client address but this operation return a value of -1 for the connect process and I don't understand the reason.Here is my function wait for client the error is at the end when I try to connect to clientaddr.
int wait_for_client(int sfd){
struct sockaddr_in clientaddr;
int len;
char buf[32];
len = sizeof(clientaddr);
int rec_stat = recvfrom(sfd, buf, 32, 0, (struct sockaddr*)&clientaddr, (socklen_t *)&len);
if(rec_stat<0){
printf("echec de receive");
fflush(stdout);
return -1;
}
printf("%s\n",buf);
fflush(stdout);
int connect_status = connect(sfd,(struct sockaddr *)&clientaddr,sizeof(clientaddr));
if(connect_status == -1){
printf("failed to connect to client\n");
fflush(stdout);
return -1;
}
printf("%s\n",buf);
return sfd;
}
The problem was because my clientaddr is declared as sockaddr_in but my client was created as sockaddr_in6. So the mistake was the struct of my clientaddr
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.