Networking Forums

Networking Forums > Computer Networking > Linux Networking > accept trouble

Reply
Thread Tools Display Modes

accept trouble

 
 
stef
Guest
Posts: n/a

 
      09-16-2005, 10:27 PM
Hello guys...


Could you tell me why this little tcp server code below
send me an ernno 22 : Invalid argument
when I try to connect it with telnet client (or another)

It seems its accept() which fails but why ???

I've tested this code under CygWin, it works, but under Linux
(gentoo 2005 et RH 7.2) it doesn't


#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <setjmp.h>
#ifdef SUN
#include <signal.h>
#else
#include <sys/signal.h>
#endif
#include <sys/wait.h>
#include <dlfcn.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>

#define SA struct sockaddr
#define SA_IN struct sockaddr_in



/*
*/
static int init_nw(char *address, int port, SA_IN *servaddr)
{
struct hostent *inf;
int f_on=1;
int s;

s = socket(AF_INET, SOCK_STREAM, 0);

if (s<0)
return 0;

setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void *) &f_on, sizeof(int));
setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, (void *) &f_on, sizeof(int));

memset(servaddr, 0, sizeof(SA_IN));
servaddr->sin_family = AF_INET;
servaddr->sin_port = htons((unsigned short)port);

if (address==NULL || strlen(address)==0)
servaddr->sin_addr.s_addr = htonl(INADDR_ANY);
else
servaddr->sin_addr.s_addr = inet_addr(address);

if (servaddr->sin_addr.s_addr==INADDR_NONE)
{
inf = gethostbyname(address);

if (inf==NULL)
return 0;

memcpy( &servaddr->sin_addr.s_addr, inf->h_addr, inf->h_length );
}

return s;
}






/*
*
*/
int main(int argc, char *argv[])
{
int sock, sd, r;
SA_IN servaddr;
int rf, f=0;
char ok[2048];
SA addr;
socklen_t fromlen;

sd = init_nw("", 5600, &servaddr);

if (sd<=0)
{
exit(500);
}

// blocking
r = fcntl(sd, F_SETFL, f & (~O_NONBLOCK) );

if (r<0)
{
exit(500);
}


if ( bind(sd, (SA *) &servaddr, sizeof(servaddr))==-1 )
{
exit(501);
}


r = listen( sd, 5);


while(1)
{
errno=0;

/* always send errno = 22 Invalid argument ?????? */
fromlen = sizeof(SA);
sock = accept(sd, (SA *) &addr, &fromlen);

if (errno==0)
{
rf = fork();


/* father will still listener forever */
if (!rf)
{
int x;
fcntl(sock, F_SETFL, f & (~O_NONBLOCK) );

shutdown(sd, SHUT_RDWR);

recv(sock, ok, 2048, 0);

sprintf(ok, "01101996 123 444");
send(sock, ok, strlen(ok)+1, 0);

/* no return from client just to "block" the child */
recv(sock, ok, 2048, 0);

shutdown(sock, SHUT_RDWR);

break;
}
}
else
{
perror(">>>");
}
}



shutdown(sd, SHUT_RDWR);

return 0;
}

 
Reply With Quote
 
 
 
 
chris dewbery
Guest
Posts: n/a

 
      09-19-2005, 11:50 PM
stef wrote:
> Hello guys...
>
>
> Could you tell me why this little tcp server code below
> send me an ernno 22 : Invalid argument
> when I try to connect it with telnet client (or another)
>
> It seems its accept() which fails but why ???
>
> I've tested this code under CygWin, it works, but under Linux
> (gentoo 2005 et RH 7.2) it doesn't
>
>
> #include <stdio.h>
> #include <unistd.h>
> #include <fcntl.h>
> #include <time.h>
> #include <setjmp.h>
> #ifdef SUN
> #include <signal.h>
> #else
> #include <sys/signal.h>
> #endif
> #include <sys/wait.h>
> #include <dlfcn.h>
> #include <sys/types.h>
> #include <sys/socket.h>
> #include <netinet/in.h>
> #include <arpa/inet.h>
> #include <netdb.h>
> #include <unistd.h>
> #include <errno.h>
> #include <stdio.h>
> #include <stdlib.h>
> #include <strings.h>
>
> #define SA struct sockaddr
> #define SA_IN struct sockaddr_in
>
>
>
> /*
> */
> static int init_nw(char *address, int port, SA_IN *servaddr)
> {
> struct hostent *inf;
> int f_on=1;
> int s;
>
> s = socket(AF_INET, SOCK_STREAM, 0);
>
> if (s<0)
> return 0;
>
> setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void *) &f_on, sizeof(int));
> setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, (void *) &f_on, sizeof(int));

what do these calls to setsockopt return?
>
> memset(servaddr, 0, sizeof(SA_IN));
> servaddr->sin_family = AF_INET;
> servaddr->sin_port = htons((unsigned short)port);
>
> if (address==NULL || strlen(address)==0)
> servaddr->sin_addr.s_addr = htonl(INADDR_ANY);
> else
> servaddr->sin_addr.s_addr = inet_addr(address);
>
> if (servaddr->sin_addr.s_addr==INADDR_NONE)
> {
> inf = gethostbyname(address);
>
> if (inf==NULL)
> return 0;
>
> memcpy( &servaddr->sin_addr.s_addr, inf->h_addr, inf->h_length );
> }
>
> return s;
> }
>
>
>
>
>
>
> /*
> *
> */
> int main(int argc, char *argv[])
> {
> int sock, sd, r;
> SA_IN servaddr;
> int rf, f=0;
> char ok[2048];
> SA addr;

perhaps addr should be defined as SA_IN

> socklen_t fromlen;
>
> sd = init_nw("", 5600, &servaddr);
>
> if (sd<=0)
> {
> exit(500);
> }
>
> // blocking
> r = fcntl(sd, F_SETFL, f & (~O_NONBLOCK) );
>
> if (r<0)
> {
> exit(500);
> }
>
>
> if ( bind(sd, (SA *) &servaddr, sizeof(servaddr))==-1 )
> {
> exit(501);
> }
>
>
> r = listen( sd, 5);


what does listen return?

>
>
> while(1)
> {
> errno=0;
>
> /* always send errno = 22 Invalid argument ?????? */
> fromlen = sizeof(SA);
> sock = accept(sd, (SA *) &addr, &fromlen);
>
> if (errno==0)
> {
> rf = fork();
>
>
> /* father will still listener forever */
> if (!rf)
> {
> int x;
> fcntl(sock, F_SETFL, f & (~O_NONBLOCK) );
>
> shutdown(sd, SHUT_RDWR);
>
> recv(sock, ok, 2048, 0);
>
> sprintf(ok, "01101996 123 444");
> send(sock, ok, strlen(ok)+1, 0);
>
> /* no return from client just to "block" the child */
> recv(sock, ok, 2048, 0);
>
> shutdown(sock, SHUT_RDWR);
>
> break;
> }
> }
> else
> {
> perror(">>>");
> }
> }
>
>
>
> shutdown(sd, SHUT_RDWR);
>
> return 0;
> }
>

can't see any major flaws at quick glance, sorry i can't be of more help
 
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
snmp accept from all MSNews Windows Networking 1 09-23-2009 07:29 PM
listen() and accept() Amit Yadav Linux Networking 1 01-09-2005 02:50 PM
getting client ip before accept? Stephan Dreyer Linux Networking 4 09-26-2004 03:08 AM
WAG54G and ZoneAlarm..... trouble trouble trouble... MP Wireless Internet 4 07-28-2004 10:42 AM
adapter doesn't accept wep key david Broadband Hardware 1 04-13-2004 12:02 PM



1 2 3 4 5 6 7 8 9 10 11