当前位置: 技术问答>linux和unix
socket编程使用select的问题?
来源: 互联网 发布时间:2016-01-18
本文导语: 一个简单的程序: Server端使用select监听多个Client,Client发数据给Server,Server把受到的数据打印出来。 出现的问题是:当只有一个Client连接时,无法收到Client端发来的数据,多个Client连接时则正常。 我是使用telnet做Clien...
一个简单的程序:
Server端使用select监听多个Client,Client发数据给Server,Server把受到的数据打印出来。
出现的问题是:当只有一个Client连接时,无法收到Client端发来的数据,多个Client连接时则正常。
我是使用telnet做Client来测试的。
代码如下,各位老大帮看一下:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define BUF_SIZ 1024
int port = 2007;
int main()
{
/* local varibles */
int srv_skt_fd;
int clt_skt_fd;
fd_set set,read_set;
int max_fd;
int i;
char buf[BUF_SIZ];
int len;
struct sockaddr_in serv_addr;
struct sockaddr_in clt_addr;
/* program begin from here */
/* Step1: open a socket */
if ((srv_skt_fd = socket(AF_INET,SOCK_STREAM,0)) == -1)/* TCP socket */
{
perror("socket open");
exit(1);
}
/* Step2: bind it to host address */
bzero(&serv_addr,sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(port);
if (bind(srv_skt_fd,(struct sockaddr*)&serv_addr,sizeof(serv_addr)) == -1)
{
perror("bind");
close(srv_skt_fd);
exit(1);
}
/* Step3: listen */
if (listen(srv_skt_fd,SOMAXCONN) == -1)
{
perror("listen");
close(srv_skt_fd);
exit(1);
}
/* Step4: build a fd_set for select */
FD_ZERO(&set);
/* Step5: add server socket fd to this fd_set */
FD_SET(srv_skt_fd,&set);
max_fd = srv_skt_fd + 1;
/* Step6: write a infinite loop that call select and handle all requests */
while (1)
{
printf("startn");
read_set = set;
/* wait for a request here */
if (select(max_fd,&read_set,NULL,NULL,NULL) == -1)
{
perror("select");
close(srv_skt_fd);
exit(1);
}
printf("after selectn");
/* cleck which one is coming */
for (i = 0;i max_fd)
{
max_fd = clt_skt_fd + 1;
}
}else /* case 2 */
{
/* add your code here */
if ((len = recv(i,buf,BUF_SIZ,0)) == -1)
{
perror("recv");
close(srv_skt_fd);
exit(1);
}
if (len == 0)
{
FD_CLR(i,&set);
if ((i+1) == max_fd)
{
max_fd--;
}
close(i);
}else {
buf[len] = '';
printf("got msg from %d: %sn",i,buf);
}
}
}
}
}
/* Step8: clean up */
return 0;
}
Server端使用select监听多个Client,Client发数据给Server,Server把受到的数据打印出来。
出现的问题是:当只有一个Client连接时,无法收到Client端发来的数据,多个Client连接时则正常。
我是使用telnet做Client来测试的。
代码如下,各位老大帮看一下:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define BUF_SIZ 1024
int port = 2007;
int main()
{
/* local varibles */
int srv_skt_fd;
int clt_skt_fd;
fd_set set,read_set;
int max_fd;
int i;
char buf[BUF_SIZ];
int len;
struct sockaddr_in serv_addr;
struct sockaddr_in clt_addr;
/* program begin from here */
/* Step1: open a socket */
if ((srv_skt_fd = socket(AF_INET,SOCK_STREAM,0)) == -1)/* TCP socket */
{
perror("socket open");
exit(1);
}
/* Step2: bind it to host address */
bzero(&serv_addr,sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(port);
if (bind(srv_skt_fd,(struct sockaddr*)&serv_addr,sizeof(serv_addr)) == -1)
{
perror("bind");
close(srv_skt_fd);
exit(1);
}
/* Step3: listen */
if (listen(srv_skt_fd,SOMAXCONN) == -1)
{
perror("listen");
close(srv_skt_fd);
exit(1);
}
/* Step4: build a fd_set for select */
FD_ZERO(&set);
/* Step5: add server socket fd to this fd_set */
FD_SET(srv_skt_fd,&set);
max_fd = srv_skt_fd + 1;
/* Step6: write a infinite loop that call select and handle all requests */
while (1)
{
printf("startn");
read_set = set;
/* wait for a request here */
if (select(max_fd,&read_set,NULL,NULL,NULL) == -1)
{
perror("select");
close(srv_skt_fd);
exit(1);
}
printf("after selectn");
/* cleck which one is coming */
for (i = 0;i max_fd)
{
max_fd = clt_skt_fd + 1;
}
}else /* case 2 */
{
/* add your code here */
if ((len = recv(i,buf,BUF_SIZ,0)) == -1)
{
perror("recv");
close(srv_skt_fd);
exit(1);
}
if (len == 0)
{
FD_CLR(i,&set);
if ((i+1) == max_fd)
{
max_fd--;
}
close(i);
}else {
buf[len] = '';
printf("got msg from %d: %sn",i,buf);
}
}
}
}
}
/* Step8: clean up */
return 0;
}
|
srv_skt_fd这个,select监听我觉得应该是返回的clientsock和srv_skt_fd之间最大值加1;而不是本身监听端口加1;