//Tcp_server.c
#include <stdio.h>
#include <sys/socket.h>
#include <unistd.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <pthread.h>
#define SERVER_PORT 8081
#define LENGTH_OF_LISTEN_QUEUE 10
#define MAX_THREAD 10
#define BUFFER_SIZE 255
#define SUCCESS_MASSAGE "login Success!"
#define FAIL_MESSAGE "login Failed!"
#define TOTAL_UAER_NUM 6
struct clientInformation
{
int flag;
char loginUsr[20];
int socket;
};
struct clientInformation clientInfo[TOTAL_UAER_NUM];
int servfd,server_socket;
int len = 0;
int numOfThread = 0;
char users[6][10] = {"Me","You","Him","Her","User","Tmd"};
char buf[BUFFER_SIZE];
pthread_t pthread[MAX_THREAD];
struct sockaddr_in servaddr,cliaddr;
void *docommand(int);
int main(int argc)
{
int num;
for(num = 0;num < TOTAL_UAER_NUM;num++)
{
clientInfo[num].flag = 0;
clientInfo[num].socket = 0;
}
if ((servfd = socket(AF_INET,SOCK_STREAM, 0 )) < 0 )
{
printf("create socket error!\n");
exit(1);
}
bzero( & servaddr,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(SERVER_PORT);
servaddr.sin_addr.s_addr = htons(INADDR_ANY);
if(bind(servfd,(struct sockaddr * ) & servaddr,sizeof(servaddr)) < 0 )
{
printf("bind to port %d failure!\n",SERVER_PORT);
exit(1);
}
if(listen(servfd,LENGTH_OF_LISTEN_QUEUE) < 0)
{
printf("call listen failure!\n");
exit(1);
}
while(numOfThread < MAX_THREAD)
{
socklen_t length = sizeof(cliaddr);
server_socket = accept(servfd,(struct sockaddr *)&cliaddr,&length);
if(server_socket < 0)
{
printf("error comes when call accept!\n");\
break;
}
else
{
//void *arg[] = {&server_socket};
if(pthread_create(&pthread[numOfThread++],NULL,(void *)docommand,(void *)server_socket) != 0)
printf("thread %d create failed!\n",numOfThread);
else
printf("thread %d create success!\n",numOfThread);
}
}
close(servfd);
return 0;
}
void *docommand(int server_socket)
{
while(1)
{
memset(buf,0,sizeof(buf));
len = recv(server_socket,buf,BUFFER_SIZE,0);
if(len < 0)
{
printf("error comes when receive data from client");
exit(1);
}
else
{
if(strcmp(buf,">>") == 0)
{
len = recv(server_socket,buf,BUFFER_SIZE,0);
if(len < 0)
{
printf("error comes when receive data from client!");
exit(1);
}
char temp[100],*talkto;
strcpy(temp,buf);
talkto = strtok(buf,":");
int j;
for(j = 0;j < TOTAL_UAER_NUM;j++)
{
if(clientInfo[j].flag == 1)
{
if(strcmp(clientInfo[j].loginUsr,talkto) == 0)
{
strcpy(buf,"!talk|");
strcat(buf,temp);
send(clientInfo[j].socket,buf,BUFFER_SIZE,0);
break;
}
}
}
if(j == TOTAL_UAER_NUM)
{
strcpy(buf,"!talk");
strcat(buf,"|0");
send(server_socket,buf,BUFFER_SIZE,0);
}
}
else if(strcmp(buf,"!file") == 0)
{
len = recv(server_socket,buf,BUFFER_SIZE,0);
if(len < 0)
{
printf("error comes when receive data from client!");
exit(1);
}
int i;
for(i = 0;i < TOTAL_UAER_NUM;i++)
{
if(clientInfo[i].flag == 1)
{
if(strcmp(clientInfo[i].loginUsr,buf) == 0)
{
strcpy(buf,"!file|");
strcat(buf,"1");
send(server_socket,buf,BUFFER_SIZE,0);
strcpy(buf,"!msg\nreceive the file!\n");
send(clientInfo[i].socket,buf,BUFFER_SIZE,0);
break;
}
}
}
if(i == TOTAL_UAER_NUM)
{
strcpy(buf,"!file");
strcat(buf,"|0");
send(server_socket,buf,BUFFER_SIZE,0);
}
}
else if(strcmp(buf,"!list") == 0)
{
strcat(buf,"|");
int k;
for(k = 0;k < TOTAL_UAER_NUM;k++)
{
if(clientInfo[k].flag == 1)
{
strcat(buf,clientInfo[k].loginUsr);
strcat(buf,"\n");
}
}
send(server_socket,buf,BUFFER_SIZE,0);
}
else if(strcmp(buf,"!logoff") == 0)
{
while(1)
{
strcat(buf,"!logoffsuccess!\n");
send(server_socket,buf,BUFFER_SIZE,0);
memset(buf,0,sizeof(buf));
len = recv(server_socket,buf,BUFFER_SIZE,0);
int k;
for(k = 0;k < TOTAL_UAER_NUM;k++)
{
if(strcmp(buf,clientInfo[k].loginUsr) == 0)
{
strcpy(clientInfo[k].loginUsr," ");
clientInfo[k].flag = 0;
clientInfo[k].socket = 0;
break;
}
}
close(server_socket);
numOfThread--;
pthread_exit(NULL);
break;
}
}
else if(strcmp(buf,"!login") == 0)
{
len = recv(server_socket,buf,BUFFER_SIZE,0);
if(len < 0)
{
printf("error comes when receive data from client!login");
exit(1);
}
printf("from client :%s\tIP:%d\tPort:%d\n",buf,inet_ntoa(cliaddr.sin_addr),ntohs(cliaddr.sin_port));
int i;
for(i = 0;i < TOTAL_UAER_NUM;i++)
{
if(strcmp(buf,users[i]) == 0)
{
if(clientInfo[i].flag == 0)
{
for(i = 0;i < TOTAL_UAER_NUM;i++)
{
strcpy(clientInfo[i].loginUsr,buf);
clientInfo[i].socket = server_socket;
clientInfo[i].flag = 1;
break;
}
}
strcpy(buf,SUCCESS_MASSAGE);
send(server_socket,buf,BUFFER_SIZE,0);
break;
}
}
if(i == TOTAL_UAER_NUM)
{
printf("No user called %s!Login failed!\n",buf);
memset(buf,0,sizeof(buf));
strcpy(buf,FAIL_MESSAGE);
send(server_socket,buf,BUFFER_SIZE,0);
}
}
}
}
}