yash cp
yash cp

Reputation: 193

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 ?

Upvotes: 0

Views: 712

Answers (2)

JMBise
JMBise

Reputation: 690

In c in the function accept:

csock = accept(sock, (struct sockaddr*)&csin, &recsize);
  • sock is the socket server(int)
  • csock is the socket client (int)
  • recsize is the size
  • csin is a struct with client's details
    • csin.sin_addr is the client's address
    • csin.sin_port is the client's port

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

Jay
Jay

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

Related Questions