Unix Programming - Strange accept prlblem

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > March 2005 > Strange accept prlblem





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Strange accept prlblem
MSR

2005-03-16, 6:02 pm

accept system call frequently returns 0. I have server to which some
clients connect. I got a strange problem while testing. I started the
server and a client and the client connects without any problems. Then
I killed the client and stated and no problem again. I killed it once
again now the accept call from the server returns a 0. Client thinks it
is connected. From man page return value of 0 is not a error. I am
pretty sure that 0 is not a real valid socket. This is very repeatable.
Here is my core code for listening and accepting. I looked at it many
many times but didnt find anything wrong with the code. I am testing on
fedora core release 2.90. Thank you.


bool tcp_connection::listen(int tcpPort)
{

if(tcpPort < 0)
{
frpintf(stderr, "Can't listen on invalid port %d\n", tcpPort);
return false;
}

port = tcpPort;

struct sockaddr_in server;

bzero((char *)&server, sizeof(server));
server.sin_family = AF_INET;
server.sin_port = htons (port);
server.sin_addr.s_addr = htonl(INADDR_ANY);

int tfd = socket (PF_INET, SOCK_STREAM, 0);

set_fd(tfd);

int c=5;

if ((fd < 0) ||
(bind (get_fd(), (struct sockaddr *) &server, sizeof (server)) <
0) ||
(::listen (get_fd(), c) < 0))
{
perror("tcp_connection::listen: error\n");
return false;
}

return true;
}


SOCKET tcp_connection::new_connection(std::stri
ng *ipaddr)
{
if(new_client())
return accept_client(ipaddr);

else return 0;
}


int tcp_connection::new_client()
{
fd_set rfds;
struct timeval time_out;
int selval;

time_out.tv_sec = 0;
time_out.tv_usec = 0;

FD_ZERO(&rfds);
FD_SET((u_int)get_fd(), &rfds);

selval = select(get_fd() + 1, &rfds, NULL, NULL, &time_out);
if (selval == SOCKET_ERROR) {
fprintf(stderr,"tcp_connection::new_client: select error\n");
selval = 0;
}
return (selval);
}


int tcp_connection::accept_client(std::strin
g *ipaddr)
{
SOCKET new_socket;
struct sockaddr_in client_addr;
int socklen;

if(IS_INVALID_SOCKET(port)) {
vsam_error("tcp_connection::accept_client(): No socket on which to
listen\n",1);
return INVALID_SOCKET;
}
socklen = sizeof(client_addr);
new_socket = accept(get_fd(), (struct sockaddr *)&client_addr,
(socklen_t *)&socklen);
if ( IS_INVALID_SOCKET(new_socket) ) {
perror("tcp_connection::accept_client():");
return INVALID_SOCKET;
}

char ip_addr[17];

if(client_addr.sin_family == AF_INET){
sprintf(ip_addr,"%d.%d.%d.%d",
0xFF & ((client_addr.sin_addr.s_addr&0xFF)),
0xFF & ((client_addr.sin_addr.s_addr)>>8),
0xFF & ((client_addr.sin_addr.s_addr)>>16),
0xFF & ((client_addr.sin_addr.s_addr)>>24));

ipaddr->assign(ip_addr);
}
bool tcp_connection::listen(int tcpPort)
{

if(tcpPort < 0)
{
char msg[128];
sprintf(msg, "Can't listen on invalid port %d\n", tcpPort);
vsam_error(msg, 0);
return false;
}

port = tcpPort;

struct sockaddr_in server;

bzero((char *)&server, sizeof(server));
server.sin_family = AF_INET;
server.sin_port = htons (port);
server.sin_addr.s_addr = htonl(INADDR_ANY);

int tfd = socket (PF_INET, SOCK_STREAM, 0);

set_fd(tfd);

int c=5;

if ((fd < 0) ||
(bind (get_fd(), (struct sockaddr *) &server, sizeof (server)) <
0) ||
(::listen (get_fd(), c) < 0))
{
perror("tcp_connection::listen: error\n");
return false;
}

return true;
}


SOCKET tcp_connection::new_connection(std::stri
ng *ipaddr)
{
if(new_client())
return accept_client(ipaddr);

else return 0;
}


int tcp_connection::new_client()
{
fd_set rfds;
struct timeval time_out;
int selval;

time_out.tv_sec = 0;
time_out.tv_usec = 0;

FD_ZERO(&rfds);
FD_SET((u_int)get_fd(), &rfds);

selval = select(get_fd() + 1, &rfds, NULL, NULL, &time_out);
if (selval == SOCKET_ERROR) {
fprintf(stderr,"tcp_connection::new_client: select error\n");
selval = 0;
}
return (selval);
}



SOCKET tcp_connection::accept_client(std::strin
g *ipaddr)
{
SOCKET new_socket;
struct sockaddr_in client_addr;
int socklen;

if(IS_INVALID_SOCKET(port)) {
vsam_error("tcp_connection::accept_client(): No socket on which to
listen\n",1);
return INVALID_SOCKET;
}
socklen = sizeof(client_addr);
new_socket = accept(get_fd(), (struct sockaddr *)&client_addr,
(socklen_t *)&socklen);
if ( IS_INVALID_SOCKET(new_socket) ) {
perror("tcp_connection::accept_client():");
return INVALID_SOCKET;
}

char ip_addr[17];

if(client_addr.sin_family == AF_INET){
sprintf(ip_addr,"%d.%d.%d.%d",
0xFF & ((client_addr.sin_addr.s_addr&0xFF)),
0xFF & ((client_addr.sin_addr.s_addr)>>8),
0xFF & ((client_addr.sin_addr.s_addr)>>16),
0xFF & ((client_addr.sin_addr.s_addr)>>24));

ipaddr->assign(ip_addr);
}

fprintf(stderr, "tcp_connection::accept_client(): New connection "
"on socket %d from ip %s\n", new_socket, ip_addr);

return new_socket;
}



return new_socket;
}


---MSR

Fletcher Glenn

2005-03-16, 6:02 pm

MSR wrote:
> accept system call frequently returns 0. I have server to which some
> clients connect. I got a strange problem while testing. I started the
> server and a client and the client connects without any problems. Then
> I killed the client and stated and no problem again. I killed it once
> again now the accept call from the server returns a 0. Client thinks it
> is connected. From man page return value of 0 is not a error. I am
> pretty sure that 0 is not a real valid socket. This is very repeatable.
> Here is my core code for listening and accepting. I looked at it many
> many times but didnt find anything wrong with the code. I am testing on
> fedora core release 2.90. Thank you.
>
>

<snipped code>
>
> ---MSR
>


Zero is a perfectly valid socket ID if you have closed stdin. If you
have not closed stdin, then you may be having a problem.

--

Fletcher Glenn

MSR

2005-03-16, 6:02 pm

No I have not closed stdin.

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com