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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
07-27-2004, 12:34 PM
|
#1
|
LQ Newbie
Registered: Jun 2004
Posts: 5
Rep:
|
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.
|
|
|
07-27-2004, 12:49 PM
|
#2
|
Member
Registered: Mar 2003
Posts: 804
Rep:
|
printf value of serverSocket.
|
|
|
07-27-2004, 03:29 PM
|
#3
|
LQ Newbie
Registered: Jun 2004
Posts: 5
Original Poster
Rep:
|
When I printf the value of serverSocket, it has the value "3" from the socket() call. Is this a clue?
|
|
|
07-27-2004, 03:37 PM
|
#4
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
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.
|
|
|
|
07-13-2012, 10:33 AM
|
#5
|
LQ Newbie
Registered: Jan 2010
Posts: 1
Rep:
|
Common error is firewall rule.
In other words, program is written correct, but system access making trouble.
checking iptables might help.
|
|
|
07-13-2012, 12:12 PM
|
#6
|
Member
Registered: Mar 2012
Location: New Jersey
Distribution: Gentoo
Posts: 291
Rep: 
|
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.
|
|
|
07-14-2012, 10:12 PM
|
#7
|
LQ Newbie
Registered: Mar 2007
Posts: 3
Rep:
|
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
}
|
|
|
All times are GMT -5. The time now is 04:26 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|