服务器程序: #include <sys/types.h> #include <sys/socket.h> #include <string.h> #include <netinet/in.h> #include <stdio.h> #include <stdlib.h> #define MAXLINE 1024 #define SERV_PORT 8000 void do_echo(int sockfd, struct sockaddr *pcliaddr, socklen_t clilen); int main(void) { int sockfd; struct sockaddr_in servaddr, cliaddr; sockfd = socket(AF_INET, SOCK_DGRAM, 0); /* create a socket */ /* init servaddr */ bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(SERV_PORT); /* bind address and port to socket */ if(bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1) { perror("bind error"); exit(1); } do_echo(sockfd, (struct sockaddr *)&cliaddr, sizeof(cliaddr)); close(sockfd); return 0; } void do_echo(int sockfd, struct sockaddr *pcliaddr, socklen_t clilen) { int n; socklen_t len; char mesg[MAXLINE]; char *hostname = "app-server"; for(;;) { len = clilen; /* waiting for receive data */ n = recvfrom(sockfd, mesg, MAXLINE, 0, pcliaddr, &len); //mesg[n] ='/0'; printf("OK,i receave a data %s \n",mesg); snprintf(mesg,sizeof(mesg)," Authentication Required: %s\r\n" "Result: 1\r\n" "Call-ID: %s\r\n\r\n",hostname,hostname,hostname); /* sent data back to client */ sleep(2); n = sendto(sockfd, mesg, n, 0, pcliaddr, len); } } 客户端程序: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <sys/types.h> #include <netinet/in.h> #include <sys/socket.h> #include <arpa/inet.h> #include <unistd.h> #include <netdb.h> #include <time.h> #define SERVPORT 8000 #define MAXDATASIZE 1024 #define AST_MAX_MANHEADERS 10 typedef struct _input_data { /*! Buffer */ char inbuf[1024]; int inlen; const char *headers[AST_MAX_MANHEADERS]; }input_data; #define input_data_len sizeof(input_data) static char *parse_result(input_data *s,char *var); int main() { char sentbuf[1024]; char recvBuffer[1024]; char outbuffer[1024]; int serlen; struct hostent *host; char hostname[30] = {0}; char tmp[55]; time_t start; memcpy(hostname,"app-server",sizeof(hostname)); snprintf(sentbuf,sizeof(sentbuf)," Authentication Required: %s\r\n" "Agent-Number:%s\r\n" "Result: 2\r\n" "Call-ID: %s\r\n\r\n",hostname,hostname,hostname); //snprintf(sentbuf,sizeof(sentbuf),"I alaway look for you ld%",time(&start)); printf("sentbuf is %s %s time %ld\n",sentbuf,hostname,time(&start)); int sock; int on; struct sockaddr_in servaddr; //sendto中使用的对方地址 struct sockaddr_in fromAddr; //在recvfrom中使用的对方主机地址 unsigned int fromLen; host = gethostbyname(hostname); if (!host) { snprintf(tmp, sizeof(tmp), "No such address: %s\n", hostname); printf("Host lookup failed", tmp); return -1; } sock = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP); memset(&servaddr,0,sizeof(servaddr)); servaddr.sin_family=AF_INET; servaddr.sin_addr = *((struct in_addr *)host->h_addr); servaddr.sin_port = htons(SERVPORT); struct timeval tv; tv.tv_sec = 5; tv.tv_usec = 0; setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)); //发送时限 setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); //接收时限 if (setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, (const char* ) &on, sizeof (on)) == -1) { printf("Error:Failed to set socket option SO_REUSEADDR\n"); close(sock); exit(1); } serlen = sizeof(servaddr); sendto(sock,sentbuf,MAXDATASIZE,0,(struct sockaddr*)&servaddr,serlen); fromLen = sizeof(fromAddr); int n; n = recvfrom(sock,recvBuffer,MAXDATASIZE,0,(struct sockaddr*)&fromAddr,&fromLen); if(n<0) { if (errno == EWOULDBLOCK) { fprintf(stderr,"socket timeout \n"); } else { printf("recefrom error \n"); } } printf("I receave a data %s \n",recvBuffer); input_data *s; s = (input_data *)malloc(input_data_len); if (NULL == s) { exit; } memset(s, 0, input_data_len); memcpy(s->inbuf,recvBuffer,sizeof(s->inbuf)); s->inlen = strlen(recvBuffer); char *res; res = parse_result(s,"Call-ID"); if(res) printf("the result is %s \n",res); else printf("not found result ----- \n"); close(sock); return 0; } static char *parse_result(input_data *s,char *var) { char *tmp; char *result; char *res; char cmp[80]; snprintf(cmp, sizeof(cmp), "%s: ", var); tmp = s->inbuf; if(!tmp || *tmp == '\0') { printf("no data return \n"); return 0; } while(1){ res = strstr(tmp,"\r\n"); if(res) { *res = '\0'; res += 2; if (strstr(tmp,var)) { result = tmp + strlen(cmp); return result; } tmp = res; } else { return NULL; } } }