Thread: Using select() for client UDP

  1. #1
    Registered User
    Join Date
    Mar 2007

    Using select() for client UDP


    I have a program running properly which utilizes a UDP connection to send data back and forth. I want to set it up so that the program may check whether the UDP server is currently receiving and if so send the data and if it isn't running to just continue as normally.

    What happens currently is that the program only runs fully if the server receives the data and if the server isn't receiving the program just sits there trying to send the data forever.

    I tried this code but it doesn't work:

    void UDPcli(void)
    	int sd, fdmax;						/* The socket descriptor */
    	int server_length;				/* Length of server struct */
    	struct hostent *hp, *h;				/* Information about the server */
    	struct sockaddr_in server;			/* Information about the server */
    	struct sockaddr_in client;			/* Information about the client */
    	char posix[64], posiy[64], posiz[64];		/* Positions to be sent to server */
    	char IPad[]= "";			/* Server IP address */
    	int PORT = 8000;				/* Server Port */
    	/* For the select() program */
    	struct timeval tv;
    	fd_set wfds;
    	tv.tv_sec = 0;
    	tv.tv_usec = 200000;
    	/* Converting IP address to usable structure for UDP connection */
    	h = gethostbyname(IPad);
    	/* Open a datagram socket */
    	sd = socket(AF_INET, SOCK_DGRAM, 0);
    	FD_SET(sd, &wfds);
    	/* Set Server family and port */
    	server.sin_family = AF_INET;
    	server.sin_port = htons(PORT);
    	inet_ntoa(*(struct in_addr *)h->h_addr_list[0]);
    	memcpy((char *) &server.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
    	/* Set Client family, port & address */
    	client.sin_family = AF_INET;
    	client.sin_port = htons(0);
    	client.sin_addr.s_addr = htonl(INADDR_ANY);
    	/* Bind local address to socket */
    	bind(sd, (struct sockaddr *)&client, sizeof(struct sockaddr_in));
    	/* Receive positions from Haptic-Master */
    	pHapticMaster->GetParameter(FCSPRM_POSX, posx);
    	pHapticMaster->GetParameter(FCSPRM_POSY, posy);
    	pHapticMaster->GetParameter(FCSPRM_POSZ, posz);
    	/* Convert positions from 'double' to 'char' so that they can be sent over UDP */
    	sprintf(posix,"%e ",posx);
    	sprintf(posiy,"%e ",posy);
    	server_length = sizeof(struct sockaddr_in);
    	/* Checking whether server program is running and sending/receiving positions accordingly */
    	if (select(sd+1, NULL, NULL, NULL, &tv) <= 0){
    	} else {	
    		/* Transmit positions */
    		sendto(sd, posix, sizeof(posix), 0, (struct sockaddr *)&server, server_length);
    		sendto(sd, posiy, sizeof(posiy), 0, (struct sockaddr *)&server, server_length);
    		sendto(sd, posiz, sizeof(posiz), 0, (struct sockaddr *)&server, server_length);
    Any help would be great! Thanks.

  2. #2
    Registered User
    Join Date
    Mar 2007

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Rule 5.

    You can start by checking all the return values of all the functions which don't return void.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  2. simultaneously waiting for data on FIFO and UDP using select call
    By yogesh3073 in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-05-2007, 09:53 AM
  3. multiple UDP sockets with select()
    By nkhambal in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-17-2006, 07:36 PM
  4. Directional Keys - Useing in Console
    By RoD in forum C++ Programming
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM