![]() |
| |||||||
![]() |
| | LinkBack | Thread Tools | Display Modes |
| | #1 |
| Registered User Join Date: Mar 2008 Location: India
Posts: 72
| how to know which ip is binded to the specific port using a c/linux program? 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 |
| vlrk is offline | |
| | #2 | |
| critical genius Join Date: Jul 2008 Location: SE Queens
Posts: 5,230
| Quote:
| |
| MK27 is offline | |
| | #3 |
| Registered User Join Date: Mar 2008 Location: India
Posts: 72
| 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);
}
But when i run the netstat command it shows me a other ip as 255.255.255.255 regards |
| vlrk is offline | |
| | #4 |
| critical genius Join Date: Jul 2008 Location: SE Queens
Posts: 5,230
| 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;
}
|
| MK27 is offline | |
| | #5 |
| Senior software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 5,768
| 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.
__________________ "Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot |
| brewbuck is offline | |
| | #6 |
| Registered User Join Date: Apr 2008
Posts: 282
| Code: addrServer.sin_addr.s_addr = inet_addr(UPNP_SERVER_IP); |
| root4 is offline | |
| | #7 |
| Registered User Join Date: Aug 2008 Location: Croatia
Posts: 20
| 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); |
| dotunix is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| BOOKKEEPING PROGRAM, need help! | yabud | C Programming | 3 | 11-16-2006 11:17 PM |
| Waiting for a specific character through a port | shoobsie | C Programming | 5 | 08-25-2005 11:45 AM |
| is there a way of checking what program uses a specific port? | Xterria | A Brief History of Cprogramming.com | 6 | 05-27-2004 10:54 PM |
| Basic port scanner code .. pls help ??? | intruder | C Programming | 18 | 03-13-2003 08:47 AM |
| DOS, Serial, and Touch Screen | jon_nc17 | A Brief History of Cprogramming.com | 0 | 01-08-2003 04:59 PM |