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.