当前位置: 技术问答>linux和unix
客户重连服务器的问题
来源: 互联网 发布时间:2015-08-19
本文导语: 服务器代码,如下: #include #include #include #include #include #include #include #include #include #include #include #include #define PORT 15888 #define BUFSIZE 1024 vectorvfds; int Bedaemon(void) { pid_t pid; if ( (pid=fork()) = ...
服务器代码,如下:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 15888
#define BUFSIZE 1024
vectorvfds;
int Bedaemon(void)
{
pid_t pid;
if ( (pid=fork()) = nRetVal )
nRetVal = (*iter);
return nRetVal+1;
}
int main(int argc, char **argv)
{
int serverfd,maxfd;
fd_set readfds;
struct sockaddr_in svaddr;
struct sockaddr_in claddr;
int sin_size;
char buf[BUFSIZE]={0};
Bedaemon();
if ( ( serverfd = socket( AF_INET, SOCK_STREAM, 0 ) ) == -1 )
{
herror("socket");
exit(1);
}
svaddr.sin_family = AF_INET;
svaddr.sin_port = htons(PORT);
svaddr.sin_addr.s_addr = INADDR_ANY;
bzero(&(svaddr.sin_zero),0);
if ( bind(serverfd, (struct sockaddr*)&svaddr, sizeof(struct sockaddr)) == -1 )
{
perror("bind");
exit(1);
}
if ( listen(serverfd,20) == -1 )
{
perror("listen");
exit(1);
}
vfds.push_back( 0 ); // std in
vfds.push_back( serverfd ); // serverfd
FD_ZERO( &readfds );
FD_SET( vfds[0], &readfds );
FD_SET( vfds[1], &readfds );
struct timeval tv;
tv.tv_sec = 2;
tv.tv_usec = 500000;
vector::iterator iter;
int nClientfd;
while(1)
{
maxfd = GetMaxfd( vfds );
printf("max fd is %dn", maxfd);
if ( select(maxfd, &readfds, NULL, NULL, NULL) == -1 )
{
perror("select");
exit(1);
}
if ( FD_ISSET( serverfd, &readfds ) )
{
sin_size = sizeof(struct sockaddr_in);
if ( ( nClientfd = accept(serverfd,(struct sockaddr*)&claddr,
(socklen_t*)&sin_size)) == -1 )
{
perror("accept");
continue;
}
vfds.push_back( nClientfd );
FD_SET( nClientfd, &readfds );
printf("Server:connected form client with addr:%sn",
inet_ntoa(claddr.sin_addr));
}
iter = vfds.begin();
iter = iter + 2;
for ( ; iter != vfds.end(); iter++ )
{
if ( FD_ISSET( (*iter), &readfds ) )
{
int nReadRes = read((*iter), buf, 1024 );
if ( nReadRes == -1 || nReadRes == 0 )
{
perror("read");
FD_CLR( (*iter), &readfds );
shutdown((*iter),2);
vfds.erase( iter );
iter--;
}
else
printf( "The Data is %sn", buf );
}
}
sleep(1);
}
return 0;
}
客户端是在windows下,关闭连接的时候调用了closesocket(sockfd);
连接:
socket();
connect();
第一次连接没问题,但是closesocket之后再连就不行了,服务器在select()这里阻塞了
大侠们帮忙看一下
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 15888
#define BUFSIZE 1024
vectorvfds;
int Bedaemon(void)
{
pid_t pid;
if ( (pid=fork()) = nRetVal )
nRetVal = (*iter);
return nRetVal+1;
}
int main(int argc, char **argv)
{
int serverfd,maxfd;
fd_set readfds;
struct sockaddr_in svaddr;
struct sockaddr_in claddr;
int sin_size;
char buf[BUFSIZE]={0};
Bedaemon();
if ( ( serverfd = socket( AF_INET, SOCK_STREAM, 0 ) ) == -1 )
{
herror("socket");
exit(1);
}
svaddr.sin_family = AF_INET;
svaddr.sin_port = htons(PORT);
svaddr.sin_addr.s_addr = INADDR_ANY;
bzero(&(svaddr.sin_zero),0);
if ( bind(serverfd, (struct sockaddr*)&svaddr, sizeof(struct sockaddr)) == -1 )
{
perror("bind");
exit(1);
}
if ( listen(serverfd,20) == -1 )
{
perror("listen");
exit(1);
}
vfds.push_back( 0 ); // std in
vfds.push_back( serverfd ); // serverfd
FD_ZERO( &readfds );
FD_SET( vfds[0], &readfds );
FD_SET( vfds[1], &readfds );
struct timeval tv;
tv.tv_sec = 2;
tv.tv_usec = 500000;
vector::iterator iter;
int nClientfd;
while(1)
{
maxfd = GetMaxfd( vfds );
printf("max fd is %dn", maxfd);
if ( select(maxfd, &readfds, NULL, NULL, NULL) == -1 )
{
perror("select");
exit(1);
}
if ( FD_ISSET( serverfd, &readfds ) )
{
sin_size = sizeof(struct sockaddr_in);
if ( ( nClientfd = accept(serverfd,(struct sockaddr*)&claddr,
(socklen_t*)&sin_size)) == -1 )
{
perror("accept");
continue;
}
vfds.push_back( nClientfd );
FD_SET( nClientfd, &readfds );
printf("Server:connected form client with addr:%sn",
inet_ntoa(claddr.sin_addr));
}
iter = vfds.begin();
iter = iter + 2;
for ( ; iter != vfds.end(); iter++ )
{
if ( FD_ISSET( (*iter), &readfds ) )
{
int nReadRes = read((*iter), buf, 1024 );
if ( nReadRes == -1 || nReadRes == 0 )
{
perror("read");
FD_CLR( (*iter), &readfds );
shutdown((*iter),2);
vfds.erase( iter );
iter--;
}
else
printf( "The Data is %sn", buf );
}
}
sleep(1);
}
return 0;
}
客户端是在windows下,关闭连接的时候调用了closesocket(sockfd);
连接:
socket();
connect();
第一次连接没问题,但是closesocket之后再连就不行了,服务器在select()这里阻塞了
大侠们帮忙看一下
|
这个程序的框架不大对,可以参照一下stevens的书