#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <assert.h>
#include <fcntl.h>
#define MYPORT 9999
#define BACKLOG 10
#define MAXDATASIZE 128
main()
{
int sockfd,numbytes;
int index = 0;
int num;
int new_fd[128];
char str[6];
struct sockaddr_in their_addr[128];
struct sockaddr_in my_addr;
char buf[MAXDATASIZE+1];
int sin_size,sendNumServer;
fd_set rfds,wfds;
struct timeval tv;
int retval,maxfd=-1;
int keyboard = open("/dev/tty",O_RDONLY | O_NONBLOCK);
assert(keyboard>0);
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket");
exit(1);
}
bzero(&my_addr,sizeof(my_addr));
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(MYPORT);
my_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr))== -1)
{
perror("bind");
exit(1);
}
if (listen(sockfd, BACKLOG) == -1)
{
perror("listen");
exit(1);
}
new_fd[0] = sockfd;
index++;
while(1)
{
FD_ZERO(&rfds);
FD_ZERO(&wfds);
FD_SET(0,&rfds);
FD_SET(keyboard,&rfds);
maxfd=0;
for (int i=0; i<index; i++)
{
FD_SET(new_fd[i], &rfds);
FD_SET(new_fd[i], &wfds);
FD_SET(0,&rfds);
FD_SET(keyboard,&rfds);
maxfd=0;
for (int i=0; i<index; i++)
{
FD_SET(new_fd[i], &rfds);
FD_SET(new_fd[i], &wfds);
if (new_fd[i] > maxfd)
maxfd = new_fd[i];
}
if(keyboard>maxfd)
{
maxfd = keyboard;
}
tv.tv_sec = 10;
tv.tv_usec = 0;
retval = select(maxfd+1, &rfds, NULL, NULL, &tv);
if(retval == -1)
{
printf("select erro\n");
break;
}
else if(retval == 0)
{
printf("no message\n");
continue;
}
if(FD_ISSET(sockfd,&rfds))
{
sin_size = sizeof(struct sockaddr_in);
if ((new_fd[index++] = accept(sockfd, (struct sockaddr*)&their_addr[inde$
{
perror("accept");
continue;
index--;
}
printf("a new client ,server: got connection from %s\n", inet_ntoa(their_addr[index].sin_addr));
}
for(int i = 1; i<index; i++)
{
if(FD_ISSET(new_fd[i], &rfds))
{
bzero(buf, MAXDATASIZE);
numbytes=recv(new_fd[i], buf, MAXDATASIZE, 0);
if(-1 == numbytes)
{
perror("recveive erro");
exit(1);
}
else if(0 == numbytes)
{
printf("client close\n");
return 0;
}
buf[numbytes] = '\0';
printf("receive messagefrom client%d: %s\n",i, buf);
}
}
if(FD_ISSET(keyboard, &rfds))
{
bzero(buf,MAXDATASIZE);
numbytes=read(0, buf, MAXDATASIZE);
if(-1 == numbytes)
{
perror("recv2 input erro");
exit(1);
}
else if(0 == numbytes)
{
printf("client close\n");
return 0;
}
buf[numbytes] = '\0';
printf("receive messagefrom client%d: %s\n",i, buf);
}
}
if(FD_ISSET(keyboard, &rfds))
{
bzero(buf,MAXDATASIZE);
numbytes=read(0, buf, MAXDATASIZE);
if(-1 == numbytes)
{
perror("recv2 input erro");
exit(1);
}
else if(0 == numbytes)
{
printf("read from error\n");
return 0;
}
buf[numbytes]='\0';
printf("input char %s\n",buf);
strncpy(str,buf,3);
num = atoi(str);
if(num>index || num == 0)
{
for(int i = 1; i<index; i++)
{
if(FD_ISSET(new_fd[i], &wfds))
{
sendNumServer = send(new_fd[i], buf, numbytes, 0);
}
if(sendNumServer == -1)
{
printf("server send everyone erro");
return 0;
}
}
printf("you have send message to everyone:%s\n", buf);
}
else
{
if(FD_ISSET(new_fd[num], &wfds))
{
sendNumServer = send(new_fd[num], buf, numbytes, 0);
}
if(sendNumServer == -1)
{
printf("server send message erro\n");
return 0;
}
printf("you have send message to client:%d %s\n",num,buf);
}
}
}
return 0;
}
没有合适的资源?快使用搜索试试~ 我知道了~
select 监控多个客户端代码
共2个文件
txt:2个
3星 · 超过75%的资源 需积分: 10 11 下载量 19 浏览量
2008-09-01
18:07:39
上传
评论
收藏 2KB RAR 举报
温馨提示
SELECT的详细代码,没什么好说的,自己看吧~~~
资源推荐
资源详情
资源评论
收起资源包目录
select.rar (2个子文件)
1.txt 7KB
2.txt 3KB
共 2 条
- 1
资源评论
- 亦也矣2012-03-21写的还可以 但是注释比较少 读起来比较困难 select貌似多客户端有延迟
hubeimacheng
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功