Using select() for client UDP

This is a discussion on Using select() for client UDP within the C Programming forums, part of the General Programming Boards category; Hello, I have a program running properly which utilizes a UDP connection to send data back and forth. I want ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    2

    Using select() for client UDP

    Hello,

    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:

    Code:
    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[]= "129.187.159.139";			/* Server IP address */
    	int PORT = 8000;				/* Server Port */
    	
    	/* For the select() program */
    	struct timeval tv;
    	fd_set wfds;
    	FD_ZERO(&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);
    	sprintf(posiz,"%e",posz);
    	
    	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
    Posts
    2
    bump

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    http://cboard.cprogramming.com/annou...t.php?f=4&a=51
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 08:53 AM
  3. multiple UDP sockets with select()
    By nkhambal in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-17-2006, 06:36 PM
  4. Directional Keys - Useing in Console
    By RoD in forum C++ Programming
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21