收到的UDP包怎么知道发送人的IP地址?
收到的UDP包怎么知道发送人的IP地址? 问题点数:0、回复次数:5Top
1 楼walkonthesky(★★★★★)回复于 2004-08-04 11:30:04 得分 0
不可能知道
IP是三层的概念
UDP是四层的概念
三层数据对四层透明Top
2 楼datuhao(峡谷)回复于 2004-08-04 11:37:05 得分 0
UDP层不知道,但是我们可以知道。
socket编程中recvfrom函数可以获得远端的ip地址。Top
3 楼cryptonym(想裸睡)回复于 2004-08-04 16:39:31 得分 0
int recvfrom(
SOCKET s,
char* buf,
int len,
int flags,
struct sockaddr* from,
int* fromlen
);
Parameters
s
[in] Descriptor identifying a bound socket.
buf
[out] Buffer for the incoming data.
len
[in] Length of buf, in bytes.
flags
[in] Indicator specifying the way in which the call is made.
from
[out] Optional pointer to a buffer in a SOCKADDR structure that will hold the source address upon return.
fromlen
[in, out] Optional pointer to the size, in bytes, of the from buffer.
Top
4 楼Pandona(口袋妖怪)回复于 2004-08-05 12:44:47 得分 0
int recvfrom(
SOCKET s,
char* buf,
int len,
int flags,
struct sockaddr* from, //这个参数里就是发送人的地址结构
int* fromlen
);
如果想得到发送方的ip:"192.168.0.100"可用char *inet_ntoa (struct in_addr in);
如: char ip[16];
int port;
strcpy(ip,inet_ntoa (from->sin_addr)); //from即为上面recvfrom里的参数
port=ntohs(from->sin_port); //转换成主机字节端口
Top
5 楼greatzhum(纯净水)回复于 2004-08-05 13:52:22 得分 0
楼上正解.Top




