Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Open C Sockets: bind method

From Wiki
Jump to: navigation, search
Article Metadata
Article
Created: polaka_sr (17 Jun 2008)
Last edited: hamishwillee (27 Jul 2012)

The bind method binds a name to a socket.

int bind (int s, const struct sockaddr *addr, socklen_t addrlen);

The bind system call assigns the local protocol address to a socket. When a socket is created with socket it exists in an address family space but has no protocol address assigned. The bind system call requests that addr be assigned to the socket.

Following is the usage of bind 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));
close(sock_fd);
}

Note: For maximum portability, you should always zero the socket address structure before populating it and passing it to bind.

Wiki Links

Open C Sockets Overview

This page was last modified on 27 July 2012, at 04:18.
61 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×