[英]Details of socket using socket ID
這個問題可能聽起來很基本。 在c或Java中是否有任何函數可以使用套接字標識符獲取套接字詳細信息,如端口,地址,緩沖區大小?
我可以獲得的一些最小信息發布在下面。
我對Java知之甚少。 但就“C”而言,您可以使用getsockopt函數來獲取套接字的緩沖區大小(發送緩沖區和recv緩沖區)。
似乎getsockname可以幫助您獲取套接字綁定的ip和端口。
在c函數中接受:
csock = accept(sock, (struct sockaddr*)&csin, &recsize);
從套接字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));
.
.
.
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.