聊天室服务器基本代码分享

来源:互联网 发布:类似沙盘的软件 编辑:程序博客网 时间:2024/06/01 23:53
#include <stdio.h>#include <stdlib.h>#include <sys/types.h>#include <sys/socket.h>#include <unistd.h>#include <sys/un.h>#include <netinet/in.h>#define SER_PORT 3333struct message{    int action;    char name[20];    char passwd[10];    char toname[20];    char msg[100];};struct online{    int cfd;    char name[20];    struct online *next;};struct online *head = NULL;void insert_user(struct online *new_user){     new_user->next = head;     head = new_user;}int find_fd(char *toname){    if(head == NULL)    {        return -1;    }    struct online *temp = head;    while(temp != NULL)    {        if(strcmp(temp->name,toname) == 0)    {        return temp->cfd;    }    temp = temp->next;    }    return -1;}void * read_msg(void *arg){    int cfd = *((int *)arg);    struct online *new_user;    struct message *msg = (struct message *)malloc(sizeof(struct message));    int n_read;    while(1)    {            n_read = read(cfd,msg,sizeof(struct message));            if(n_read == 0)            {                printf("client is close!\n");            break;            }        printf("msg->action = %d\n",msg->action);        switch(msg->action)        {            case 1:            {                msg->action = 1;            write(cfd,msg,sizeof(struct message));            printf("write success!\n");            break;            }            case 2:            {                         new_user = (struct online *)malloc(sizeof(struct online));             new_user->cfd = cfd;             strcpy(new_user->name,msg->name);             msg->action = 2;             insert_user(new_user);             write(cfd,msg,sizeof(struct message));             break;            }            case 3:            {                  int to_fd = find_fd(msg->toname);             if(to_fd == -1)             {                 msg->action = -3;                 write(cfd,msg,sizeof(struct message));             }             else             {                 msg->action = 3;                 write(to_fd,msg,sizeof(struct message));             }             break;            }        }    }}int main(int argc, char *argv[]){    if(argc != 2)    {        printf("Please input server bind ip!\n");    exit(1);    }    int s_len;    int sockfd;    int cfd;    int pid;    int n_read;    int opt = 1;    pthread_t id;    char buffer[1024];    struct message *msg = (struct message *)malloc(sizeof(struct message));    struct sockaddr_in ser_addr;    struct sockaddr_in client_addr;    if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == - 1)    {        perror("socket creat error!");    exit(1);    }    printf("sockfd success!\n");    bzero(&ser_addr,sizeof(ser_addr));    ser_addr.sin_family = AF_INET;    //ser_addr.sin_addr.s_addr = htonl(INADDR_ANY);    ser_addr.sin_addr.s_addr = inet_addr(argv[1]);    ser_addr.sin_port = htons(SER_PORT);    setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));    if(bind(sockfd,(struct sockaddr *)&ser_addr,sizeof(ser_addr)) == -1)    {        perror("bind error!");    exit(1);    }    printf("bind success!\n");    if(listen(sockfd,5) == -1)    {        perror("listen error!");    exit(1);    }    printf("listen success!\n");    while(1)    {        s_len = sizeof(client_addr);        if((cfd = accept(sockfd,&client_addr,&s_len)) < 0)        {            perror("accept error!");        exit(1);        }        printf("accept success = %s\n",inet_ntoa(client_addr.sin_addr));    pthread_create(&id,NULL,read_msg,(void *)&cfd);  #if 0    pid = fork();    if(pid == 0)    {        while(1)        {            n_read = read(cfd,msg,sizeof(struct message));            if(n_read == 0)            {                printf("client is close!\n");            break;            }                printf("recv client send msg:%d %s %s\n",msg->action,msg->name,msg->passwd);            msg->action = -1;            write(cfd,msg,sizeof(struct message));             }    }    else if(pid > 0)    {        ;        } #endif    }    return 0;}
0 0
原创粉丝点击