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 is WAN? A wide area network is defines a LAN of LANs. WANs connect LANs that might be on opposite sides of a building, across the country or around the world.  WANS

CPU bus It is connect with various components of the system and  transformation of data between components.   Power Supply It is separate  external part of cisco rou

One of the most popular models for designing parallel algorithms is PRAM (Parallel Random Access Machine). A PRAM consists of unbounded number of processors interrelating with each

Classification according to level of processing According to this categorization computer operations are classified like arithmetic operations and instruction execution. Subseq


Q. Describe about Data Communications? - Data - Information presented in whatsoever form is agreed upon by the parties creating and using the data - Data Communications - ex

A.) JPEG and PICT B.) MPEG and MIDI C.) ASCII and EBCDIC For example, the Presentation layer would be liable for changing from EDCDIC to ASCII. Data compression, decompres

Switches operate at layer 2. They enhance bandwidth by decreasing the number of devices sharing the media. They isolate collisions. Like a bridge they forward traffic based upon la


Network Infrastructure: This project will require replacement of major networking components throughout the office. Virtualization will result in increased speed and reliability fo