Reputation: 193
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 ?
Upvotes: 0
Views: 712
Reputation: 690
In c in the function accept:
csock = accept(sock, (struct sockaddr*)&csin, &recsize);
From a socket ID try this:
#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));
.
.
.
}
Upvotes: 0
Reputation: 24895
Some minimal information available with me is posted below.
I don't know much of 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.
It appears getsockname helps you in getting the ip & port to which the socket is bound to.
Upvotes: 2