tcp/ip 程序改进版 server.h

来源:互联网 发布:mysql select 多张表 编辑:程序博客网 时间:2024/06/01 23:15
/***************************************************版权说明:该程序为Shaw Song所有版本号:  2.0生成日期:2016.8.2作者:    Shaw Song内容:    服务端头文件(.h)功能:    完成以下三种服务的具体代码          1.客户端登录服务  2.客户端注册服务  3.客户端交流服务函数列表:1.Server_Login            完成用户端的登录服务  2.Server_Sign    完成用户端的注册服务  3.Server_Talk    完成客户端与服务器交流的服务修改日志:2016.08.02  Shaw Song  修改程序使之符合C语言编码规范 ****************************************************/#include <stdio.h>#include <stdlib.h>#include <sys/types.h>#include <sys/socket.h>#include <arpa/inet.h>#include <unistd.h>#include <string.h>#include <malloc.h>#define Port 8888#define BUFFERSIZE 104#define BUFSIZE 100#define MAX_DATA_SIZE 200/*********************************************    Message是服务器与客户端之间的服务数据结构    其中:head为服务的类型,为4个字节          content为服务类型信息,为100字节**********************************************/struct Message{char head[4];char content[100];};/************************************************函数名:  Server_Login目的:    完成用户端的登录服务,从userlist中检验          客户端输入的用户名以及密码,决定是否允  许客户端登录输入参数:int ClientFd:客户端Socket描述字          char* buffer:指向服务端recv到的数据的指针输出参数:void调用关系:上级函数:server.c          下级函数:无    ************************************************/void Server_Login(int ClientFd, char* buffer){/* 初始化变量 */char username[30];char password[30];char buf[100];char temp[100];struct Message msg;memset(username,'\0',30);memset(password,'\0',30);memset(temp,'\0',100);msg=*(struct Message*)buffer;strcpy(buf,msg.content);/* 打开文件 */FILE *file;//文件句柄if ((file = fopen("userlist", "r+")) == NULL){printf ("ERROR! Can not open file!\n");exit(1);}/* 扫描文件,验证用户名以及密码 */while (fscanf(file, "%s\n", temp) != EOF){if (strcmp (temp, buf) == 0){printf("Log in success!\n");strcpy(msg.head, "LISC");strcpy(msg.content, "server--->Login in success!\n");send(ClientFd, (char*)&msg, BUFFERSIZE, 0);            return ;}              }//end of while (fscanf(file, "%s\n", temp) != EOF)        /* 文件中没有匹配的用户名以及密码 */printf("login fail!!!!!\n");strcpy(msg.head, "LIFL");    strcpy(msg.content, "server:Wrong username or Password!");    send(ClientFd, (char*)&msg, BUFFERSIZE,0);}/*********************************************函数名:  Server_Talk目的:    完成客户端与服务器交流的服务输入参数: int ClientFd 客户端Socket描述字输出参数:void调用关系:上级函数:server *********************************************/void Server_Talk(int ClientFd){/* 变量初始化 */pid_t pid;char SendBuf[MAX_DATA_SIZE];char RecvBuf[MAX_DATA_SIZE];int SendSize, RecvSize;if ((pid = fork()) < 0)    {        perror("fork error\n");    }    else if (pid == 0)/*child progress*/    {        /*send datas to client*/        while (1)        {            fgets(SendBuf, MAX_DATA_SIZE, stdin);            *strchr(SendBuf, '\n') = '\0';            if ((SendSize = send(ClientFd, SendBuf, strlen(SendBuf), 0)) != strlen(SendBuf))            {                perror("fail to send datas");                exit(1);            }    if (strcmp(SendBuf, "exit") == 0)    {exit(0);}            memset(SendBuf,0,MAX_DATA_SIZE);        }//end of while(1)    }    else    {        while(1)        {            memset(RecvBuf, '\0', MAX_DATA_SIZE);            if ((RecvSize = recv(ClientFd, RecvBuf, MAX_DATA_SIZE, 0)) <= 0)            {                perror("fail to receive datas");                exit(1);            }RecvBuf[RecvSize]='\0';if (strcmp(RecvBuf,"exit")==0){break;}            printf("Client:%s\n",RecvBuf);            }//end of while(1)    }//end of if ((pid = fork()) < 0)}/*****************************************************函数名:  Server_Sign目的:    完成用户端的注册服务输入参数:int ClientFd 客户端Socket描述字输出参数:void调用关系:上级函数:server          下级函数:无 ****************************************************/void Server_Sign(int ClientFd){/* 变量初始化 */char RecvBuf[BUFSIZE];char SendBuf[BUFSIZE];    char buf[BUFSIZE];memset(SendBuf, '\0', BUFSIZE);memset(buf, '\0', BUFSIZE);/* recv sign message from client */int RecvSize = recv(ClientFd, RecvBuf, BUFSIZE, 0);if (RecvSize <= 0){perror("server recv error");exit(1);}RecvBuf[RecvSize] = '\0';printf("RecvBuf:%s\n", RecvBuf);    /* write into userlist */FILE *fp;    strcpy(buf, RecvBuf); if((fp = fopen("userlist","a")) == NULL){printf("Can't open userlist\n");exit(1);}        int fpSize = fputs(buf, fp);        fputs("\n",fp);//先换行if (fpSize < 0){strcpy("file write fail!", SendBuf);send (ClientFd,SendBuf, BUFSIZE, 0);}strcpy(SendBuf, "sign success!");send(ClientFd, SendBuf, BUFSIZE, 0);printf("%s\n", buf);}

0 0
原创粉丝点击