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

Discuss on basic services in gsm, Question 1 Explain the process of Compon...

Question 1 Explain the process of Components Identification and call establishment Question 2 Discuss on Basic services in GSM Question 3 Explain the Protocols Use

Explain the concept of token passing, Token Passing S...

Token Passing Station is able to send only when it receives a special frame called a token Token circulates around the ring If station wishes to sen

What are the categories of traditional ethernet, Categories of traditional ...

Categories of traditional Ethernet a)  Baseband - digital signals using Manchester encoding b) 10Base2, 10-Base-T, 10Base5, 10Base-FL c)  First number signifies data rate

Gateway - networl layer and routing , Gateway A gateway  is an internet...

Gateway A gateway  is an internetwork  device which  joins different  network  protocols  together. It woods on all  seven  layers of OSI model. Gateways are also known as  pro

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

What is meant by middleware, What is meant by Middleware? Middleware is...

What is meant by Middleware? Middleware is a distributed software needed to support interaction between clients and servers. In short, it is the software that is in the middle

Define co-axial cable, Define Co-axial cable. A solid central conducto...

Define Co-axial cable. A solid central conductor surrounded by insulating material and then by a cylindrical shield woven from fine wires is called as co-axial cable. The shie

Advantages of vpns - point to point , Advantages of VPNs Advantages di...

Advantages of VPNs Advantages discussed in the following  section include those related to security. Consolidation transparency cost. And administration. a. Improved secur

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