채팅방 서버 기본 코드

4497 단어
#include 
#include 

#include 
#include 
#include 
#include 
#include 

#define SER_PORT 3333

struct 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!
"); break; } printf("msg->action = %d
",msg->action); switch(msg->action) { case 1: { msg->action = 1; write(cfd,msg,sizeof(struct message)); printf("write success!
"); 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!
"); 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!
"); 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!
"); if(listen(sockfd,5) == -1) { perror("listen error!"); exit(1); } printf("listen success!
"); 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
",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!
"); break; } printf("recv client send msg:%d %s %s
",msg->action,msg->name,msg->passwd); msg->action = -1; write(cfd,msg,sizeof(struct message)); } } else if(pid > 0) { ; } #endif } return 0; }

좋은 웹페이지 즐겨찾기