#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#define ___PORT 1357
#define ___select_max (50*4)
static fd_set readfds_restore;
static int sockfd_server;
fd_set *get_fd_set2(void);
void fd_set2(int fd);
void fd_clr2(int fd);
void fd_zero2(void);
void process_data(int sockfd, char *buf);
int main(int argc, char *argv[])
{
int newsockfd,fd;
struct sockaddr_in addr;
int addr_len;
fd_set readfds;
time_t local_time;
int readbytes;
char buffer[256];
char msg[]="Welcome 2gliethttp server!";
addr_len = sizeof(struct sockaddr_in);
if((sockfd_server = socket(PF_INET,SOCK_STREAM,0)) < 0)//申请套接字
{
perror("socket");
return 0;
}
memset(&addr, 0, sizeof(addr));
addr.sin_family =PF_INET;
addr.sin_port = htons(___PORT);//该套接字对___PORT端口数据进行监听
addr.sin_addr.s_addr = htonl(INADDR_ANY);//处理来自PC上的任何一块网卡数据
if(bind(sockfd_server, (struct sockaddr *)&addr, sizeof(addr)) < 0)//将套接字和配置参数绑定在一起
{
perror("connect");
return 0;
}
if(listen(sockfd_server, 5) < 0)//同一时刻最多允许5个连接
{
perror("listen");
return 0;
}
fd_zero2();
fd_set2(sockfd_server);
for(;;)
{
readfds = *get_fd_set2();
printf("\nselecting...\n");
if(!select(___select_max, &readfds ,NULL ,NULL ,NULL))
continue;
for(fd = 0;fd < ___select_max;fd++)
{
if(FD_ISSET(fd, &readfds))
{
if(sockfd_server == fd)
{
//监听端口有连接触发请求了,那么处理之
if((newsockfd = accept(sockfd_server,(struct sockaddr *)&addr,(socklen_t*)&addr_len)) < 0)
perror("accept");
fprintf(stderr,"=====%d=====\n",newsockfd);
fd_set2(newsockfd);
time(&local_time);
sprintf(buffer, "<%d> : %s %s", newsockfd, msg, ctime(&local_time));
write(newsockfd, buffer, strlen(buffer));
printf("conect: <%d> from %s %s\n", newsockfd, inet_ntoa(addr.sin_addr), ctime(&local_time));
}
else
{
readbytes = read(fd, buffer, sizeof(buffer));
if(readbytes <= 0)
{
time(&local_time);
printf("closed: <%d> %s\n", fd, ctime(&local_time));
fd_clr2(fd);
close(fd);
}
else
{
buffer[readbytes] = 0;
process_data(fd, buffer);
}
}
}
}
}
}
fd_set *get_fd_set2(void)
{
return &readfds_restore;
}
void fd_set2(int fd)
{
FD_SET(fd, get_fd_set2());
}
void fd_clr2(int fd)
{
FD_CLR(fd, get_fd_set2());
}
void fd_zero2(void)
{
FD_ZERO(get_fd_set2());
}
void process_data(int sockfd, char *buf)
{
char *pdata;
int dest_sock;
int tmp_len;
char buffer[1024*10];
if(strncmp("ls", buf, 2) == 0)
{
fd_set *pfd_set;
int fd;
char *p;
pfd_set = get_fd_set2();
p = buffer;
for(fd = 0;fd < ___select_max;fd++)
{
if(FD_ISSET(fd, pfd_set))
{
if(fd == sockfd)p += sprintf(p, "*", fd);
p += sprintf(p, "%d\n", fd);
}
}
write(sockfd, buffer, strlen(buffer));
return;
}
#if 0
pdata = strtok(buf, ":");
if(pdata == NULL)
{
}
else
#endif
{
pdata = buf;
dest_sock = strtol(pdata, 0, 0);//atoi(pdata);strtol
if((dest_sock < 1024) && FD_ISSET(dest_sock, get_fd_set2()))
{
if(dest_sock < 10)tmp_len = 1;
else if(dest_sock < 100)tmp_len = 2;
else if(dest_sock < 1000)tmp_len = 3;
else tmp_len = 4;
//pdata = strtok(buf, ":");
//pdata += strlen(pdata) + 1;
pdata += tmp_len;
if(dest_sock == sockfd_server)
{
printf("<%d> %s", sockfd, pdata);
fflush(stdout);
}
else
{
sprintf(buffer, "<%d> : %s", sockfd, pdata);
pdata = buffer;
write(dest_sock, pdata, strlen(pdata));
}
}
else
{
sprintf(buffer, "client : %d not exist\n",dest_sock);
write(sockfd, buffer, strlen(buffer));
}
}
}
|