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

Show the data encryption standard, Q. Show the Data Encryption Standard? ...

Q. Show the Data Encryption Standard? Data Encryption Standard - Bit-level encryption method designed by IBM - Adopted as standard for nonmilitary and nonclassified us

Explain the bit-level encryption, Q. Explain the Bit-Level Encryption? ...

Q. Explain the Bit-Level Encryption? Bit-Level Encryption - Data is divided into blocks of bits then altered by encoding/decoding, permutation, substitution, exclusive OR

Signaling, The term signaling is used to define communication about the net...

The term signaling is used to define communication about the network, as opposed to interaction that just uses the network. A computer uses signaling with reserved VCI/VPI numbe

Routing principle - network layer and routing , Routing Principle The p...

Routing Principle The principle  criterion of  successful routing is of course correctness but it not only criterion. You might  prefer to take the most direct route ( the  one

What is subnetting, Used in IP Networks to break up larger networks into sl...

Used in IP Networks to break up larger networks into slighter subnetworks. It is used to decrease network traffic, Optimized network performance, and make simpler management i.e. t

Determine the relation between storage size and genomes, Genome4U is a scie...

Genome4U is a scientific research project at a large university in the United States. Genome4U has recently started a large-scale project to sequence the genomes of 250,000 volun

Calculate utilization percentage of the link, Q. Calculate Utilization perc...

Q. Calculate Utilization percentage of the link? - System: Stop and wait ARQ - Bandwidth: 1Mbps - Round trip for one bit: 20ms - Frame length: 1000 bits Utilizatio

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