Sockets, Computer Networking

Assignment Help:

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


Related Discussions:- Sockets

Data mining, The following DNA sequences are extracted from promoter region...

The following DNA sequences are extracted from promoter region of genes which are co-regulated by the same transcription factor (TF). The nucleotide segments capitalized in the giv

Clock-driven scheme , Assume that the clock-driven scheme for generating st...

Assume that the clock-driven scheme for generating starting sequence numbers is used with a 16-bit wide clock counter (that is, sequence numbers range from 0 to 2^16).  The clock t

What is an error, Q. What is an Error? At any time bits flow from one p...

Q. What is an Error? At any time bits flow from one point to another they are subject to unpredictable changes because of interference The interference is able to change the

Estimate of average delay deviation, Consider the procedure described for e...

Consider the procedure described for estimating average delay di. Suppose that u=0.1. Let r1-t1 be the most recent sample delay, let     r2 -t2 be the next most recent sample delay

Fat tree, Fat tree It is a modified version of the tree network. In thi...

Fat tree It is a modified version of the tree network. In this group the bandwidth of edge (or the connecting wire among nodes) increases towards the root. It is a more practic

What is the full form of FDM and TDM, What is the full form of FDM and TDM ...

What is the full form of FDM and TDM It is both. Each of the 100 channels is possess its own frequency band (FDM), and on every channel two logical streams are intermixed by th

Illustrate TCP – bytes and segments, Q. Illustrate TCP – bytes and segments...

Q. Illustrate TCP – bytes and segments? TCP - bytes & segments TCP at the sending site collects bytes into a packet called a segment TCP adds a header to each se

Framing techniques of datalink layer, java program to implement framing tec...

java program to implement framing techniques of datalink layer

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd