Solved

Server demo program returns "accept: Bad address"

Posted on 2004-03-29
5
673 Views
Last Modified: 2007-12-19
Hi

I built one server demo program on SunOS and got the run time error as I ran it.
server: accept: Bad address

The ADDRESS is set to the machine's IP address. I don't know why I got that error, indicating server can't accept a connection on its won IP. server is the name of this demo program.
Thanks

-chuehw


Here is the source code.
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <stdio.h>

#define NSTRS       3           /* no. of strings  */
#define ADDRESS     "146.x.y.z"  /* addr to connect */

/*
 * Strings we send to the client.
 */
char *strs[NSTRS] = {
    "This is the first string from the server.\n",
    "This is the second string from the server.\n",
    "This is the third string from the server.\n"
};

main()
{
    char c;
    FILE *fp;
    int fromlen;
    register int i, s, ns, len;
    struct sockaddr_un saun, fsaun;

    /*
     * Get a socket to work with.  This socket will
     * be in the UNIX domain, and will be a
     * stream socket.
     */
    if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
        perror("server: socket");
        exit(1);
    }

    /*
     * Create the address we will be binding to.
     */
    saun.sun_family = AF_UNIX;
    strcpy(saun.sun_path, ADDRESS);

    /*
     * Try to bind the address to the socket.  We
     * unlink the name first so that the bind won't
     * fail.
     *
     * The third argument indicates the "length" of
     * the structure, not just the length of the
     * socket name.
     */
    unlink(ADDRESS);
    len = sizeof(saun.sun_family) + strlen(saun.sun_path);

    if (bind(s, &saun, len) < 0) {
        perror("server: bind");
        exit(1);
    }

    /*
     * Listen on the socket.
     */
    if (listen(s, 5) < 0) {
        perror("server: listen");
        exit(1);
    }

    /*
     * Accept connections.  When we accept one, ns
     * will be connected to the client.  fsaun will
     * contain the address of the client.
     */
    if ((ns = accept(s, &fsaun, &fromlen)) < 0) {
        perror("server: accept");
        exit(1);
    }

    /*
     * We'll use stdio for reading the socket.
     */
    fp = fdopen(ns, "r");

    /*
     * First we send some strings to the client.
     */
    for (i = 0; i < NSTRS; i++)
        send(ns, strs[i], strlen(strs[i]), 0);

    /*
     * Then we read some strings from the client and
     * print them out.
     */
    for (i = 0; i < NSTRS; i++) {
        while ((c = fgetc(fp)) != EOF) {
            putchar(c);

            if (c == '\n')
                break;
        }
    }

    /*
     * We can simply use close() to terminate the
     * connection, since we're done with both sides.
     */
    close(s);

    exit(0);
}

0
Comment
Question by:chuehw
  • 2
  • 2
5 Comments
 
LVL 12

Expert Comment

by:stefan73
ID: 10705189
Hi chuehw,
Looks like you have a problem in the call sequence of your app.

Check this tutorial:
http://www.ecst.csuchico.edu/~beej/guide/net/



Cheers,
Stefan
0
 

Author Comment

by:chuehw
ID: 10705408
stefan73,
I found the root cause. But I don't know why.
I need to set the var fromlen to global variable in order to let accept being able to accept a connection. When fromlen was declared as local var to main( ), accept() returns "Bad address". This is verified. But I have no clue why it behaves like that.

-chuehw

 if ((ns = accept(s, &fsaun, &fromlen)) < 0) {
        perror("server: accept");
        exit(1);
    }
0
 

Author Comment

by:chuehw
ID: 10705461
OK. I think I need to initialize fromlen to 0. So when I declare fromlen global, it was set to 0 by default ?

I did another test. I declare fromlen local to main( ) but this time I initialize it to 0 and accept( ) works this time.
Two sets of solutions:
1. declare fromlen to global. Don't care to initialize it to 0 or not.
2. declare fromlen to local but need to initialize it to 0.

-chuehw
0
 
LVL 44

Accepted Solution

by:
Karl Heinz Kremer earned 125 total points
ID: 10709480
The "addrlen" parameter (what you call fromlen) needs to be set to the size of the struct the second parameter points to (in your case sockaddr_un). So before you call accept(), you should do the following:

    fromlen = sizeof(sockaddr_un);

You can find this information in the man page (man 2 access), but also in the tutorial that stefan73 provided a link to.
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 10709485
BTW: I don't know why it works with fromlen set to 0, but this is not the way this function is supposed to be used.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now