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
The application layer This  layer enables users  to access the  network  by providing  a few  services to the user some of the protocols and services available to the  user ar

Many benefits of configuring VLAN as listed below: • Performance Enhancements: It reduces heavy load on the network by permitting operations at lower latencies by using VLAN t

Question: (a) (i) Distinguish between Physical topology and Logical topology. (ii) Explain the following statement with the help of a diagram: "A star topology can have a l

What is Sonet Synchronous Optical Network is a fiber optic technology that can transmit high-speed data audio, used for text and video. Single clock handles timing of transm

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

TCP is connection-oriented. Connection-oriented is a kind of connection established between the source & destination machines before any data is sent that means a connection is est

Network Service Model The network  service  model  defines  the characteristics of end to end  transport of data between  one edge of the  network  to the  other  that is betwe

Explain about the Switches LAN switches are an expansion of the method in LAN bridging, which controls data flow, handles transmission errors, gives physical addressing, and ma

Determine the names of Network installation ? Buying the components ? Cabling the network ? Installation of networking components such as hubs, switches, routers, gateway

100Base-T4 Utilizing four pairs of category 3 (voice grade) UTP to transmit 100 Mbps Two pairs are bidirectional and other two are unidirectional 8B/6T (eight bin