Sockets, Computer Networking

When programming a server or a client, we have to deal with port numbers and IP addresses, but we usually do this through an abstraction called a socket. Sockets are the standard API for network programming; they are the abstraction which we use to describe a connection, i.e. a duplex communication. The typical way of creating and using sockets, in C, is the following:

// Client side ------------------------------------------------------------

struct addrinfo *addr;

int fd = socket(AF_INET, SOCK_STREAM, 0); // fd is the socket's filehandle
getaddrinfo("www.cnn.com", "http", NULL, &addr); // converts hostname to IP address
connect(fd, addr->ai_addr, addr->ai_addrlen); // connect to remote host
send(fd, send_buf, len, 0); // now send then receive data
...
recv(fd, recv_buf, len, 0);

// Server side ------------------------------------------------------------

struct addrinfo *addr, *remote_addr;
int fd = socket(AF_INET, SOCK_STREAM, 0);

// fd is the socket's filehandle getaddrinfo(NULL, "http", NULL, &addr); // convert service to port number ("ht bind(fd, addr->ai_addr, addr->ai_addrlen); // bind socket to a particular port nu listen(fd, 1); // listen for connections. Second argument is # of allowable waiting

// Now accept connection on socket fd; when a connection comes in, we'll move it to int newfd = accept(fd, remote_addr->ai_addr, &(remote_addr->ai_addrlen));

// we process the accepted connection on newfd, with recv() and send(),

// and at the same time, we'll keep using fd to accept other incoming connections

Posted Date: 3/13/2013 6:21:17 AM | Location : United States







Related Discussions:- Sockets, Assignment Help, Ask Question on Sockets, Get Answer, Expert's Help, Sockets Discussions

Write discussion on Sockets
Your posts are moderated
Related Questions
What are different kinds of packets in EIGRP?

State the statements about PPP and SLIP for WAN communications?  Ans)    a) PPP uses its Network Control Programs (NCPs) component to encapsulate many protocols.  b) SLIP

Dimensionality of Interconnection Network Dimensionality refers the arrangement of processing elements or nodes in an interconnection network. In linear network or single dimen

Q. What is Peripheral Connect Interface? PCI signify Peripheral Connect Interface. It's the revolutionary high speed expansion bus presented by Intel.  With the growing signifi

Q. Explain Stop--and--Wait automatic repeat request? 1. Numbering frames prevents the retaining off duplicate frames. 2. Numbered acknowledgement are needed in case of delay

Multi Tasking Environment Multi tasking exploits parallelism by: 1)  Concurrently using the multiple functional units 2)  Pipelining functional units are pipe line toget

The World  Wide Web HTTP The Hyper text Transfer Protocols( HTTP) is a protocols  used mainly to  access data on the world  wide  web. HTTP functions as a carbonation of FTP(


Q. Show the TCP/IP and OSI Similarities? TCP/IP and OSI Similarities - Both have Layers - Both have Application Layers - Have Comparable Transport and Network Layer

Q. Explain about Horn Antenna? - Outgoing transmissions broadcast by a stem and deflected outward - Received transmissions collect by a scooped part of the horn and deflecte