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

Selective repeat ARQ, Selective Repeat ARQ - Processing at the receive...

Selective Repeat ARQ - Processing at the receiver additionally complex - The window size is reduced to 2(m-1) (2m/2 at most) - Both the transmitter as well as the receiv

Use of coaxial cable, Has a central core conductor covered in an insulating...

Has a central core conductor covered in an insulating sheath encased in an outer conductor of metal foil  RG numbers indicate physical specs such as thickness and type of insula

What are the disadvantages of star topology, What are the disadvantages of ...

What are the disadvantages of Star Topology? The disadvantages are considered as follows: a. Needs more cable than most topologies. b. Moderately complex to install.

Role of design in achieving reliability, ROLE OF DESIGN IN ACHIEVING RELIAB...

ROLE OF DESIGN IN ACHIEVING RELIABILITY One of the most important methods for achieving high reliability is through design.  Use reliability cannot be higher than the design re

Describe the term - license-free operation, Describe the term - License-fre...

Describe the term - License-free operation Users would prefer to buy and operate wireless LAN products without having to secure a license for the frequency band used by the LAN

State the term - netbios and imap, State the term - NetBIOS and IMAP Ne...

State the term - NetBIOS and IMAP NetBIOS: Network Basic Input Output System is an API (applications programming interface) used with other programs to transmit messages betwee

Routing in a wan, As there will be more devices there will be more traffic ...

As there will be more devices there will be more traffic of information. We can include capacity to WAN by adding more links and packet switches. Packet switches use not have compu

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