TCP socket: "accept" failed: Invalid argument

TCP socket: "accept" failed: Invalid argument

Post by Shichen » Wed, 22 Sep 1999 04:00:00



I test a TCP server sample code in C; and have
the error message receved from the "accept" call:

in the code, I declared:

......
struct sockaddr_in fsin;
        char *service = "daytime";
        int msock, ssock;
        int alen = sizeof(struct sockaddr);
......

msock = passivetcp(service, QLEN); /*call another function to create the
TCP socket, QLEN = 5;*/

printf("msock: %d\n", msock);
...
ssock = accept(msock, (struct sockaddr *)&fsin, &alen);
if (ssock < 0 ) {
        printf("errno: %d\n", errno);
        errexit("accept failed: %s\n", sys_errlist[errno]);}
...

after compiling, I run the code, yet received the following
errors:

[44]% tcpdaytimed&
[1] 3379
[45]% The port is: 8013
msock: 3
errno: 22
accept failed: Invalid argument

"tcpdaytimed" is the name of the executable.
as you can see the TCP socket may have been created successfully,
since "msock" is "3"; the TCP socket uses the no. 8013 port;
yet, the code generates the no. "22" error - "Invalid argument"!!!

Hope any can advice me on how to get around this problem.

Thanks,

Shicheng

PS: the code runs on Unix SunOS 5.6 system.

------------------------------------------------------
/* passivetcp.c */
int
passivetcp( service, qlen )
char *service;          /* service associated with the desired port */
int qlen;               /* maximum sever request queue length */
{
        return passivesock( service, "tcp", qlen);

Quote:}

--------------------------------------------------------
/* passivesock.c - passivesock */

#include <sys/types.h>
#include <sys/socket.h>

#include <netinet/in.h>
#include <netdb.h>

extern int errno;
extern char *sys_errlist[];

u_short htons(), ntohs();
u_short portbase = 8000;                /* port base, for non-root
servers */

/*-----------------------------------------------------------
 * passivesock - allocate & bind a server socket using TCP or UDP
 *-----------------------------------------------------------
 */

int
passivesock( service, protocol, qlen )
char *service;  /* service associated with the desired port     */
char *protocol; /* name of protocol to use ("tcp" or "udp") */
int qlen;       /* maximum length of the server request queue   */
{
        struct servent *pse;    /* pointer to service information entry
*/
        struct protoent *ppe;   /* pointer to protocol information entry
*/
        struct sockaddr_in sin; /* an Internet endpoint address */
        int s, type;            /* socket descriptor and socket type */
        int n = 10;

        bzero((char *)&sin, sizeof(sin));
        sin.sin_family = AF_INET;
        sin.sin_addr.s_addr = INADDR_ANY;

        /* Map service name to port number */
        if ( pse = getservbyname(service, protocol) )
                sin.sin_port = htons( ntohs((u_short)pse->s_port) +
portbase);
        else if ( (sin.sin_port = htons((u_short)atoi(service))) == 0 )
                errexit("can't get \"%s\" service entry\n", service);

        printf("The port is: %d\n", sin.sin_port);

        /* Map protocol name to protocol number */
        if ( (ppe = getprotobyname(protocol)) == 0 )
                errexit("con't get \"%s\" protocol entry\n", protocol);

        /* Use protocol to choose a socket type */
        if (strcmp(protocol, "udp") == 0)
                type = SOCK_DGRAM;
        else
                type = SOCK_STREAM;

        /* Allocate a socket */
        s = socket(PF_INET, type, ppe->p_proto);
        if (s < 0)
                errexit("can't create socket: %s\n",
sys_errlist[errno]);

        /* Bind the socket */
        if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0)
                errexit("con't bind to %s port: %s\n", service,
sys_errlist[errno]);
        return s;

Quote:}

Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.