Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 07-27-2004, 01:34 PM   #1
LQ Newbie
Registered: Jun 2004
Posts: 5

Rep: Reputation: 0
Accept(): Invalid Argument

Hi guys,

I'm trying to create a simple server using C. I'm running RH 7.2. When my code runs the following code segment, it returns with the following error:

accept(): invalid argument

I'm not sure why I'm getting this message since my arguments seem to be of the correct type. Anyway, here's my code:

for (;;)
   struct sockaddr_in clientName = {0};
   int slaveSocket, clientLength = sizeof(clientName);

   (void) memset(&clientName, 0, sizeof(clientName));

   slaveSocket = accept(serverSocket, (struct sockaddr *) &clientName, &clientLength);

   if (-1 == slaveSocket)

/* irrelevant code follows */
Anyone have any suggestions? Thanks.
Old 07-27-2004, 01:49 PM   #2
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
printf value of serverSocket.
Old 07-27-2004, 04:29 PM   #3
LQ Newbie
Registered: Jun 2004
Posts: 5

Original Poster
Rep: Reputation: 0
When I printf the value of serverSocket, it has the value "3" from the socket() call. Is this a clue?
Old 07-27-2004, 04:37 PM   #4
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
EINVAL Socket is not listening for connections.
Check out 'man listen'.
#include <sys/socket.h>

int listen(int s, int backlog);

To accept connections, a socket is first created with
socket(2), a willingness to accept incoming connections
and a queue limit for incoming connections are specified
with listen, and then the connections are accepted with
accept(2). The listen call applies only to sockets of
Old 07-13-2012, 11:33 AM   #5
LQ Newbie
Registered: Jan 2010
Posts: 1

Rep: Reputation: 0
Common error is firewall rule.
In other words, program is written correct, but system access making trouble.

checking iptables might help.
Old 07-13-2012, 01:12 PM   #6
Registered: Mar 2012
Location: New Jersey
Distribution: Gentoo
Posts: 291

Rep: Reputation: Disabled
Hey joyce092130.

Your clientLength should contain the size of the structure pointed to by clientName. When it returns it should contain the length.
socklen_t clientLength;
clientLength = sizeof(clientName);
slaveSocket = accept(serverSocket, (struct sockaddr *)&clientName, &clientLength);
Instead you are
struct sockaddr_in clientName = {0};
int slaveSocket, clientLength = sizeof(clientName);

(void) memset(&clientName, 0, sizeof(clientName));
filling the address of clientName with zeros then passing it to accept. Also, clientLength should be of type socklen_t and not int. You would know this if you stopped copying and pasting and read up on this stuff like I told you to!

int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
You can find documentation on or use the terminal man pages.

Also, memset is already of type void, so there is no reason to cast it.
   (void) memset(&clientName, 0, sizeof(clientName));

Last edited by amboxer21; 07-13-2012 at 02:08 PM.
Old 07-14-2012, 11:12 PM   #7
LQ Newbie
Registered: Mar 2007
Posts: 3

Rep: Reputation: 0
Seems you have missed the listen function (you are using TCP), which should be called before you enter your for loop,

Algorithm should be

1)get the socket;(call the socket function)
2)populate a address structure, which contains the details of your server.
3)Associate the server's address this with your socket(bind)
4)listen (change the state of your socket so that now you can accept connections - applicable for TCP)
for( ; ; )
//Where you accept connection (that is get the connection socket) and do data transfer


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
alsamixer invalid argument subaruwrx Linux - Software 1 03-12-2005 02:34 PM
KDSKBENT: Invalid Argument tommytomato Linux - Newbie 6 08-27-2004 11:23 PM
[ Invalid Argument on 'iptables' ] benleung Linux - Security 1 09-23-2003 01:25 AM
xawtv - invalid argument liguorir Linux - Software 0 06-17-2003 09:55 PM
invalid numeric argument Copenhagen Cowboy Linux - General 4 03-29-2001 03:01 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:30 AM.

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