Accept returns "bad address" while running a sample program server_socket.c file

I built the attached code and ran it. I got run time error, "accept: Bad address".
Do you know why it is a bad address ? The ADDRESS was set to whatever it is from command ipconfig -a.
This program was run on SunOS server. Do I need to have root permission to run this program ? I am just a user.

gcc -o server socket_server.c -lsocket -lnsl is the command I used to build program server.


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

#define NSTRS       3           /* no. of strings  */
#define ADDRESS     ""  /* 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"

    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");

     * 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.
    len = sizeof(saun.sun_family) + strlen(saun.sun_path);

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

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

     * 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");

     * 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) {

            if (c == '\n')

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


Who is Participating?
CetusMODConnect With a Mentor Commented:
PAQed, with points refunded (125)

Community Support Moderator
"" is a really strange address for a UNIX domain socket. UNIX domain socket adresses normally looks like /dev/log or something like that, ie paths. Perhaps you have misunderstood that this code doesn't use TCP/IP?
chuehwAuthor Commented:
I have found the root cause. I need to set the address length in accept( ) call. Once I did that, the problem was gone.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.