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

Calculate the voltage on the capacitor, Quetion: A 22 nF capacitor is i...

Quetion: A 22 nF capacitor is initially charged to 10V. It is then discharged by connecting a 100kW resistor across it. Approximately how long does it take for the voltage on t

Assignment, Hi Sir. I am Deep. I am studying masters of networking. I have ...

Hi Sir. I am Deep. I am studying masters of networking. I have an assignment which is due on Monday 28 August. Is this possible to make it within 5 days? And also tell me that how

What is dhcp, What is DHCP? DHCP is short for Dynamic Host Configuratio...

What is DHCP? DHCP is short for Dynamic Host Configuration Protocol. Its major task is to automatically assign an IP address to devices across the network. It first checks for

What is transmission media, Q. What is Transmission Media? - Actually s...

Q. What is Transmission Media? - Actually situated below the physical layer - Directly controlled through the physical layer - Data should be converted into electromagnet

Explain about traditional modems, Q. Explain about Traditional Modems? ...

Q. Explain about Traditional Modems? - Subsequent to modulation by the modem an analog signal reaches the telephone company switching station where it sampled and digitized to

Create and simulate a network - netml system, Create and simulate a network...

Create and simulate a network similar to the one shown here, using the NetML system. The network is a little more complex, as depicted at the right. There is an extra node

Path layer - sonet sdh , Path Layer The path  layer maps services  such...

Path Layer The path  layer maps services  such as DS3, FDDI, and ATM into the SONET  payload formal. This layer  provides  end to end  communications  signals  labelling path m

Define the class B addresses, Define the Class B Addresses? First t...

Define the Class B Addresses? First two octets are the network number as well as the last two octets are the host number 16382 probable blocks for assignment to organi

Connectionless service, In connectionless schema, there is no connection re...

In connectionless schema, there is no connection required. The source of data includes destination information in data and transmit to the network. Network transmit each data item

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