简体   繁体   English

使用套接字API查找IP地址和端口号?

[英]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.

相关问题 使用命令行参数进行端口号和IP地址的套接字编程 - Socket Programming using command line arguments for port number and IP address 如何在同一C程序中侦听套接字并连接到不同的套接字(相同的IP地址但不同的端口号) - How to listen to a socket and connect to a different socket(same ip address but different port number) in a same c program 在套接字编程中查找客户端和服务器的IP地址 - Finding IP Address of Client and Server in Socket Programming 找出与 sendto function 中使用的套接字关联的 src IP 和端口 - Finding out the src IP and port associated with the socket used in sendto function Unix socket编程中如何获取本地IP地址和端口? - How to get local IP address and port in Unix socket programming? 从recvfrom获取错误的IP地址和端口号 - Getting wrong ip address and port number from recvfrom IP 地址和端口号可以一起唯一标识一个进程ID吗? - Can an IP address and a port number together uniquely identify a process ID? 需要帮助在C中获取TCP端口号和IP地址 - Need help getting TCP port number and IP address in C C socket问题——随机客户端地址和端口号 - C socket problems - random client address and port number 不能使用“ strtok”分割IP地址和端口,其中输入是Char * - Can not split the IP address and port using “strtok” where input is in Char *
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM