⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 thread.c

📁 一个C语言的聊天室源代码
💻 C
字号:
/*
	Version: 0.2.0(alpha)
	Author: Computer_xu
	Email: Computer_xu@sina.com
	HomePage: http://www.socketchat.com
	LastModify: 2001-07-04 (yyyy-mm-dd)
*/

#include "thread.h"

#define DEBUGMODE

pthread_mutex_t acclock=PTHREAD_MUTEX_INITIALIZER;

char *r200 = "HTTP/1.0 200\n";
char *r302 = "HTTP/1.0 302 Found\n";
char *notfound ="HTTP/1.0 404 Not Found\n";
char *header = "Content-type: text/html\n";
char *end = "\n";
char *version="0.2.0";

/* 线程导引 */
void *thread_main(void *arg)
{

        int connfd;
        int clilen=sizeof(struct sockaddr_in);
        struct sockaddr_in cliaddr;
        PTHREAD_INFO *p;
        int off;

        pthread_detach(pthread_self());
	p=arg;
        for(;;)
        {
                pthread_mutex_lock(&acclock);
                connfd=accept(sockfd,(struct sockaddr *)&cliaddr,&clilen);
		sysinfo.linkcount++;
                pthread_mutex_unlock(&acclock);
                p->pid=getpid();
                p->status=1;
                off=WebChild(connfd);
                p->status=0;
                if( off ) close(connfd);
        }
}
/* 线程创建 */
void thread_make(PTHREAD_INFO *p)
{
        int status;
 
	do{
        	status=pthread_create(&(p->pid),NULL,&thread_main,(void *) p);
	}while( status && errno==EINTR && status!=EAGAIN);

        if( status )
        {
                perror("pthread_create");
		printf("Errno: %d/%d, Threads Max: %d\n",status,EAGAIN,PTHREAD_THREADS_MAX);
                exit(1);
        }
        return;
}

void WriteHeader(int connfd)
{
	time_t now;
	char Buffer[1025];

	now=time(NULL);
	write(connfd,r200,Strlen(r200));
	sprintf(Buffer,"Server: Socket Chat Server %s\n",version);
	write(connfd,Buffer,strlen(Buffer));
	sprintf(Buffer,"Date: %s",ctime(&now)); /* ctime 输出的时候自动在行尾输出\n */
	sprintf(Buffer,"Expires: %s",ctime(&now)); /* ctime 输出的时候自动在行尾输出\n */
	sprintf(Buffer,"Last-Modified: %s",ctime(&now)); /* ctime 输出的时候自动在行尾输出\n */
	write(connfd,Buffer,strlen(Buffer));

	sprintf(Buffer,"Cache-Control: no-cache\nPragma: no-cache\n");
	write(connfd,Buffer,strlen(Buffer));

	write(connfd,header,Strlen(header));
	write(connfd,end,Strlen(end));
}
void WriteRedirect(int connfd,char *loc)
{
	time_t now;
	char Buffer[1025];

	now=time(NULL);
	write(connfd,r302,Strlen(r302));
	sprintf(Buffer,"Location: %s\n",loc);
	send(connfd,Buffer,strlen(Buffer),0);
	sprintf(Buffer,"Server: Socket Chat Server %s\n",version);
	write(connfd,Buffer,strlen(Buffer));
	sprintf(Buffer,"Date: %s",ctime(&now)); /* ctime 输出的时候自动在行尾输出\n */
	sprintf(Buffer,"Expires: %s",ctime(&now)); /* ctime 输出的时候自动在行尾输出\n */
	sprintf(Buffer,"Last-Modified: %s",ctime(&now)); /* ctime 输出的时候自动在行尾输出\n */
	write(connfd,Buffer,strlen(Buffer));

	sprintf(Buffer,"Cache-Control: no-cache\nPragma: no-cache\n");
	write(connfd,Buffer,strlen(Buffer));

	write(connfd,header,Strlen(header));
	write(connfd,end,Strlen(end));
}

int WebChild(int connfd)
{
	char *methods,*action;
	RequestData *RDp;
        char Buffer[4096];

        int n,i;
        char *p1,*p2;
        SPLIT *sp1, *sp2;
        char *p3;
        char *p4;
        ROOMINFO *rp;
        int off=1;
	unsigned long filesize;

	time_t now;

#ifdef DEBUGMODE
        printf("Enter WebChild(%d)...\n",Mcount);

	p3=GetSocketIP(connfd);
        printf("Connect From %s:%d\n",p3,GetSocketPort(connfd));
#endif

	if( (RDp=GetRequestData(connfd))==NULL )
	{
		printf("Nothing have received!\n");
		printf("Return from WebChild\n");
        	return(off);
	}
	methods=GetRIVarValue(RDp->Request,"Methods");
	action=GetRIVarValue(RDp->Request,"Action");
	if( methods==NULL || action==NULL )
	{
		FreeRD(RDp);
		printf("Only Support GET and POST Methods!\n");
        	printf("Return from WebChild(1).\n");
	        return(off);
	}
#ifdef DEBUGMODE
        printf("%s %s\n",methods,action);
#endif
        /* "GET /" to "GET /login.htm" */
        if( StrCmp(action,"/")==0 )
	{
		action=Realloc(action,Strlen(DefaultHTML)+2);
	        sprintf(action,"/%s",DefaultHTML);
	}
#ifdef DEBUGMODE
	printf("RealAction: %s\n",action);
#endif
        if( StrCmp(methods,"GET")==0 )
        {
                /* login: ?Login&user=[user]&pass=[pass]&roomid=[roomid]&sex=[sex]&box=[box] */
                if( strstr(action,"/cmd?Login")!=NULL )
                {
			Func_Login(connfd, RDp);
                }else
		/* enter: ?Enter&user=[user]&sid=[sid] */
                if( strstr(action,"/cmd?Enter")!=NULL )
                {
			Func_Enter(connfd, RDp);
                }else
                /* logout: ?Logout&user=[user]&uid=[sid] */
                if( strstr(action,"/cmd?Logout")!=NULL )
                {
			Func_Logout(connfd, RDp);
                }else
                /* OnlineUserList */
                if( strstr(action,"/cmd?OnlineUserList")!=NULL )
                {
			Func_OnlineUserList(connfd, RDp);
                }else
                /* FindUser */
                if( strstr(action,"/cmd?FindUser")!=NULL )
                {
			Func_FindUser(connfd, RDp);
                }else
                /* chatmessage */
                if( strstr(action,"/cmd?chatmessage")!=NULL )
                {
			off = Func_ChatMessage(connfd, RDp);
                }else
                /* get login.htm */
                if( StrCmp(action,"/login.htm")==0 )
                {
                        p1=GetFileData(action,&filesize);
                        if(p1!=NULL)
                        {
				WriteHeader(connfd);
                                p2=strstr(p1,"<!--System var-->");
                                if(p2!=NULL)
                                {
                                        *p2='\0';
                                        send(connfd,p1,Strlen(p1),0);
                                        *p2='<';
                                        sprintf(Buffer,"var TotalUsers = %d;\r\n",sysinfo.onlineuser);
                                        send(connfd,Buffer,strlen(Buffer),0);
                                        sprintf(Buffer,"var MaxOnline = %d;\r\n",sysinfo.maxuser);
                                        send(connfd,Buffer,strlen(Buffer),0);
                                        sprintf(Buffer,"var RegUsers = %d;\r\n",sysinfo.usernum);
                                        send(connfd,Buffer,strlen(Buffer),0);
                                        sprintf(Buffer,"var room=new Array(");
                                        send(connfd,Buffer,strlen(Buffer),0);
                                        rp=sysinfo.ROOM_HEAD;
                                        while(rp!=NULL)
                                        {
                                                sprintf(Buffer,"\"%d\",\"%s\",\"%s\",\"%d\",",rp->roomid,rp->roomname,rp->topic,rp->usernum);
                                                send(connfd,Buffer,strlen(Buffer),0);
                                                Buffer[0]='\0';
                                                sprintf(&Buffer[strlen(Buffer)],"\"");
                                                for(i=0;i<10 && Strlen(rp->managelist[i]);i++)
                                                {
                                                        sprintf(&Buffer[strlen(Buffer)],"%s,",rp->managelist[i]);
                                                }
                                                if( Buffer[strlen(Buffer)-1]==',' ) Buffer[strlen(Buffer)-1]='\0';
                                                sprintf(&Buffer[strlen(Buffer)],"\"");
                                                if( rp->next!=NULL )   sprintf(&Buffer[strlen(Buffer)],",");
                                                send(connfd,Buffer,strlen(Buffer),0);
                                                rp=rp->next;
                                        }
                                        sprintf(Buffer,");\r\n");
                                        send(connfd,Buffer,strlen(Buffer),0);
                                        p2+=strlen("<!--System var-->");
                                        send(connfd,p2,Strlen(p2),0);
                                }
                                else    send(connfd,p1,Strlen(p1),0);
                        }
			else
			{
				printf("Could not Load %s\n",action);
			}
                }else
                /* Normal Http GET request */
                {
#ifdef DEBUGMODE
                        printf("Normal Http Get request\n");
#endif
			sp1=split(action,"?");
                        p1=GetFileData(sp1->msg,&filesize);
			free_split(sp1);
                        if(p1!=NULL)
                        {
				WriteHeader(connfd);
                                send(connfd,p1,filesize,0);
				/* printf("Size: %d\n",filesize); */
                        }
                        else
                        {
                                send(connfd,notfound,Strlen(notfound),0);
                                send(connfd,end,Strlen(end),0);
                        }
                }
        }
        else /* POST */
        {
                /* register */
                if( strstr(action,"/cmd?Register")!=NULL )
                {
			Func_Register(connfd, RDp);
                }else
                /* say */
                if( strstr(action,"/cmd?say")!=NULL )
                {
			Func_Say(connfd, RDp);
                }else
                /* function */
                if( strstr(action,"/cmd?func")!=NULL )
                {

                }else
                /* Normal Http POST request */
                {
#ifdef DEBUGMODE
                        printf("Normal Http POST request\n");
#endif
			sp1=split(action,"?");
                        p1=GetFileData(sp1->msg,&filesize);
			free_split(sp1);

                        if(p1!=NULL)
                        {
				WriteHeader(connfd);
                                send(connfd,p1,filesize,0);
				/* printf("Size: %d\n",filesize); */
                        }
                        else
                        {
                                send(connfd,notfound,Strlen(notfound),0);
                                send(connfd,end,Strlen(end),0);
                        }
                }
        }
#ifdef DEBUGMODE
        p3=GetSocketIP(connfd);
	if( filesize==0 )
		filesize=(p1==NULL)?0:Strlen(p1);
        printf("Send %d bytes to %s:%d\n",filesize,p3,GetSocketPort(connfd));
#endif
	FreeRD(RDp);
#ifdef DEBUGMODE
        printf("Return from WebChild(%d)\n",Mcount);
#endif
        return(off);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -