Thread: >: Address already in use

  1. #1
    Registered User
    Join Date
    Jul 2017
    Posts
    6

    >: Address already in use

    Code:
    #include <stdio.h>
    #include <sys/socket.h>
    #include <sys/types.h>
    #include <errno.h>
    #include <sys/un.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main()
    {
    	int sock = socket(AF_LOCAL, SOCK_STREAM, 0);
    	if(sock==-1)
    	    perror(">");
    	
        struct sockaddr_un my_addr;
        memset(&my_addr, 0, sizeof(struct sockaddr_un));
        my_addr.sun_family=AF_UNIX;
        strncpy(my_addr.sun_path, "/tmp", sizeof(my_addr.sun_path) - 1);
    	
    	if( (bind(sock, (struct sockaddr *)&my_addr, sizeof(struct sockaddr_un))) == -1)
    	   perror(">");
    }
    I did everything on this manual bind(2) - Linux manual page
    But the manual is bad because it does not show how to assign a socket to the address I need! What to do?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    1. You need to pick a path that doesn't exist.
    2. You need to pick a path that you own.
    3. You need to manually remove that path every time you exit in a non-clean way from your program. See the comment "When no longer required, the socket pathname, MY_SOCK_PATH should be deleted using unlink(2) or remove(3)" at the bottom of the example.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 08-24-2015, 07:43 AM
  2. Replies: 1
    Last Post: 11-07-2010, 11:39 PM
  3. Ip address
    By munna_dude in forum Networking/Device Communication
    Replies: 6
    Last Post: 06-27-2007, 08:26 AM
  4. Block address from word address
    By xddxogm3 in forum Tech Board
    Replies: 0
    Last Post: 04-25-2007, 09:02 PM

Tags for this Thread