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

Intradomain and interdomain routing protocols?, Describe the concept of int...

Describe the concept of intradomain and interdomain routing protocols?

Introduction-pram algorithms, One of the most popular models for designing ...

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

What is segmentation and fragmentation, To split data in pieces is known as...

To split data in pieces is known as segmentation and divide segmentation in pieces called Fragmentation. #question..

What is a transaction server, What is a Transaction server? With a tran...

What is a Transaction server? With a transaction server, the client includes remote procedures that reside on the server with an SQL database engine. These remote processes on

Explain process management and transaction management, TP Monitor does main...

TP Monitor does mainly two things extremely well. They are Process management and Transaction management? They were originally introduced to run classes of applications that co

What is project 802, What is Project 802? It is a project begins by IEE...

What is Project 802? It is a project begins by IEEE to set standards that enable intercommunication among equipment from a variety of manufacturers. It is a way for specifying

State ethernet frame format, Ethernet Frame Format Consists of sev...

Ethernet Frame Format Consists of seven fields There is No mechanism for acknowledging received frames considered an unreliable medium

Identify the purpose of the dlci, DLCI stands for Data Link Connection Iden...

DLCI stands for Data Link Connection Identifier. It identifies the local PVC.

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