c++ - implementing a non blocking udp socket by select() -
i wanted create asynchronous/non-blocking udp client server application client , server supposed chat away each other without waiting each other's turns. came know done select()...
here server(mentioning communication part):
fd_set readfds,writefds; while(1){ fd_zero(&readfds); fd_zero(&writefds); fd_set(sd,&readfds); fd_set(sd,&writefds); int rv = select(n, &readfds, null, null, null); if(rv==-1) { printf("error in select!!!\n"); exit(0); } if(rv==0) { printf("timeout occurred\n"); } if (fd_isset(sd, &readfds)) { int client_length = (int)sizeof(struct sockaddr_in); memset(&buffer,0,sizeof(buffer)); int bytes_received = recvfrom(sd, buffer,size, 0, (struct sockaddr *)&client, &client_length); if (bytes_received < 0) { fprintf(stderr, "could not receive datagram.\n"); closesocket(sd); wsacleanup(); exit(0); } } printf("\nclient says: %s",buffer); printf("\nwrite :"); fgets(buffer,size,stdin); if(fd_isset(sd,&writefds)) { int client_length = (int)sizeof(struct sockaddr_in); if(sendto(sd, buffer,strlen(buffer), 0, (struct sockaddr *) &client,client_length)<0) { printf("error sending file! \n"); printf("%d\n",wsagetlasterror()); exit(1); } } } closesocket(sd); wsacleanup(); return 0;
}
and client:
fd_set readfds,writefds; while(1) { fd_zero(&readfds); fd_zero(&writefds); fd_set(cs,&readfds); fd_set(cs,&writefds); int rv=select(n,&readfds,&writefds,null,null); if(rv==-1) { printf("error in select!!!\n"); exit(0); } if(rv==0) { printf("timeout occurred\n"); } printf("\nwrite "); fgets(send_buffer,size,stdin); if(fd_isset(cs,&writefds)) { int server_length = sizeof(struct sockaddr_in); fd_clr(cs,&writefds); if (sendto(cs, send_buffer, (int)strlen(send_buffer) + 1, 0, (struct sockaddr *)&server, server_length) == -1) { fprintf(stderr, "error transmitting data.\n"); closesocket(cs); wsacleanup(); exit(0); } } char file_buffer[size]; //reply reception server:"ready receive file" int data2=0; if (fd_isset(cs, &readfds)) { fd_clr(cs,&readfds); int server_length = sizeof(struct sockaddr_in); data2=recvfrom(cs,file_buffer,strlen(file_buffer)-1,0,(struct sockaddr *)&server,&server_length); //file_buffer[data2]='\0'; if(data2<0) { printf("server not on:(\n"); exit(0); } } //printf("%d",data2); printf("\nserver says:"); for(int i=0;i<data2;i++) { putchar(file_buffer[i]); } } return 0; }
at first on server side wrote:int rv = select(n, &readfds, &writefds, null, null);
but led printing of entire empty array on server console when server initialised in addition fact communication between server , client not proper. removing "&writefds" did remove redundant data improper communication issue still persists... thankful if helped me out...
i see couple of problems code.
first of all, if want wait input both socket , terminal, should put both of fds readfds
set:
fd_set(cs, &readfds); fd_set(stdin, &readfds);
since you're not doing writing, shouldn't using writefds
.
next, should make sure you're trying read terminal if has input ready. so, should doing like:
if (fd_isset(stdin, &readfds)) { // read in terminal... }
you're trying read each time, no matter happens.
last, you're measuring size of file_buffer
incorrectly when make recvfrom
call. strlen
works once you've put data it. should using sizeof(file_buffer)
.
Comments
Post a Comment