Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
hey
I wanted to create an asynchronous/non-blocking udp client server application where the client and server were supposed to chat away with each other without waiting for each other's turns.
I came to know that this could be done by select()...


here is my Server(Mentioning only the communication part):
C++
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 the file! \n");
            printf("%d\n",WSAGetLastError());
            exit(1);
        }
            }


    }
    closesocket(sd);
    WSACleanup();

    return 0;

}


and this is my client:
C++
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))
	
	   
		
			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 from the server:"Ready to 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 is 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 the server side I wrote:
int rv = select(n, &readfds, &writefds, NULL, NULL);


but that led to the printing of an entire empty array on the server console when the server initialised in addition to the fact that communication between the server and client was not proper.
removing "&writefds" did remove the redundant data but the improper communication issue still persists...
So I would be really thankful if somebody helped me out...
Posted
Updated 19-Apr-13 22:23pm
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900