I've opened a TCP socket server (I've omitted a few stuff, it is taken from here
sockfd = socket(p->ai_family, p->ai_socktype,
p->ai_protocol))
Is it possible to get the IP address of the server from sockfd? If not where should I look?
EDIT: I want to know the address of the server (this is before any client connects).
OK assuming you are using IPV4 then do the following: struct sockaddr_in* pV4Addr = (struct sockaddr_in*)&client_addr; struct in_addr ipAddr = pV4Addr->sin_addr; If you then want the ip address as a string then do the following: char str[INET_ADDRSTRLEN]; inet_ntop( AF_INET, &ipAddr, str, INET_ADDRSTRLEN );
Use lsof -p pid . You need to grep on the output to get the port values for eg. something like this - lsof -p pid| grep TCP. This will list all the ports opened or connected to by the process.
There is no difference between a socket (descriptor) and a file descriptor(s). A socket is just a special form of a file. For example, you can use the syscalls used on file descriptors, read() and write(), on socket descriptors.
First, import the socket module and then get the h_name using the socket.gethostname (). Now, find the IP address by passing the h_name as an argument to the socket.gethostbyname () and store it in a variable. Print the IP address.
You can't use the socket to get the server's address before a client connects, because it isn't known. In principle, a host may have multiple IPs. The IP used for a connection to a server is the one belonging to the interface, through which the connection arrived. Until a connection arrives, it isn't known.
The IP used for a connection to a server is the one belonging to the interface, through which the connection arrived. Until a connection arrives, it isn't known. Even if you have only one IP, connections may arrive from within the machine, in which case the address would be 127.0.0.1. So the listening socket has no information about the IP.
The IP used for a connection to a server is the one belonging to the interface, through which the connection arrived. Until a connection arrives, it isn't known. Even if you have only one IP, connections may arrive from within the machine, in which case the address would be 127.0.0.1.
If you want to know who's at the other end of your socket you can use getpeername in Linux. getsockname will tell you who you are. You decide what address you want your server to sit on initially though, at bind time.
You may also find this SO question useful: bind socket to network interface
And the book "Unix Network Programming, vol 1", by W. Richard Stevens.
You can't use the socket to get the server's address before a client connects, because it isn't known.
In principle, a host may have multiple IPs. The IP used for a connection to a server is the one belonging to the interface, through which the connection arrived. Until a connection arrives, it isn't known.
Even if you have only one IP, connections may arrive from within the machine, in which case the address would be 127.0.0.1
.
So the listening socket has no information about the IP.
You'll need to find what interfaces the machine has, and what's their IPs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With