Thread: bind() error

  1. #1
    Registered User PutoAmo's Avatar
    Join Date
    Mar 2002
    Posts
    72

    bind() error

    Why do I keep getting bind() error when attempting to execute this server?
    By the way, it's meant to get to integers, calculate sum and return sum to client.
    Any help appreciated !

    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <sys/un.h>
    #include <string.h>
    
    int main (void)
    {
      int sd, new_sd, sum;
      struct sockaddr_un serv_addr;
    
      struct mystruct
      {
        int addend;
        int augend;
      } values;
    
      void report_fatal_error (char *message);
    
      sd = socket (AF_UNIX, SOCK_STREAM, 0);
    
      if (sd == -1)
        report_fatal_error ("Server: Cannot open socket");
    
      bzero ((char *) &serv_addr, sizeof (serv_addr));
    
      serv_addr.sun_family = AF_UNIX;
    
      strcpy (serv_addr.sun_path, "/tmp/mysocket");
    
      if (bind (sd, (struct sockaddr *) &serv_addr, sizeof (serv_addr)) == -1)
        report_fatal_error ("Server: Binding error in local address");  /* Here is where I get the error */
    
      listen (sd, 5);
    
      while (1)
      {
        new_sd = accept (sd, NULL, 0);
    
        if (new_sd == -1)
          report_fatal_error ("Server: Accept error");
    
        read (new_sd, &values, sizeof (values));
    
        sum = values.addend + values.augend;
    
        printf ("server: %d + %d = %d\n", values.addend, values.augend, sum);
    
        write (new_sd, &sum, sizeof (sum));
    
        close (new_sd);
      }
    }
    
    void report_fatal_error (char *message)
    {
      perror (message);
    
      exit (1);
    }
    PS. Program is not written by me.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    What port are you binding on?

    What error message is printed by perror()?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User PutoAmo's Avatar
    Join Date
    Mar 2002
    Posts
    72
    Port? I thought AF_UNIX domain didn't use any ports.

    Error from perror: "Address is already in use". As if there was another socket using that address. (??)
    Last edited by PutoAmo; 05-23-2002 at 12:51 PM.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    If your app is not running, try removing this file.
    >"/tmp/mysocket"

    this is what your prog binds to I believe (not a port, as you pointed out).

    Also, you can add this to your server if you want to remove it each time the app starts:

    >unlink("/tmp/mysocket");
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User PutoAmo's Avatar
    Join Date
    Mar 2002
    Posts
    72

    Thumbs up Thx

    Removing file "/tmp/mysocket" really helped.
    Thank you, Hammer.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: Thx

    Originally posted by PutoAmo
    Removing file "/tmp/mysocket" really helped.
    Thank you, Hammer.
    Unless you change your program, this file doesn't get deleted automatically....... just thought I should highlight that.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM