免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 20407 | 回复: 6

如何根据一个socket的fd获取如下信息? [复制链接]

论坛徽章:
0
发表于 2004-05-12 14:50 |显示全部楼层
程序中监听了两个端口,这样我可能有很多fd描述符,有时我也不太确实某个fd是哪一个监听socket创建的,请问有没有办法根据一个fd来判断它的客户端是连到自身的哪个端口上的
比如
我的程序监听9901和9902端口,accept生成了5~100这么多的socket的描述符,如果我知道60是个合法的描述符,请问有没有办法判断出这个60哪个监听socket创建的呢?也就是说根据60,我能否判断出是9901,还是9902,
败了,我自己都说不清楚了,大家知道我要问什么了吗   
谢谢,谢谢

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
发表于 2004-05-12 14:57 |显示全部楼层

如何根据一个socket的fd获取如下信息?

应该可以的。

Perl 里边就可以。

论坛徽章:
0
发表于 2004-05-12 14:58 |显示全部楼层

如何根据一个socket的fd获取如下信息?

应当用哪个函数呢?

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
发表于 2004-05-12 15:03 |显示全部楼层

如何根据一个socket的fd获取如下信息?

  1. GETSOCKNAME(2)             Linux Programmer's Manual            GETSOCKNAME(2)

  2. NAME
  3.        getsockname - get socket name

  4. SYNOPSIS
  5.        #include <sys/socket.h>;

  6.        int getsockname(int s, struct sockaddr *name, socklen_t *namelen);

  7. DESCRIPTION
  8.        Getsockname  returns  the  current  name for the specified socket.  The
  9.        namelen parameter should be initialized to indicate the amount of space
  10.        pointed  to by name.  On return it contains the actual size of the name
  11.        returned (in bytes).

  12. RETURN VALUE
  13.        On success, zero is returned.  On error, -1 is returned, and  errno  is
  14.        set appropriately.

  15. ERRORS
  16.        EBADF  The argument s is not a valid descriptor.

  17.        ENOTSOCK
  18.               The argument s is a file, not a socket.

  19.        ENOBUFS
  20.               Insufficient  resources  were available in the system to perform
  21.               the operation.

  22.        EFAULT The name parameter points to memory not in a valid part  of  the
  23.               process address space.

  24. CONFORMING TO
  25.        SVr4,  4.4BSD (the getsockname function call appeared in 4.2BSD).  SVr4
  26.        documents additional ENOMEM and ENOSR error codes.

  27. NOTE
  28.        The third argument of getsockname is in reality an `int *' (and this is
  29.        what  BSD 4.* and libc4 and libc5 have).  Some POSIX confusion resulted
  30.        in the present socklen_t.  The draft standard has not been adopted yet,
  31.        but  glibc2  already  follows  it  and  also  has  socklen_t.  See also
  32.        accept(2).

  33. SEE ALSO
  34.        bind(2), socket(2)

  35. BSD Man Page                      1993-07-24                    GETSOCKNAME(2)
复制代码

论坛徽章:
0
发表于 2004-05-12 15:22 |显示全部楼层

如何根据一个socket的fd获取如下信息?

getpeername Gets the name of the peer socket.
getsockname The getsockname retrieves the locally bound address of the specified socket.

论坛徽章:
0
发表于 2004-05-12 15:43 |显示全部楼层

如何根据一个socket的fd获取如下信息?

唉,原来没调用ntohs,所以看到的不一样
一直以为getsockname,getpeername有问题呢

多谢二位

论坛徽章:
0
发表于 2004-05-12 18:11 |显示全部楼层

如何根据一个socket的fd获取如下信息?

  1. #if defined(HPPLATFORM)
  2.    #define SOCKLENPTRCAST   
  3.    #define SOCKLEN      int
  4.    #define SOCKLENPTR
  5. #elif defined(SCOPLATFORM)
  6.    #define SOCKLENPTRCAST
  7.    #define SOCKLEN      int
  8.    #define SOCKLENPTR

  9. #elif defined(LINUXPLATFORM)
  10.    #define  SOCKLENPTRCAST (socklen_t *)
  11.    #define SOCKLEN      socklen_t
  12.    #define SOCKLENPTR   socklen_t *
  13. #endif
  14. int         CSocket::PeerPort ( int sock )
  15. {
  16.         int len = sizeof(struct sockaddr_in);
  17.         struct sockaddr_in sin;
  18.        
  19.         if ( sock < 0 )
  20.                 return -1;
  21.                
  22.         if ( getpeername ( sock, (sockaddr*)&sin, SOCKLENPTRCAST&len ) == 0 )
  23.                 return ( ntohs ( sin.sin_port ) );
  24.        
  25.         return -1;
  26. }       

  27. int         CSocket::LocalPort ( int sock )
  28. {
  29.         int len = sizeof(struct sockaddr_in);
  30.         struct sockaddr_in sin;
  31.        
  32.         if ( getsockname ( sock, (sockaddr*)&sin, SOCKLENPTRCAST&len ) == 0 )
  33.                 return ( ntohs ( sin.sin_port ) );
  34.                
  35.         return -1;
  36. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP