导航

servicesdszfz.c

Posted on 2015-01-21 13:25  Super_pan  阅读(112)  评论(0编辑  收藏  举报
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>


//int user_num=0;    //普通用户数量
//int vip_num=0;     //VIP用户数量

char* buf1;     //哪种请求
char* buf2;     //字段1,如用户名
char* buf3;     //字段2,如密码
int len;     
char buf[BUFSIZ];  //数据传送的缓冲区
int client_sockfd; //客户端套接字
    
void parseBuf(char buf[]);
void doService();
                                   
int main(int argc, char *argv[])
{               
    int server_sockfd; //服务器端套接字
    struct sockaddr_in my_addr;   //服务器网络地址结构体
    struct sockaddr_in remote_addr; //客户端网络地址结构体
    int sin_size;
    memset(&my_addr,0,sizeof(my_addr)); //数据初始化--清零
    my_addr.sin_family=AF_INET; //设置为IP通信
    my_addr.sin_addr.s_addr=INADDR_ANY;//服务器IP地址--允许连接到所有本地地址上
    my_addr.sin_port=htons(8000); //服务器端口号
                       
    /*创建服务器端套接字--IPv4协议,面向连接通信,TCP协议*/
    if((server_sockfd=socket(PF_INET,SOCK_STREAM,0))<0)
    { 
          perror("socket");
          return 1;
    }
     
        /*将套接字绑定到服务器的网络地址上*/
    if (bind(server_sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr))<0)
    {
          perror("bind");
          return 1;
    }
        
    /*监听连接请求--监听队列长度为5*/
    listen(server_sockfd,5);
        
    sin_size=sizeof(struct sockaddr_in);
    while(1)
    {
        /*等待客户端连接请求到达*/
        if((client_sockfd=accept(server_sockfd,(struct sockaddr *)&remote_addr,&sin_size))<0)
        {
             perror("accept");
             return 1;
        }
        //printf("\n\n\naccept client %s\n",inet_ntoa(remote_addr.sin_addr));
        //len=send(client_sockfd,"Welcome to my server",21,0);//发送欢迎信息
        
        /*接收客户端的数据并将其发送给客户端--recv返回接收到的字节数,send返回发送的字节数*/
        while((len=recv(client_sockfd,buf,BUFSIZ,0))>0)
        { 
            buf1=buf2=buf3=NULL;
            parseBuf(buf);
            doService();  
        } 
            
        close(client_sockfd);
    }
    close(server_sockfd);
    return 0;
}
 
 
void parseBuf(char buf[])
{ 
    char delims[] = "#";  
    char *result = NULL;  
    result = strtok(buf,delims);
    buf1=result; 
    result = strtok( NULL, delims );
    buf2=result; 
    result = strtok( NULL, delims );
    buf3=result;
}
    
void doService()
{
    if(strstr(buf1,"vipLogin")!=NULL)
    {
        char res[BUFSIZ];
        if(strcmp(buf2,"aaa")&&strcmp(buf2,"bbb"))
             strcpy(res,"2");
        else if(strcmp(buf3,"123"))
             strcpy(res,"3");
        else
             strcpy(res,"1");
        len=send(client_sockfd,res,BUFSIZ,0);
    }
    else if(strstr(buf1,"staffLogin")!=NULL)
    {
        char res[BUFSIZ];
        if(strcmp(buf2,"lmx")&&strcmp(buf2,"lwc"))
             strcpy(res,"2");
        else if(strcmp(buf3,"123"))
             strcpy(res,"3");
        else
             strcpy(res,"1");
        len=send(client_sockfd,res,BUFSIZ,0);
    }
   
    else
    {
        printf("\n%s\n",buf1);
        printf("\n%s\n",buf2);
        printf("\n%s\n",buf3);
    }

}