从开源中国里拿来的,学习学习吧,看着还好吧!
Distributed File Sharing System.zip:
else if(argv[1][0] == 's')
{
int opt = TRUE,master_socket , addrlen , new_socket , client_socket[30], activity, i , valread , sd,j,k =9,l = 0,m,max_sd,eff,kk,fee,effd,PORT;
struct sockaddr_in address;
char buffer[1025],buf[25],prtn[30],ptnumb[30], sln[4],slchar[4],msgdis[100];
fd_set readfds,connfs;
for (i = 0; i < max_clients; i++)
{
client_socket[i] = 0;
}
if( (master_socket = socket(AF_INET , SOCK_STREAM , 0)) == 0)
{
perror("socket failed");
exit(EXIT_FAILURE);
}
if( setsockopt(master_socket, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt)) < 0 )
{
perror("setsockopt");
exit(EXIT_FAILURE);
}
PORT = atoi(argv[2]);
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons( PORT );
strcpy(masteraddr,"128.205.36.8");
serverip[0].setvalues(ntohs(address.sin_port),masteraddr,0);//correct here
szls = 1;
if (bind(master_socket, (struct sockaddr *)&address, sizeof(address))<0)
{
perror("bind failed");
exit(EXIT_FAILURE);
}
printf("Listener on port %d \n", PORT);
if (listen(master_socket, 3) < 0)
{
perror("listen");
exit(EXIT_FAILURE);
}
addrlen = sizeof(address);
puts("Waiting for connections ...");
printf("Please enter your option :\nPlease note that you can enter only HELP, CREATOR, DISPLAY AND QUIT for the server\n");
HELP();
while(TRUE)
{
FD_ZERO(&readfds);
FD_SET(master_socket, &readfds);
FD_SET(STDIN, &readfds);
max_sd = master_socket;
if(STDIN > max_sd)
{ max_sd = STDIN;
}
for ( i = 0 ; i < max_clients ; i++)
{
sd = client_socket[i];
if(sd > 0)
FD_SET( sd , &readfds);
if(sd > max_sd)
max_sd = sd;
}
activity = select( max_sd + 1 , &readfds , NULL , NULL , NULL);
if ((activity < 0) && (errno!=EINTR))
{
printf("select error");
}
if(FD_ISSET(STDIN, &readfds))
{
fgets(buf,40,stdin);
buf[strlen(buf)-1]='\0';
if(strcmp(buf, "HELP") == 0)
{
HELP();
}
else if(strcmp(buf, "CREATOR") == 0)
{
CREATOR();
}
else if(strcmp(buf, "DISPLAY") == 0)
{
DISPLAY(PORT);
}
else if(strcmp(buf, "QUIT") == 0)
{
int nkk;
printf("Quitting Server\n");
setbuffer();
for (nkk = 0; nkk < max_clients; nkk++)
{
if(client_socket[nkk] > 0)
{
if( send(client_socket[nkk], message, strlen(message), 0) != strlen(message) )
{
perror("send");
}
}
}
close(master_socket);
for (nkk = 0; nkk < max_clients; nkk++)
{
sd = client_socket[nkk];
close(sd);
}
exit(0);
}
else
{
printf("Enter a valid option from the list of commands, type HELP to get the List of commands\n");
}
printf("Please enter your option :\n");
}
if (FD_ISSET(master_socket, &readfds)) {
if ((new_socket = accept(master_socket, (struct sockaddr *)&address, (socklen_t*)&addrlen))<0)
{
perror("accept");
exit(EXIT_FAILURE);
}
int n1,n2;char bur[50];
bzero(bur,256);
n1 = read(new_socket,bur,255);
if (n1 < 0) printf("ERROR reading from socket");
printf("New connection,ip is : %s\n",inet_ntoa(address.sin_addr));
n2 = atoi(bur);
for (i = 0; i < max_clients; i++)
{
if( client_socket[i] == 0 )
{
client_socket[i] = new_socket;
serverip[i+1].setvalues(n2,inet_ntoa(address.sin_addr),i+1);
printf("----------------------\n");
for(eff = 0; eff <= i+1; eff++)
{
serverip[eff].listm();
}
if(szls < eff)
{
szls = eff;
}
break;
}
}
setbuffer();
for (kk = 0; kk < max_clients; kk++)
{
if(client_socket[kk] > 0)
{
if( send(client_socket[kk], message, strlen(message), 0) != strlen(message) )
{
perror("send");
}
}
}
}
for (i = 0; i < max_clients; i++)
{
sd = client_socket[i];
if (FD_ISSET( sd , &readfds))
{
valread = read( sd , buffer, 1024);
if (valread == 0)
{
getpeername(sd , (struct sockaddr*)&address , (socklen_t*)&addrlen);
for(fee = 0; fee <= szls; fee++)
{
if(strcmp(inet_ntoa(address.sin_addr), serverip[fee].ipa) == 0)
{
strcpy(serverip[fee].ipa,"-2");
}
}
close( sd );
client_socket[i] = 0;
setbuffer();
for (kk = 0; kk < max_clients; kk++)
{
if(client_socket[kk] > 0)
{
if( send(client_socket[kk], message, strlen(message), 0) != strlen(message) )
{
perror("send");
}
}
}
}
else if(strncmp(buffer, "TERMINATE", 9) == 0)
{
for(effd = 0; effd < szls; effd++)
{
sprintf(slchar,"%d",serverip[effd].slno);
if(buffer[10] == slchar[0])
{
strcpy(serverip[effd].ipa,"-2");
sd = client_socket[effd-1];
strcpy(msgdis,"Client has been remotely disconnected\n");
if(send(client_socket[effd-1], msgdis, strlen(msgdis), 0) != strlen(msgdis) )
{
perror("send");
}
close(sd);
client_socket[effd-1] = 0;
break;
}
}
setbuffer();
for (kk = 0; kk < max_clients; kk++)
{
if(client_socket[kk] > 0)
{
if( send(client_socket[kk], message, strlen(message), 0) != strlen(message) )
{
perror("send");
}
}
}
}
else if(strncmp(buffer, "QUIT", 4) == 0)
{
close( sd );
client_socket[i] = 0;
strcpy(serverip[i+1].ipa,"-2");
setbuffer();
for (kk = 0; kk < max_clients; kk++)
{
if(client_socket[kk] > 0)
{
if( send(client_socket[kk], message, strlen(message), 0) != strlen(message) )
{
perror("send");
}
}
}
}
else
{
}
}
}
}
}