?? tcpserver.c
字號:
#include "tcpserver.h"#include "tcplink.h"#include <pthread.h>#ifdef HAVE_CONFIG_H#include <config.h>#endif//thread structtypedef struct threadargs{ int sock; LList *list;}threadargs;int getuser(char *buf,char *username,LList *userlist);char iscmd(const char * message);int useratlist(LList *userlist,char *username);int findclientsock(LList *userlist,char *username);char *getsecond(char *message);char *getthird(char *message);void domessage(char *dest,char * userfrom,char *message);void accept_cli(threadargs *newargs);int main(int argc,char **argv){ int sctcp; int z,len_inet; struct sockaddr_in adr_srvr,adr_clnt = {0}; int sockfd,connfd; char hostname[80] = ""; int server_port=8000; int childPid=0; static LList userlist; InitList(&userlist); pthread_t id; int ret; printf("Server is starting...\n"); sctcp=tcpSocket(); //set SO_REUSEADDR,SO_LINGER opt Setsockopt(sctcp); GetHostName(hostname, sizeof(hostname)); CreateSockAddr(hostname,&adr_srvr,server_port); Bind(sctcp, (struct sockaddr *) &adr_srvr,sizeof(adr_srvr)); Listen(sctcp); printf("Server started successfully and it is ready now\n"); printf("Now entered listening mode\n"); for (;;){ int cli_socket, cli_sock2,clientLength = sizeof(adr_clnt); (void) memset(&adr_clnt, 0, sizeof(adr_clnt)); //waiting for link cli_socket = Accept(sctcp,(struct sockaddr *) &adr_clnt, &clientLength); if (-1 == cli_socket){ perror("accept()"); } threadargs newargs; newargs.sock=cli_socket; newargs.list=&userlist; //creat pthread ret=pthread_create(&id,NULL,(void *)accept_cli,&newargs); if(ret!=0) perror("thread create error"); } return EXIT_SUCCESS; }void accept_cli(threadargs *newargs){ LList *userlist=newargs->list; int cli_socket=newargs->sock; int cli_sock2; //num of recv bytes int recvn; char buf[bufsize+1]=""; char buf2[bufsize+1]=""; char cmd; client newcli; //the user which client talk to last time client lastuser; bzero(&newcli,sizeof(client)); bzero(&lastuser,sizeof(client)); if(-1==Recv(cli_socket,buf)) pthread_exit(NULL); printf("%s",buf); if(-1==Send(cli_socket,"Wercome you\n")) pthread_exit(NULL); bzero(buf,bufsize); if(-1==Recv(cli_socket,buf)) pthread_exit(NULL); Rtrim(buf); //username has been used while(useratlist(userlist,buf)==0) { if(-1==Send(cli_socket,":x")) pthread_exit(NULL); bzero(buf,bufsize); if(-1==Recv(cli_socket,buf)) pthread_exit(NULL); Rtrim(buf); } Send (cli_socket,"Longin Successfully\n"); strncpy(newcli.nick,buf,strlen(buf)); newcli.sock=cli_socket; ListInsert(userlist,newcli); while(1){ //use in :a LNode *node=userlist->head->next; bzero(buf,bufsize); //client offline if(Recv(cli_socket,buf)==-1){ ListDelete(userlist,cli_socket); pthread_exit(NULL); } //if message body contains only message(not have a command) if((cmd=iscmd(buf))==0){ if(useratlist(userlist,lastuser.nick)==0){ cli_sock2=lastuser.sock; domessage(buf2,newcli.nick,buf); if(-1==Send(cli_sock2,buf2)) pthread_exit(NULL); } else{ if(-1==Send(cli_socket,"The user you want to talk isn't online\n")) pthread_exit(NULL); } continue; } switch(cmd){ case 'l': bzero(buf,bufsize); LNode *user=userlist->head->next; while(user!=NULL){ strcat(buf,user->e.nick); strcat(buf,"\n"); user=user->next; } if(-1==Send(cli_socket,buf)) pthread_exit(NULL); break; //client change user which will talk to case 'u': //buf client's message if(getuser(buf,lastuser.nick,userlist)!=-1){ cli_sock2=findclientsock(userlist,lastuser.nick); lastuser.sock=cli_sock2; if(getthird(buf)!=NULL){ //buf2 username domessage(buf2,newcli.nick,getthird(buf)); if(-1==Send(cli_sock2,buf2)) pthread_exit(NULL); } } else{ if(-1==Send(cli_socket,"You doesn't specify a user,or the user you want to talk to isn't online\n")) pthread_exit(NULL); } break; //client quit case 'q': if(-1==Send(cli_socket,buf)) pthread_exit(NULL); ListDelete(userlist,cli_socket); close(cli_socket); pthread_exit(NULL); break; //client talk to all user case 'a': while(node!=NULL){ client user=node->e; cli_sock2=user.sock; //don't send the message to your if (cli_sock2!=cli_socket){ //if the message body only contains the :a string if(getsecond(buf)!=NULL) domessage(buf2,newcli.nick,getsecond(buf)); if(-1==Send(cli_sock2,buf2)) pthread_exit(NULL); } node=node->next; } break; default : if(-1==Send(cli_socket,"Sever can't recognize your command\n")) pthread_exit(NULL); } }}//if the user is online(int the userlist),set the username string,return 0,else return -1int getuser(char *buf,char *username,LList *userlist) { const char delimiters[] = " "; char *token, *cp; cp = strdup(buf); token = strtok (cp, delimiters); //token=username token = strtok (NULL, delimiters); if(token==NULL) return -1; strncpy(username,token,namesize); if(username[strlen(username)-1]='\n') username[strlen(username)-1]=0; return useratlist(userlist,username);}//get commandchar iscmd(const char * message){ char cmd; if((cmd=message[0])!=':') return 0; return message[1];}int useratlist(LList *userlist,char *username){ LNode *node=userlist->head->next; client user; while(node!=NULL){ user=node->e; if(strncmp(user.nick,username,strlen(username))==0) return 0; else node=node->next; } return -1;}int findclientsock(LList *userlist,char *username){ LNode *node=userlist->head->next; client user; while(node!=NULL){ user=node->e; if(strncmp(user.nick,username,strlen(username))==0) return user.sock; else node=node->next; } return -1;}char *getsecond(char *message){ const char delimiters[] = " "; char *token, *cp; cp = strdup(message); token = strtok (cp, delimiters); token = strtok (NULL, delimiters); return token;}char *getthird(char *message){ const char delimiters[] = " "; char *token, *cp; cp = strdup(message); token = strtok (cp, delimiters); token = strtok (NULL, delimiters); token = strtok (NULL, delimiters); return token;}void domessage(char *dest,char * userfrom,char *message){ strcpy(dest,"From "); strcat(dest,userfrom); strcat(dest,": "); strcat(dest,message);}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -