LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-26-2007, 10:07 AM   #1
KKKKKKKK
LQ Newbie
 
Registered: Jun 2005
Location: France
Distribution: Fedora 6
Posts: 7

Rep: Reputation: 0
accept() : invalid argument


Hi,

I'm trying to create a server using C. I'm running FC6.

When my code runs, it returns with the following error :

accept() : invalid argument

but ONLY AFTER the first client connection (the first connection is OK).

The same code was working without problem under FC3 and RH7




int tcp_waitCnx(void)
{
socklen_t *T_adLen;
struct sockaddr_in S_client;
int i_clientSockFD;

T_adLen = sizeof(struct sockaddr);
if ( (i_clientSockFD = accept(i_sockFD, (struct sockaddr *)&S_client, &T_adLen)) == -1 )
{
perror("accept");
return (-1);
}

return(i_clientSockFD)
}



I do this before :

int tcp_init(int i_tcpServerPort)
{
char ct_myHostName[1024];
struct hostent *Sp_myHostEnt;
struct sockaddr_in S_serveur;

if (gethostname(ct_myHostName, sizeof(ct_myHostName)) == -1)
{ EVENT_PRINT(SYS, "gethostname()"); return(-1); }

if ( (Sp_myHostEnt = gethostbyname(ct_myHostName)) == NULL )
{ EVENT_PRINT(SYS, "gethostbyname()"); return(-1); }

if ( (i_sockFD = socket(AF_INET, SOCK_STREAM, 0)) == -1 )
{ EVENT_PRINT(SYS, "socket()"); return(-1); }

if (fcntl(i_sockFD, F_SETFL, O_NONBLOCK) == -1)
{ EVENT_PRINT(SYS, "fcntl()"); return(-1); }

bzero(&S_serveur, sizeof(S_serveur));
S_serveur.sin_family = AF_INET;
S_serveur.sin_port = htons(i_tcpServerPort);
bcopy(Sp_myHostEnt->h_addr_list[0], &S_serveur.sin_addr, Sp_myHostEnt->h_length);

if (bind(i_sockFD, (struct sockaddr *)&S_serveur, sizeof(S_serveur)) == -1)
{ EVENT_PRINT(SYS, "bind()"); return(-1); }

if (listen(i_sockFD, 10) == -1)
{ EVENT_PRINT(SYS, "listen()"); return(-1); }

return (0);
}

I need help

Thanks in advance

Jean-Luc
 
Old 01-26-2007, 12:58 PM   #2
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Three suggestions:
Code:
socklen_t T_adLen; // Do *not* declare this as a pointer
Code:
int tcp_waitCnx(SOCKET listener_socket)
{
  ...
  if ( (i_clientSockFD = accept(listener_socket, (struct sockaddr *)&S_client, &T_adLen)) == -1 )
  {  // Do *not* make "i_sockFD" a global variable
... and ...

Please use "[code]" tags when you post code.

'Hope that helps .. PSM
 
Old 01-26-2007, 04:21 PM   #3
KKKKKKKK
LQ Newbie
 
Registered: Jun 2005
Location: France
Distribution: Fedora 6
Posts: 7

Original Poster
Rep: Reputation: 0
Hi,

paulsm4 thanks for your help.

1st suggestion : OK, it's a typo error then I write to this forum (could not work with FC3 & RH7) !

2nd suggestion : why not a global variable ? I have printed i_sockFD before calling accept() and the result was allways 3 !

and Ok for the third suggestion (for the next time I post) !

Jean-Luc
 
Old 01-27-2007, 04:56 PM   #4
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi, again -

The problem is almost certainly with your listener socket. Maybe you're inadvertantly closing it somewhere along the way? Or maybe one some function its visible to is inadvertantly closing it (it *is* global, after all)?

In any case, the accept error "invalid argument" suggests that your listener socket is no longer valid at that point in your program.

'Hope that helps .. PSM
 
Old 01-29-2007, 04:06 AM   #5
KKKKKKKK
LQ Newbie
 
Registered: Jun 2005
Location: France
Distribution: Fedora 6
Posts: 7

Original Poster
Rep: Reputation: 0
Hi Paul,

First a great great thanks to you (you give me the KEY).

The problem came from the child (after a fork()).

The child closed the connexion of the listener socket (normal !),

but I add just before this a shutdown(i_sockFD, ..) and this was the problem.

This shutdown() wasn't on the RH7 and FC3 version !

Again, thanks for your help

Jean-Luc
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Accept(): Invalid Argument bonhomme Programming 6 07-14-2012 10:12 PM
sendto: invalid argument Yury Programming 12 11-04-2006 06:06 AM
Invalid Argument Error ahmedb72 Red Hat 1 02-25-2006 07:52 AM
xinetd error message on Suse 9.1: service xxx, accept: Invalid argument (errno = 22) alteraffe Linux - Networking 1 02-15-2006 04:44 PM
KDSKBENT: Invalid Argument tommytomato Linux - Newbie 6 08-27-2004 10:23 PM


All times are GMT -5. The time now is 10:17 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration