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

Explain about wireless ethernet, Wireless Ethernet (802.11) a) Operates...

Wireless Ethernet (802.11) a) Operates on physical plus data link layers b) BSS (Basic service set) stationary or mobile wireless stations and a central base station known a

Dns tunneling – domain name server, DNS Tunneling – Domain Name Server ...

DNS Tunneling – Domain Name Server "If, on one system, it is possible to transmit bits to another in any form, and in turn receive a reply as a result of that transmission, th

Networking, What is the implication of increasing and decreasing subnet Bit...

What is the implication of increasing and decreasing subnet Bits?

How can you manage a network using a router, How can you manage a network u...

How can you manage a network using a router? Routers have built in console that lets you configure dissimilar settings, like security and data logging. You can allocate restric

Discuss your strategy to test wlpc system, Wireless Phone Company (WLPC) is...

Wireless Phone Company (WLPC) is a provider of wireless phone service in the US with 100,000 customers serving mostly small markets. WLPC has just purchased additional airwaves in

Show the drawbacks of go-back-N, Q. Show the Drawbacks of Go-back-N? D...

Q. Show the Drawbacks of Go-back-N? Drawbacks of Go-back- N - Inefficient - Every out of order received packets are discarded (receiver side is simplified) - This

What are the features of intranet, What are the features of Intranet In...

What are the features of Intranet Intranets provide access to electronic databases, documents, electronic training manuals, office circulars, internal job vacancies, etc. Any t

Show vpns security and the internet model, Q. Show VPNs Security and the In...

Q. Show VPNs Security and the Internet Model? - VPN's Security and the Internet Model - Application layer - provide for each application protocol (other layers may be left v

Administrative distance of 0 mean, Directly linked network have the lowest ...

Directly linked network have the lowest administrative distance of 0. They are considered the most reliable.

Describe encapsulating, Can you describe encapsulating, carrier and passeng...

Can you describe encapsulating, carrier and passenger protocol?

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