当前位置: 技术问答>linux和unix
Linux select的问题
来源: 互联网 发布时间:2016-07-11
本文导语: 问题描述如下: 客户端在有数据发送时,调用connect(...)连接客户端,然后发送数据,发送完成后。马上断开。为此服务器(为Linux RH9)端采用select模型。但程序测试的时候发现只在客户端第一次连接的时候能否接受到...
问题描述如下:
客户端在有数据发送时,调用connect(...)连接客户端,然后发送数据,发送完成后。马上断开。为此服务器(为Linux RH9)端采用select模型。但程序测试的时候发现只在客户端第一次连接的时候能否接受到数据(select函数有返回)。第二次连接及以后的连接服务器(select函数无返回)。代码如下,大家帮我看下..
【Server端】
void mod_sec_log_thread(void)
{
trace_log("enter mod_sec_log htread....", 9);
struct sockaddr_in address;
int sockListen = socket(AF_INET, SOCK_STREAM, 0);
int tmpSocket;
int cirSocket;
int nSelectRet = 0;
int nCircle = 0;
int nRecv = 0;
int comSockArray[MAX_SOCKET_ARRAY];
int nCurrentConnection = 0;
char szSockRecvBuf[MAX_int_BUF];
struct sockaddr_in tmpAddr;
int nAddressLen = sizeof(struct sockaddr_in);
int address_len = 0;
if ( -1 == sockListen )
{
trace_log("Create ModSecurity List Socket Error", 9);
return;
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = htonl(INADDR_ANY);
address.sin_port = htons(LOCAL_LISTEN_PORT);
address_len = sizeof(address);
if ( -1 == bind(sockListen, &address, address_len))
{
trace_log("error bind...", 9);
return;
}
if (listen(sockListen, 5) == -1)
{
trace_log("error Listen...", 9);
return;
}
fd_set fdread;
FD_ZERO(&fdread);
FD_SET(sockListen,&fdread);
reset_socket_array(comSockArray, MAX_SOCKET_ARRAY);
// just MAX_SOCKET_ARRAY - 1 can comunicate
add_socket_array(sockListen, comSockArray, MAX_SOCKET_ARRAY);
nCurrentConnection++;
while ( 1 )
{
trace_log("enter mod_log...", 9);
int maxSock = get_max_socket_array(comSockArray, MAX_SOCKET_ARRAY);
printf("maxSock: %dn CurrentConnection: %dn", maxSock, nCurrentConnection);
nSelectRet = select(maxSock + 1,
&fdread,
NULL,
NULL,
NULL);
trace_log("after select.......................", 9);
if ( nSelectRet MAX_SOCKET_ARRAY )
{
trace_log("too much connections...", 9);
}
else
{
trace_log("add to socket array....", 9);
printf("current connection:%dn", nCurrentConnection);
tmpSocket = accept(sockListen, &tmpAddr, &nAddressLen);
printf("new socket: %dn", tmpSocket);
FD_SET(tmpSocket, &fdread);
add_socket_array(tmpSocket, comSockArray, MAX_SOCKET_ARRAY);
// add connection count
nCurrentConnection++;
}
}
// communicate event
for ( nCircle = 0; nCircle
客户端在有数据发送时,调用connect(...)连接客户端,然后发送数据,发送完成后。马上断开。为此服务器(为Linux RH9)端采用select模型。但程序测试的时候发现只在客户端第一次连接的时候能否接受到数据(select函数有返回)。第二次连接及以后的连接服务器(select函数无返回)。代码如下,大家帮我看下..
【Server端】
void mod_sec_log_thread(void)
{
trace_log("enter mod_sec_log htread....", 9);
struct sockaddr_in address;
int sockListen = socket(AF_INET, SOCK_STREAM, 0);
int tmpSocket;
int cirSocket;
int nSelectRet = 0;
int nCircle = 0;
int nRecv = 0;
int comSockArray[MAX_SOCKET_ARRAY];
int nCurrentConnection = 0;
char szSockRecvBuf[MAX_int_BUF];
struct sockaddr_in tmpAddr;
int nAddressLen = sizeof(struct sockaddr_in);
int address_len = 0;
if ( -1 == sockListen )
{
trace_log("Create ModSecurity List Socket Error", 9);
return;
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = htonl(INADDR_ANY);
address.sin_port = htons(LOCAL_LISTEN_PORT);
address_len = sizeof(address);
if ( -1 == bind(sockListen, &address, address_len))
{
trace_log("error bind...", 9);
return;
}
if (listen(sockListen, 5) == -1)
{
trace_log("error Listen...", 9);
return;
}
fd_set fdread;
FD_ZERO(&fdread);
FD_SET(sockListen,&fdread);
reset_socket_array(comSockArray, MAX_SOCKET_ARRAY);
// just MAX_SOCKET_ARRAY - 1 can comunicate
add_socket_array(sockListen, comSockArray, MAX_SOCKET_ARRAY);
nCurrentConnection++;
while ( 1 )
{
trace_log("enter mod_log...", 9);
int maxSock = get_max_socket_array(comSockArray, MAX_SOCKET_ARRAY);
printf("maxSock: %dn CurrentConnection: %dn", maxSock, nCurrentConnection);
nSelectRet = select(maxSock + 1,
&fdread,
NULL,
NULL,
NULL);
trace_log("after select.......................", 9);
if ( nSelectRet MAX_SOCKET_ARRAY )
{
trace_log("too much connections...", 9);
}
else
{
trace_log("add to socket array....", 9);
printf("current connection:%dn", nCurrentConnection);
tmpSocket = accept(sockListen, &tmpAddr, &nAddressLen);
printf("new socket: %dn", tmpSocket);
FD_SET(tmpSocket, &fdread);
add_socket_array(tmpSocket, comSockArray, MAX_SOCKET_ARRAY);
// add connection count
nCurrentConnection++;
}
}
// communicate event
for ( nCircle = 0; nCircle