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. Thanks for all your past and future contributions.

Open C Sockets: listen method

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

The listen() method listens for connections on a socket.

  int listen (int s, int backlog);

The s argument is a socket and the backlog argument defines the maximum length the queue of pending connections may grow to. The real maximum queue length will be 1.5 times more than the value specified in the backlog argument.

Following is the usage of listen function:

#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
void listen_example()
int sock_fd;
int newsock_fd;
struct sockaddr_in addr;
struct sockaddr_in ss;
struct sockaddr_in new_socket;
unsigned int len;
unsigned int addr_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));

Wiki Links

Open C Sockets Overview

This page was last modified on 27 July 2012, at 05:23.
37 page views in the last 30 days.