日期:2014-05-16 浏览次数:20916 次
#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #define BUFFSIZE 150 int main(void){ struct sockaddr_in serv; char buff[BUFFSIZE]; int sockfd, n; if((sockfd = socket(PF_INET, SOCK_DGRAM, 0) < 0)) perror("socket error"); bzero((char*)&serv, sizeof(serv)); memset(buff, 0, BUFFSIZE); serv.sin_family = AF_INET; serv.sin_addr.s_addr = inet_addr("140.252.1.32"); serv.sin_port = htons(13); if(n = sendto(sockfd, buff, BUFFSIZE, 0, (struct sockaddr *)&serv, sizeof(serv)) != BUFFSIZE) perror("sendto error"); if((n = recvfrom(sockfd, buff, BUFFSIZE, 0, (struct sockaddr *)NULL, (int*)NULL)) != BUFFSIZE) perror("recvfrom error"); printf("%s\n", buff); exit(0); }