[英]Details of socket using socket ID
This question may sound basic one . 这个问题可能听起来很基本。 Are there any functions in c or Java where I can get the socket details like , port , address , buffer size using only socket identifier ?
在c或Java中是否有任何函数可以使用套接字标识符获取套接字详细信息,如端口,地址,缓冲区大小?
Some minimal information available with me is posted below. 我可以获得的一些最小信息发布在下面。
I don't know much of Java. 我对Java知之甚少。 But as far as 'C' is concerned, you can use the getsockopt function to get the buffer sizes (send buffer and recv buffer) of the socket.
但就“C”而言,您可以使用getsockopt函数来获取套接字的缓冲区大小(发送缓冲区和recv缓冲区)。
It appears getsockname helps you in getting the ip & port to which the socket is bound to. 似乎getsockname可以帮助您获取套接字绑定的ip和端口。
In c in the function accept: 在c函数中接受:
csock = accept(sock, (struct sockaddr*)&csin, &recsize);
From a socket ID try this: 从套接字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.