Thread: how to know which ip is binded to the specific port using a c/linux program?

  1. #1
    Registered User
    Join Date
    Mar 2008
    Location
    India
    Posts
    147

    how to know which ip is binded to the specific port using a c/linux program?

    Friends,

    I have a program which creates a tcp socket and binds on to the port 5819.

    The expected behaviour is it needs to bind to 5819 using the provided ip.

    Due to some malfunction ( which i need to trace ) after bind instead of provided ip it is taking some other ip.

    when i do a netstat it shows me like below

    tcp 0 0 255.255.255.255:5819 0.0.0.0:* LISTEN


    Do i have any system call which i can use in my program where i can check like on this port which ip is being bind .

    I thought i can use netstat like executing with system command and then output to a textfile . Now parse the text file to know the result . But it is very expensive as lot of file opertaion and then parsing .

    So Any system call or any function through which i can get that info ?

    I hope iam making some sense if not please let me know to explain with more workds.

    regards

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by vlrk View Post
    Friends,

    I have a program which creates a tcp socket and binds on to the port 5819.

    The expected behaviour is it needs to bind to 5819 using the provided ip.

    Due to some malfunction ( which i need to trace ) after bind instead of provided ip it is taking some other ip.
    This only takes at most half a dozen lines of code, so why don't you post the part that includes socket(), bind(), and the relevant struct declarations? And explain how you believe you are assigning an ip address in this process.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Mar 2008
    Location
    India
    Posts
    147

    Post the code is like below

    Code:
            /* setup address info for our server socket */
    		bzero((char *)&addrServer, lenSockAddr);
    		addrServer.sin_family = AF_INET;
            addrServer.sin_addr.s_addr = inet_addr(UPNP_SERVER_IP);
            addrServer.sin_port = htons(UPNP_SERVER_PORT);
            /* create our socket */
             if ( UPNP_HTTP_NO_SOCKET == serverSocket )
             {
                 serverSocket = socket(AF_INET, SOCK_STREAM, 0);
             }
    
    if ( -1 == serverSocket) 
    {
    syslog(LOG_ERROR, "Could not create server socket");
    continue;
    }
    
            if(setsockopt(serverSocket, SOL_SOCKET, SO_REUSEADDR,(char *)&iAddrReuse, sizeof(iAddrReuse)) == (-1))
            {
              syslog(LOG_ERR, "Setsockopt failed for SO_REUSEADDR \n");
            }
    
    	/* bind address to our socket */
            if ( bindretVal == UPNP_HTTP_BIND_ERROR  )
              {
                  bindretVal = bind(serverSocket, (struct sockaddr *)&addrServer, lenSockAddr);
              }
    here while running i tried to print the ip address structure using inet_ntoa both before and after bind it shows me as the ip which i provided .

    But when i run the netstat command it shows me a other ip as 255.255.255.255

    regards

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I don't see anything wrong, but I can't claim to be much of an authority. This works for me:
    Code:
    #include <string.h>
    #include <stdio.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    
    #define IP ((in_addr_t) 0x0a000002)
    
    int main() {
    	struct sockaddr_in Addr;
    	int sock=socket(PF_INET,SOCK_STREAM,0);
    	Addr.sin_family = AF_INET;
    	Addr.sin_port = htons(5819);
    	Addr.sin_addr.s_addr = htonl(IP);
    	if (bind(sock,(struct sockaddr*)&Addr,sizeof(Addr))<0) puts("NOPE");
    	getchar();         /* a pause so we can check netstat */
    	return 0;
    }
    However, if I use anything but 10.0.0.2 (my real address) for IP, bind fails.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Maybe this is a dumb question, but are you actually sure that UPNP_SERVER_IP isn't 255.255.255.255 ?

    It seems... well, really weird, to have an IP address hardcoded like that, unless it happened to be 255.255.255.255.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    Code:
    addrServer.sin_addr.s_addr = inet_addr(UPNP_SERVER_IP);
    inet_addr() returns the address in the host encoding, converting the result with htonl() may help.

  7. #7
    Registered User
    Join Date
    Aug 2008
    Location
    Croatia
    Posts
    36
    yeah, you need to convert from little endian byte order to a network byte order by using htonl().

    Code:
    addrServer.sin_addr.s_addr = htonl(INADDR_LOOPBACK);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  2. Waiting for a specific character through a port
    By shoobsie in forum C Programming
    Replies: 5
    Last Post: 08-25-2005, 11:45 AM
  3. is there a way of checking what program uses a specific port?
    By Xterria in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 05-27-2004, 10:54 PM
  4. Basic port scanner code .. pls help ???
    By intruder in forum C Programming
    Replies: 18
    Last Post: 03-13-2003, 08:47 AM
  5. DOS, Serial, and Touch Screen
    By jon_nc17 in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 01-08-2003, 04:59 PM