Namespaces
Variants
Actions

Open C Sockets: getsockname method

Jump to: navigation, search
Article Metadata

Article
Created: polaka_sr (17 Jun 2008)
Last edited: hamishwillee (08 Feb 2012)
  • getsockname - gets socket name
int getsockname (int s, struct sockaddr * restrict name, socklen_t * restrict namelen);

The getsockname system call returns the current name for the specified socket. The namelen argument should be initialized to indicate the amount of space pointed to by name. On return it contains the actual size of the name returned (in bytes).

Following is the usage of getsockname function:

#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
TInt GetSockName()
{
int sock_fd;
struct sockaddr_in addr,ss;
unsigned int len;
 
sock_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
 
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(5000);
bind(sock_fd,(struct sockaddr*)&addr,sizeof(addr));
 
len=sizeof(ss);
getsockname(sock_fd,(struct sockaddr*)&ss,&len);
close(sock_fd);
}

Wiki Links

Open C Sockets Overview

This page was last modified on 8 February 2012, at 05:52.
141 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved