上班咯..........
2009-04-29 01:36
127 查看
#include<pthread.h>
#include<time.h>
#include<netinet/in.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<stdio.h>
#include<stdlib.h>
#include<sched.h>
#include<signal.h>
#include<string.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<semaphore.h>
#define SERVERPORT 3333 //the port defined here for server
#define LENGTH_OF_LISTEN_QUEUE 20 //the max msg listened by the server
#define BUFFERSIZE 1024 //the max size of the string to send
int new_server_socket[20]; //the socket for accepted client
int connection_num; //the number of connected clients
char address[20][16]; //store the address of the connected clients
char buffer[BUFFERSIZE]; //string to send
unsigned short int port[20]; //store the port of the connected clients
pthread_t t[20]; //pthread for clients
sem_t sem; //for threads
void connection(int i) //the function used by each thread created below
{
bzero(buffer,BUFFERSIZE); //clear the buffer
int length; //the length of the recieved buffer
while(1)
{
length=recv(new_server_socket[i],buffer,BUFFERSIZE,0);
if(length<=0) //if there is no string recieved, stop the trap
break;
else //if there is string recieved, go on
{
if(buffer[0]=='T') //get the option and if the option is T, then return the time of the server
{
time_t timep;
bzero(buffer,BUFFERSIZE); //clear the buffer, because we will use it to send string for time.
time(&timep);
buffer[0]='\0'; //for using of strcat
strcat(buffer,ctime(&timep));
if(send(new_server_socket[i],buffer,BUFFERSIZE,0)==-1)//judging if the sending is successful
{
printf("获取时间失败!\n");
}
}
else if(buffer[0]=='N') //get the option and if the option is N, then return the name of the server
{
bzero(buffer,BUFFERSIZE);
gethostname(buffer,32);
buffer[33]='\n';
if(send(new_server_socket[i],buffer,32,0)==-1)
{
printf("获取主机名失败!\n");
}
}
else if(buffer[0]=='L') //get the option and if the option is L, then return the list of connected clients
{
sem_wait(&sem); //stop the other threads just for completing the traps below
int j;
bzero(buffer,BUFFERSIZE);
buffer[0]='\0';
char id_j[1];
char port_j[10];
bzero(port_j,sizeof(port_j));
for(j=1;j<=connection_num;j++)
{
sprintf(id_j,"%d",j); //get char from int
sprintf(port_j,"%d",port[j]); //get char from int
if(port[j]==-1) //when the connected client i was shutdown, then we will set port[i]=-1;
continue;
strcat(buffer,"id:");
printf("j:%c\n",id_j[0]);
strcat(buffer,id_j);
strcat(buffer," ip:");
strcat(buffer,address[j]);
strcat(buffer," port:");
strcat(buffer,port_j);
strcat(buffer,"\n"); //construct the string to send
}
if(send(new_server_socket[i],buffer,BUFFERSIZE,0)==-1)
{
printf("获取列表失败!\n");
}
sem_post(&sem);
}
else if(buffer[0]=='S') //get the option and if the option is S, then send message to the specific client
{
int id; //get the id of the specific client
id=buffer[1];
buffer[0]=' '; //clear the unuseful words
buffer[1]=' ';
strcat(buffer,"\n");
if(send(new_server_socket[id],buffer,BUFFERSIZE,0)==-1)
{
printf("发送消息失败,%d\n",id);
}
}
else if(buffer[0]='D') //get the option and if the option is D, then close the connection with the specific client
{
close(new_server_socket[i]); //close the socket for specific client
connection_num--;
port[i]=-1;
address[i][16]="\0";
pthread_cancel(t[i]); //close this thread
}
else
{
printf("nice to meet you!\n");
}
}
}
}
void main()
{
sem_init(&sem,0,1); //initialize the semaphore
struct sockaddr_in server_addr; //initialize the sockaddr_in for server
bzero(address,sizeof(address));
bzero(&server_addr,sizeof(server_addr));
server_addr.sin_family=AF_INET;
server_addr.sin_addr.s_addr=htons(INADDR_ANY); //set address of server: localhost
server_addr.sin_port=htons(SERVERPORT); //set port of server: SERVERPORT
int server_socket=socket(PF_INET,SOCK_STREAM,0); //get the socket,if failed, the return value will be -1
if(server_socket<0)
{
printf("Create Socket Failed!");
exit(1);
}
if(bind(server_socket,(struct sockaddr*)&server_addr,sizeof(server_addr))) //bind the socket before and the server_addr
{
printf("Server Bind Port:%d Failed!",SERVERPORT);
exit(1);
}
if(listen(server_socket,LENGTH_OF_LISTEN_QUEUE)) //watch the socket and limit the number of message waiting in the queue
{
printf("Server Listen Failed!");
exit(1);
}
int i=1;
while(1) //server keeps working
{
struct sockaddr_in client_addr;
socklen_t length=sizeof(client_addr);
new_server_socket[i]=accept(server_socket,(struct sockaddr*)&client_addr,&length); //wait untill the is a request for connecting
if(new_server_socket[i]<0) //if the connection failed, the return value will be -1
{
printf("Server Accept Failed!\n");
break;
}
else
{
sprintf(address[i],"%s",inet_ntoa(client_addr.sin_addr)); //get the address in the type of char
printf("accept %d\n",i);
port[i]=client_addr.sin_port; //get the port in the type of unsigned short int
pthread_create(&t[i],NULL,(void*)(int)connection,(int)i);//create a specific thread for handling the message from connected client
printf("connection_thread:%d\n ok!",i);
connection_num=i;
i++;
}
}
}
#include<time.h>
#include<netinet/in.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<stdio.h>
#include<stdlib.h>
#include<sched.h>
#include<signal.h>
#include<string.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<semaphore.h>
#define SERVERPORT 3333 //the port defined here for server
#define LENGTH_OF_LISTEN_QUEUE 20 //the max msg listened by the server
#define BUFFERSIZE 1024 //the max size of the string to send
int new_server_socket[20]; //the socket for accepted client
int connection_num; //the number of connected clients
char address[20][16]; //store the address of the connected clients
char buffer[BUFFERSIZE]; //string to send
unsigned short int port[20]; //store the port of the connected clients
pthread_t t[20]; //pthread for clients
sem_t sem; //for threads
void connection(int i) //the function used by each thread created below
{
bzero(buffer,BUFFERSIZE); //clear the buffer
int length; //the length of the recieved buffer
while(1)
{
length=recv(new_server_socket[i],buffer,BUFFERSIZE,0);
if(length<=0) //if there is no string recieved, stop the trap
break;
else //if there is string recieved, go on
{
if(buffer[0]=='T') //get the option and if the option is T, then return the time of the server
{
time_t timep;
bzero(buffer,BUFFERSIZE); //clear the buffer, because we will use it to send string for time.
time(&timep);
buffer[0]='\0'; //for using of strcat
strcat(buffer,ctime(&timep));
if(send(new_server_socket[i],buffer,BUFFERSIZE,0)==-1)//judging if the sending is successful
{
printf("获取时间失败!\n");
}
}
else if(buffer[0]=='N') //get the option and if the option is N, then return the name of the server
{
bzero(buffer,BUFFERSIZE);
gethostname(buffer,32);
buffer[33]='\n';
if(send(new_server_socket[i],buffer,32,0)==-1)
{
printf("获取主机名失败!\n");
}
}
else if(buffer[0]=='L') //get the option and if the option is L, then return the list of connected clients
{
sem_wait(&sem); //stop the other threads just for completing the traps below
int j;
bzero(buffer,BUFFERSIZE);
buffer[0]='\0';
char id_j[1];
char port_j[10];
bzero(port_j,sizeof(port_j));
for(j=1;j<=connection_num;j++)
{
sprintf(id_j,"%d",j); //get char from int
sprintf(port_j,"%d",port[j]); //get char from int
if(port[j]==-1) //when the connected client i was shutdown, then we will set port[i]=-1;
continue;
strcat(buffer,"id:");
printf("j:%c\n",id_j[0]);
strcat(buffer,id_j);
strcat(buffer," ip:");
strcat(buffer,address[j]);
strcat(buffer," port:");
strcat(buffer,port_j);
strcat(buffer,"\n"); //construct the string to send
}
if(send(new_server_socket[i],buffer,BUFFERSIZE,0)==-1)
{
printf("获取列表失败!\n");
}
sem_post(&sem);
}
else if(buffer[0]=='S') //get the option and if the option is S, then send message to the specific client
{
int id; //get the id of the specific client
id=buffer[1];
buffer[0]=' '; //clear the unuseful words
buffer[1]=' ';
strcat(buffer,"\n");
if(send(new_server_socket[id],buffer,BUFFERSIZE,0)==-1)
{
printf("发送消息失败,%d\n",id);
}
}
else if(buffer[0]='D') //get the option and if the option is D, then close the connection with the specific client
{
close(new_server_socket[i]); //close the socket for specific client
connection_num--;
port[i]=-1;
address[i][16]="\0";
pthread_cancel(t[i]); //close this thread
}
else
{
printf("nice to meet you!\n");
}
}
}
}
void main()
{
sem_init(&sem,0,1); //initialize the semaphore
struct sockaddr_in server_addr; //initialize the sockaddr_in for server
bzero(address,sizeof(address));
bzero(&server_addr,sizeof(server_addr));
server_addr.sin_family=AF_INET;
server_addr.sin_addr.s_addr=htons(INADDR_ANY); //set address of server: localhost
server_addr.sin_port=htons(SERVERPORT); //set port of server: SERVERPORT
int server_socket=socket(PF_INET,SOCK_STREAM,0); //get the socket,if failed, the return value will be -1
if(server_socket<0)
{
printf("Create Socket Failed!");
exit(1);
}
if(bind(server_socket,(struct sockaddr*)&server_addr,sizeof(server_addr))) //bind the socket before and the server_addr
{
printf("Server Bind Port:%d Failed!",SERVERPORT);
exit(1);
}
if(listen(server_socket,LENGTH_OF_LISTEN_QUEUE)) //watch the socket and limit the number of message waiting in the queue
{
printf("Server Listen Failed!");
exit(1);
}
int i=1;
while(1) //server keeps working
{
struct sockaddr_in client_addr;
socklen_t length=sizeof(client_addr);
new_server_socket[i]=accept(server_socket,(struct sockaddr*)&client_addr,&length); //wait untill the is a request for connecting
if(new_server_socket[i]<0) //if the connection failed, the return value will be -1
{
printf("Server Accept Failed!\n");
break;
}
else
{
sprintf(address[i],"%s",inet_ntoa(client_addr.sin_addr)); //get the address in the type of char
printf("accept %d\n",i);
port[i]=client_addr.sin_port; //get the port in the type of unsigned short int
pthread_create(&t[i],NULL,(void*)(int)connection,(int)i);//create a specific thread for handling the message from connected client
printf("connection_thread:%d\n ok!",i);
connection_num=i;
i++;
}
}
}