我不想设置什么超时,有什么办法吗????

我sendto发送一个消息时,如果指向的IP不存在,我怎么判断。能在程序中调用什么函数,象ping 那样ip不存在时会返回一个信息之类的。

智慧大石
浏览 133回答 1
1回答

人到中年有点甜

#include <os.h>&nbsp;#include <stdio.h>&nbsp;#include <stdlib.h>&nbsp;#include <string.h>&nbsp;#define ICMP_ECHO 8&nbsp;#define ICMP_ECHOREPLY 0&nbsp;#define ICMP_MIN 8 // Minimum 8 byte icmp packet (just header)&nbsp;//&nbsp;// ICMP header&nbsp;//struct iphdr&nbsp;{&nbsp;unsigned int h_len:4; // length of the header&nbsp;unsigned int version:4; // Version of IP&nbsp;unsigned char tos; // Type of service&nbsp;unsigned short total_len; // total length of the packet&nbsp;unsigned short ident; // unique identifier&nbsp;unsigned short frag_and_flags; // flags&nbsp;unsigned char ttl;&nbsp;&nbsp;unsigned char proto; // protocol (TCP, UDP etc)&nbsp;unsigned short checksum; // IP checksum&nbsp;unsigned int source_ip;&nbsp;unsigned int dest_ip;&nbsp;};//&nbsp;// ICMP header&nbsp;//struct icmphdr&nbsp;{&nbsp;unsigned char i_type;&nbsp;unsigned char i_code; // type sub codeunsigned short i_cksum;&nbsp;unsigned short i_id;&nbsp;unsigned short i_seq;&nbsp;// This is not the std header, but we reserve space for timeunsigned long timestamp;&nbsp;};&nbsp;struct pingstat{int tmin;int tmax;int tsum;int ntransmitted;int nreceived;};#define DEF_PACKET_SIZE 32&nbsp;#define MAX_PACKET 1024&nbsp;#define PKTSIZ (sizeof(struct icmphdr) + MAX_PACKET)void fill_icmp_data(char *icmp_data, int datasize);unsigned short checksum(unsigned short *buffer, int size);&nbsp;void decode_resp(char *buf, int bytes, struct sockaddr_in *from, struct pingstat *stat);int cmd_ping(int argc, char *argv[]){&nbsp;int sockraw;&nbsp;struct sockaddr_in dest, from;&nbsp;struct hostent *hp;&nbsp;int bread, datasize, rc;&nbsp;int fromlen = sizeof(from);&nbsp;int timeout = 1000;&nbsp;char *dest_ip;&nbsp;char icmp_data[PKTSIZ];&nbsp;char recvbuf[PKTSIZ];unsigned int addr = 0;&nbsp;unsigned short seq_no = 0;&nbsp;int numpackets;char *hostname;struct pingstat stat;if (argc < 2)&nbsp;{fprintf(stderr, "usage: %s <host> [data_size]\n", argv[0]);&nbsp;return 0;}hostname = argv[1];memset(&dest, 0, sizeof(dest));&nbsp;hp = gethostbyname(hostname);if (!hp) addr = inet_addr(hostname);&nbsp;if (!hp && addr == INADDR_NONE){fprintf(stderr, "%s: unknown host %s\n", argv[0], hostname);return 1;&nbsp;}&nbsp;if (hp != NULL)&nbsp;memcpy(&(dest.sin_addr), hp->h_addr, hp->h_length);&nbsp;else&nbsp;dest.sin_addr.s_addr = addr;&nbsp;if (hp)&nbsp;dest.sin_family = (unsigned char) hp->h_addrtype;&nbsp;else&nbsp;dest.sin_family = AF_INET;&nbsp;dest.sin_port = htons(53);dest_ip = inet_ntoa(dest.sin_addr);&nbsp;if (argc > 2){&nbsp;datasize = atoi(argv[2]);&nbsp;if (datasize == 0) datasize = DEF_PACKET_SIZE;&nbsp;}else&nbsp;datasize = DEF_PACKET_SIZE;&nbsp;if (datasize + sizeof(struct icmphdr) > PKTSIZ){&nbsp;fprintf(stderr, "%s: packet size too large\n", argv[0]);&nbsp;return 1;&nbsp;}&nbsp;sockraw = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);&nbsp;&nbsp;if (sockraw < 0)&nbsp;{perror("ping: socket");return 1;&nbsp;}&nbsp;rc = setsockopt(sockraw, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, sizeof(timeout));&nbsp;if (rc < 0)&nbsp;{&nbsp;perror("ping: recv timeout");return 1;&nbsp;}&nbsp;timeout = 1000;&nbsp;rc = setsockopt(sockraw, SOL_SOCKET, SO_SNDTIMEO, (char *) &timeout, sizeof(timeout));&nbsp;if(rc < 0)&nbsp;{&nbsp;perror("ping: send timeout");return 1;&nbsp;}stat.tmin = 999999999;stat.tmax = 0;stat.tsum = 0;stat.ntransmitted = 0;stat.nreceived = 0;memset(icmp_data, 0, MAX_PACKET);&nbsp;fill_icmp_data(icmp_data, datasize + sizeof(struct icmphdr));if (dest.sin_family == AF_INET)&nbsp;printf("PING %s (%s): %d data bytes\n", hostname, inet_ntoa(dest.sin_addr), datasize);elseprintf("PING %s: %d data bytes\n", hostname, datasize);printf("\n");for (numpackets = 0; numpackets < 5; numpackets++){&nbsp;int bwrote;struct icmphdr *icmphdr = (struct icmphdr *) &icmp_data;msleep(100);icmphdr->i_cksum = 0;&nbsp;icmphdr->timestamp = clock();&nbsp;icmphdr->i_seq = seq_no++;&nbsp;icmphdr->i_cksum = checksum((unsigned short *) icmp_data, datasize + sizeof(struct icmphdr));bwrote = sendto(sockraw, icmp_data, datasize + sizeof(struct icmphdr), 0, (struct sockaddr *) &dest, sizeof(dest));&nbsp;if (bwrote < 0){&nbsp;if (errno == ETIMEDOUT){printf("timed out\n");&nbsp;continue;&nbsp;}&nbsp;perror("ping: sendto");&nbsp;return 1;&nbsp;}if (bwrote < datasize){fprintf(stdout, "Wrote %d bytes\n", bwrote);&nbsp;}fflush(stdout);stat.ntransmitted++;bread = recvfrom(sockraw ,recvbuf, MAX_PACKET, 0, (struct sockaddr *) &from, &fromlen);&nbsp;if (bread < 0){&nbsp;if (errno == ETIMEDOUT){printf("timed out\n");&nbsp;continue;&nbsp;}&nbsp;perror("ping: recvfrom");&nbsp;return 1;&nbsp;}decode_resp(recvbuf, bread, &from, &stat);&nbsp;msleep(1000);&nbsp;}&nbsp;printf("----%s PING Statistics----\n", hostname);printf("%d packets transmitted, ", stat.ntransmitted);printf("%d packets received, ", stat.nreceived);if (stat.ntransmitted){if (stat.nreceived > stat.ntransmitted)printf("-- somebody's printing up packets!");elseprintf("%d%% packet loss", (int) (((stat.ntransmitted - stat.nreceived) * 100) / stat.ntransmitted));printf("\n");}if (stat.nreceived){printf("round-trip (ms) min/avg/max = %d/%d/%d\n", stat.tmin, stat.tsum / stat.nreceived, stat.tmax);}close(sockraw);return 0;&nbsp;}&nbsp;void decode_resp(char *buf, int bytes, struct sockaddr_in *from, struct pingstat *stat)&nbsp;{&nbsp;struct iphdr *iphdr;&nbsp;struct icmphdr *icmphdr;&nbsp;unsigned short iphdrlen;&nbsp;int triptime;iphdr = (struct iphdr *) buf;iphdrlen = iphdr->h_len * 4 ; // number of 32-bit words *4 = bytes&nbsp;if (bytes < iphdrlen + ICMP_MIN)&nbsp;{&nbsp;printf("Too few bytes from %s\n", inet_ntoa(from->sin_addr));&nbsp;return;}&nbsp;icmphdr = (struct icmphdr *) (buf + iphdrlen);&nbsp;if (icmphdr->i_type != ICMP_ECHOREPLY)&nbsp;{&nbsp;fprintf(stderr, "non-echo type %d recvd\n", icmphdr->i_type);&nbsp;return;&nbsp;}&nbsp;if (icmphdr->i_id != (unsigned short) gettid())&nbsp;{&nbsp;fprintf(stderr, "someone else's packet!\n");&nbsp;return;&nbsp;}&nbsp;triptime = clock() - icmphdr->timestamp;stat->tsum += triptime;if (triptime < stat->tmin) stat->tmin = triptime;if (triptime > stat->tmax) stat->tmax = triptime;stat->nreceived++;bytes -= iphdrlen + sizeof(struct icmphdr);printf("%d bytes from %s:", bytes, inet_ntoa(from->sin_addr));&nbsp;printf(" icmp_seq=%d", icmphdr->i_seq);&nbsp;printf(" time=%d ms", triptime);&nbsp;printf(" TTL=%d", iphdr->ttl);&nbsp;printf("\n");&nbsp;}&nbsp;unsigned short checksum(unsigned short *buffer, int size)&nbsp;{&nbsp;unsigned long cksum = 0;&nbsp;while (size > 1){&nbsp;cksum += *buffer++;&nbsp;size -= sizeof(unsigned short);&nbsp;}&nbsp;if (size) cksum += *(unsigned char *) buffer;&nbsp;cksum = (cksum >> 16) + (cksum & 0xffff);&nbsp;cksum += (cksum >> 16);&nbsp;return (unsigned short) (~cksum);&nbsp;}&nbsp;void fill_icmp_data(char *icmp_data, int datasize){&nbsp;struct icmphdr *icmp_hdr;&nbsp;char *datapart;&nbsp;icmp_hdr = (struct icmphdr *) icmp_data;&nbsp;icmp_hdr->i_type = ICMP_ECHO;&nbsp;icmp_hdr->i_code = 0;&nbsp;icmp_hdr->i_id = (unsigned short) gettid();&nbsp;icmp_hdr->i_cksum = 0;&nbsp;icmp_hdr->i_seq = 0;&nbsp;datapart = icmp_data + sizeof(struct icmphdr);&nbsp;memset(datapart, 'E', datasize - sizeof(struct icmphdr));
打开App,查看更多内容
随时随地看视频慕课网APP