socket编程(Linux下使用select函数实现多客户端连接服务器)

使用socket网络编程实现fsystem功能。(服务器和多个客户端通信)
sock服务器端
【socket编程(Linux下使用select函数实现多客户端连接服务器)】#include
#include
#include
#include
#include
#include
#include
#include
#include "pmanager.h"


#define SERVER_PORT 6666
#define MAXIMUM_LENGTH 10
#define BUFES 20
#define SIZE 10


void cmd_finish(int fd,int status)
{


}


int main(void)
{
int fd_server, fd_clint, sockfd;
int n, i, maxi;
int change;
char buf[BUFES];
fd_set allfds;
fd_set rfds;
int max_fd;
char ch;
int client[SIZE];
socklen_t len = sizeof(struct sockaddr_in);
struct sockaddr_in clint_addr;


struct sockaddr_in server_addr;
bzero(&server_addr, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(SERVER_PORT);
server_addr.sin_addr.s_addr = INADDR_ANY;


fd_server = socket(AF_INET, SOCK_STREAM, 0);
if(fd_server < 0)
{
perror("fail to create\n");
exit(1);
}


int opt = 1;
setsockopt(fd_server,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));


if(bind(fd_server, (struct sockaddr *) &server_addr, sizeof(server_addr)))
{
perror("fail to bind\n");
exit(1);
}


if(listen(fd_server, MAXIMUM_LENGTH))
{
perror("fail to listen\n");
exit(1);
}


pmanager_install(cmd_finish);
for(i=0; i{
client[i] = -1;
}


max_fd = fd_server;
maxi = -1;
FD_ZERO(&allfds);
FD_SET(0, &allfds);
FD_SET(fd_server, &allfds);


while(1)
{
rfds = allfds;
change = select(max_fd + 1, &rfds, NULL, NULL, NULL);
if(change <= 0)
{
continue;
}


printf("there %d event to process\n",change);


if(FD_ISSET(0, &rfds))
{
printf("you have pressed any key\n");
ch = getchar();
if((ch == 'q') || (ch == 'Q'))
{
printf("Server will quit\n");
exit(1);
}
}


if(FD_ISSET(fd_server, &rfds))
{
fd_clint = accept(fd_server, (struct sockaddr*)&clint_addr, &len);
if(fd_clint < 0)
{
perror("fail to accept\n");
continue;
}


for(i=0; i<=SIZE; i++)
{
if(client[i] < 0)
{
client[i] = fd_clint;
FD_SET(client[i], &allfds);
break;
}


}




if(fd_clint > max_fd)
{
max_fd = fd_clint;
}


if(i > maxi)
{
maxi = i;
}
}


for(i=0; i<=maxi; i++)
{


sockfd = client[i];
if(sockfd < 0)
{
continue;
}


if(FD_ISSET(sockfd, &rfds))
{
n = recv(sockfd, buf, BUFES, 0);
if(n > 0)
{
system(buf);
}
else if(n == 0)
{
close(sockfd);
FD_CLR(sockfd, &allfds);
client[i] = -1;
}


if(change < 0)
{
break;
}


}


}


}


if(close(fd_server) < 0)
{
perror("fail to close fd_server\n");
exit(1);
}


return 0;
}






客户端
#include
#include
#include
#include
#include
#include
#include
#include
#include




#define SERVER_PORT 6666
#define BUFES 20


int main(void)
{
int fd_clint;
int n;


struct sockaddr_in clint_addr;
bzero(&clint_addr, sizeof(clint_addr));
clint_addr.sin_family = AF_INET;
clint_addr.sin_port = htons(SERVER_PORT);
clint_addr.sin_addr.s_addr = INADDR_ANY;


fd_clint = socket(AF_INET, SOCK_STREAM, 0);
if(fd_clint < 0)
{
perror("fail to socket\n");
exit(1);
}


if(connect(fd_clint, (struct sockaddr*)&clint_addr, sizeof(clint_addr)) < 0)
{
perror("fail to connect\n");
exit(1);
}


fd_set readfds;
int max_fd = fd_clint + 1;


while(1)
{
FD_ZERO(&readfds);
FD_SET(0, &readfds);
FD_SET(fd_clint, &readfds);


char buf[BUFES];


printf("Please enter command!\n");


if(select(max_fd, &readfds, NULL, NULL, NULL) > 0)
{
if(FD_ISSET(0, &readfds))
{
scanf("%s", buf);
n = send(fd_clint, buf, BUFES, 0);


if(n < 0)
{
perror("send falied\n");
exit(1);
}
}


if(FD_ISSET(fd_clint, &readfds))
{
n = read(fd_clint, buf, BUFES);


if(n == 0)
{
perror("server has closed \n");
exit(1);
}
else if(n < 0)
{
perror("socket failed\n");
exit(1);
}
}
}


}


if(close(fd_clint) < 0)
{
perror("fail to close fd_clint\n");
exit(1);
}


return 0;
}




    推荐阅读