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

What is ipv6, What is IPv6? IPv6, or Internet Protocol version 6, was d...

What is IPv6? IPv6, or Internet Protocol version 6, was developed to change IPv4. At present, IPv4 is being used to control internet traffic, butis expected to get saturated in

Describe the term - stateful implies, Describe the term - stateful implies ...

Describe the term - stateful implies The term stateful implies that the firewall is wakeful and is capable of remembering the state of each session of packet exchange across it

How is computer networks used in sales and marketing, Q. How is computer ne...

Q. How is computer networks used in sales and marketing?  Marketing and sales:   Computer networks are used extensively in both marketing and sales organizations. Marketing pr

Bit stream structure, In OSI 7 layer model, a header, or possibly a trailer...

In OSI 7 layer model, a header, or possibly a trailer, can be added to the data unit at each layerI 7 layer, but we will define a simple virtual packet which contains only 8bit dat

Process of skype is a web-based service, Skype is a web-based service which...

Skype is a web-based service which offers cheap and free phone calls. Explain step by step (in terms of networking technology) what really happens from the point when you initiate

Explain about the switches, Explain about the Switches LAN switches are...

Explain about the Switches LAN switches are an expansion of the method in LAN bridging, which controls data flow, handles transmission errors, gives physical addressing, and ma

Packets in eigrp, What are different kinds of packets in EIGRP?

What are different kinds of packets in EIGRP?

Explain the different types of physical networking, Question 1 Explain the...

Question 1 Explain the different activities carried out on E-Commerce, with appropriate examples Question 2 Explain the core components of E-Commerce in detail with appropria

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