Chinaunix首页 | 论坛 | 博客
  • 博客访问: 505546
  • 博文数量: 111
  • 博客积分: 3160
  • 博客等级: 中校
  • 技术积分: 1982
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-24 11:49
个人简介

低调、勤奋。

文章分类

全部博文(111)

文章存档

2014年(2)

2013年(26)

2012年(38)

2011年(18)

2010年(27)

分类: C/C++

2012-01-17 10:30:51

服务器程序:
#include
#include
#include
#include
#include
#include

#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
#include
#include
#include
#include
#include
#include
#include
#include
#include  
#include

#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;
        }   
    }
}



阅读(5120) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~