- Can the sendto timeout be set the time for me?
- normal the sendto timeout time is 4~5 sec,but I want set the timeout for 10 msec
3.Where the program error?
follow is my program:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <pthread.h>
#include <sched.h>
#include <sys/neutrino.h>
#include <sys/time.h>
#include <sys/timeb.h>
#define MYSENPORT 6001 // the port users will be connecting to
#define RECVPORT 6001
#define MAXBUFLEN 1000
int sockfd;
struct sockaddr_in their_addr; // connector's address information
struct hostent *he;
int numbytes;
int argc;
char *argv[2];
int sockrecv;
struct sockaddr_in my_addr; // my address information
struct sockaddr_in talker_addr; // connector's address information
int addr_len;
char buf[MAXBUFLEN];
char word1[13] = "192.168.0.3";
char word2[10]= "abcdefghij";
char word3[10];
struct timeval tv;
socklen_t cmsg_len;
struct timeb timebuf;
char *now;
int r_inf, len, f_len ;
struct sockaddr_in from ;
struct sockaddr_in recv_test; // connector’s address information
#define settimeout_debug 1
int main()
{
/////////////set scheduleing type//////////////////////////////
int err;
int policy;
int i=0;
struct sched_param param;
policy=SchedGet(0, 0, ¶m);
err = SchedSet( 0, 0, SCHED_FIFO, ¶m );
if (err == -1){
printf("Set error");
return 0;
}
word1[13] = '\0';
printf("Send to address %s\n", word1);
if ((he=gethostbyname(word1)) == NULL) { // get the host info
perror("gethostbyname");
exit(1);
}
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket");
exit(1);
}
their_addr.sin_family = AF_INET; // host byte order
their_addr.sin_port = htons(MYSENPORT); // short, network byte order
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
#if settimeout_debug
tv.tv_sec = 2;
tv.tv_usec = 10000;
cmsg_len =sizeof(struct timeval);
setsockopt( sockfd, SOL_SOCKET, SO_SNDTIMEO , &tv, cmsg_len );
#endif
/*Read system time*/
ftime( &timebuf );
now = ctime( &timebuf.time );
printf( "\n1.The time is %.19s.%hu\n",now, timebuf.millitm );
while(1)
{
if ((numbytes=sendto(sockfd, word2, 78, 0,
(struct sockaddr *)&their_addr, sizeof(struct sockaddr))) == -1)
{
perror("\nsocket sendto");
fprintf( stderr,"sendto: %s\n", strerror( errno ) );
/*Read system time*/
ftime( &timebuf );
now = ctime( &timebuf.time );
printf( "2.The time is %.19s.%hu\n",now, timebuf.millitm );
i++;
if(i>5) goto end;
}
}
end:
close(sockfd);
return(0);
}
The program’s result following
Send to address 192.168.0.3
1.The time is Fri Jul 18 18:05:42.805
socket sendto: Host is down
sendto: Host is down
2.The time is Fri Jul 18 18:05:52.228
socket sendto: Host is down
sendto: Host is down
2.The time is Fri Jul 18 18:05:52.228
socket sendto: Host is down
sendto: Host is down
2.The time is Fri Jul 18 18:05:52.229
socket sendto: Host is down
sendto: Host is down
2.The time is Fri Jul 18 18:05:52.229
socket sendto: Host is down
sendto: Host is down
2.The time is Fri Jul 18 18:05:52.230
socket sendto: Host is down
sendto: Host is down
2.The time is Fri Jul 18 18:05:52.231