LinuxQuestions.org
Help answer threads with 0 replies.
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 07-27-2004, 12:34 PM   #1
bonhomme
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:

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)
   {
      perror("accept()");
      exit(1);
   }

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

Rep: Reputation: 30
printf value of serverSocket.
 
Old 07-27-2004, 03:29 PM   #3
bonhomme
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, 03:37 PM   #4
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

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

int listen(int s, int backlog);

DESCRIPTION
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
type SOCK_STREAM or SOCK_SEQPACKET.
 
Old 07-13-2012, 10:33 AM   #5
litdream
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, 12:12 PM   #6
amboxer21
Member
 
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.
Code:
socklen_t clientLength;
clientLength = sizeof(clientName);
slaveSocket = accept(serverSocket, (struct sockaddr *)&clientName, &clientLength);
Instead you are
Code:
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!

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

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

Last edited by amboxer21; 07-13-2012 at 01:08 PM.
 
Old 07-14-2012, 10:12 PM   #7
tb75
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
}
 
  


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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:26 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration