[英]Finding IP address and port number using socket api?
I am working on a code where I have need to find the sender's IP address,sender's port no and application layer packet sent by it. 我正在编写一个代码,需要查找发件人的IP地址,发件人的端口号以及由其发送的应用层数据包。
I did it using Raw Sockets (SOCK_RAW), it runs fine and did the job for me but its too much clumsy. 我是使用Raw Sockets(SOCK_RAW)来完成的,它运行良好并且为我完成了工作,但是它太笨拙了。
Question: I can use SOCK_STREAM and get the application layer packet directly and that's really clean and simple method than using Raw socket but this way I won't be able to find the IP address and port number of the client. 问:我可以使用SOCK_STREAM并直接获取应用程序层数据包,与使用Raw套接字相比,这是一种非常干净简单的方法,但是通过这种方式,我将无法找到客户端的IP地址和端口号。 Is there any function to which I pass the socket descriptor and it tells me the IP address and port number of the other end.
是否有我传递套接字描述符的函数,它告诉我另一端的IP地址和端口号。 I read a few man pages but I could not find any.
我读了几本手册页,但找不到。
I am using linux socket api in C language. 我正在使用C语言的Linux套接字API。
Thanks in advance. 提前致谢。 :)
:)
当您调用accept()
接受传入连接时, *address
是一种结构,其中填充有发送者的IP地址和端口号。
the BSD socket implementation defines a function named getpeername()
which allows to know the ip address and the port of the remote side of a tcp socket. BSD套接字实现定义了一个名为
getpeername()
的函数,该函数允许知道tcp套接字的ip地址和远程端的端口。
when you have any SOCK_STREAM connected socket, no matter which side first established the connection, you can call this function to get the informations you need. 当您有任何SOCK_STREAM连接的套接字时,无论首先建立连接的是哪一侧,都可以调用此函数来获取所需的信息。 (this is far easier than a raw socket).
(这比原始套接字要容易得多)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.