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

Design the user interface for mode switching, Design the user interface for...

Design the user interface for mode switching and data entry any way that you like. For example, to switch an application from transmit mode to receive mode, you can give it special

Determine the major advantages of ring topology, Point out the major advant...

Point out the major advantages of Ring Topology. The benefits of ring topologies are: a. They are very simple to troubleshoot because every device incorporates a repeater.

LAN, categories of LAN

categories of LAN

How is computer networks used in financial services, Q. How is computer net...

Q. How is computer networks used in Financial services? Financial services:   Today's financial services are totally depended on networks. Application includes credit history

Show the ip address representation, Q. Show the IP Address Representation? ...

Q. Show the IP Address Representation? IP Addressing IP Address Representation Binary notation - IP address is displayed as 32 bits Dotted-decimal notation -

Describe a typical dns system, Question: (a) With the help of a diagra...

Question: (a) With the help of a diagram, describe a typical DNS system. (b) Using signal sequence or state transition diagrams, illustrate the following scenarios: i.

What do you mean by protocol, What do you mean by Protocol? In the cont...

What do you mean by Protocol? In the context of data networking, a protocol is a formal set of rules and conventions that governs how computers exchange information over a netw

Substantial capital investments in new networks, Question: (a) How can...

Question: (a) How can governments provide the mobile industry with the long-term clarity and certainty it needs to make substantial capital investments in new networks? (b)

Encoding the data type, ENCODING THE DATA TYPE: The figure explain a f...

ENCODING THE DATA TYPE: The figure explain a frame in which the data kind is specified by using the data area. To ensure interoperability format of encoding location m

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