2012-06-01 53 views
0

这个问题听起来可能是基本的问题。有没有在c或Java中的任何函数,我可以使用套接字标识符来获取套接字详细信息,如端口,地址,缓冲区大小?插座使用插座的详细信息

+1

嗯,是的,有。您可能需要考虑两件事:首先,一次查找一种语言的答案。其次,这很容易谷歌 - 你发布这个问题之前,你尝试了什么? – thejh

回答

2

我的一些最基本的信息发布在下面。

我不太了解Java。但就'C'而言,您可以使用getsockopt函数获取套接字的缓冲区大小(发送缓冲区和接收缓冲区)。

看起来getsockname可以帮助您获取套接字绑定到的端口ip &。

0

在函数C接受:

csock = accept(sock, (struct sockaddr*)&csin, &recsize); 
  • 袜子是插座服务器(INT)
  • csock是插座客户端(INT)
  • recsize来是大小
  • csin是一个结构与客户的详细信息
    • csin.sin_addr是客户的地址
    • csin.sin_port是客户端的端口

从插座ID试试这个:

#include <sys/types.h> 
#include <sys/socket.h> 
#include <netinet/in.h> 
#include <arpa/inet.h> 
#include <stdio.h> 

f() 
{ 
    int s; 
    struct sockaddr_in sa; 
    int sa_len; 
    . 
    . 
    . 
     /* We must put the length in a variable.    */ 
    sa_len = sizeof(sa); 
     /* Ask getsockname to fill in this socket's local  */ 
     /* address.           */ 
    if (getsockname(s, &sa, &sa_len) == -1) { 
     perror("getsockname() failed"); 
     return -1; 
    } 

    /* Print it. The IP address is often zero beacuase */ 
    /* sockets are seldom bound to a specific local  */ 
    /* interface.           */ 
    printf("Local IP address is: %s\n", inet_ntoa(sa.sin_add r)); 
    printf("Local port is: %d\n", (int) ntohs(sa.sin_port)); 
    . 
    . 
    . 
} 
+0

这并没有回答这个问题 - 这只能用于在你接受它时获取有关套接字的详细信息,而不是以后的任何时刻,只有它的fd。 – thejh

+0

你可以试试我的新代码 – JMBise