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

Determine the computing infrastructure, Determine the computing infrastruct...

Determine the computing infrastructure One solution for the protection of the computing infrastructure is to use digital certificate-based solutions. Users can be given access

Program to load the router with the supplied scripts-ccna, Load the Router ...

Load the Router with the Supplied Scripts Enable ! config term ! hostname Router1 ! enable secret class ! no ip domain-lookup !  interface FastEth

Name the connecting devices of bridges, Connecting Devices of bridges -...

Connecting Devices of bridges - Five kinds: - Repeaters - Hubs - Bridges - Two- and three-layer switches - Repeaters and hubs - layer one of Internet model -

Define metropolitan area network, Q. Define Metropolitan Area Network? ...

Q. Define Metropolitan Area Network? Metropolitan Area Network (MAN) Cable Network Architecture: Overview Protocols and Standards - Set of regulations that go

Explain physical layer of osi reference model, Q. Explain physical layer of...

Q. Explain physical layer of Osi reference model? - The physical layer deals among the physical characteristics of the transmission medium. - It defines the mechanical, elec

What is parabolic dish antenna, Q. What is Parabolic Dish Antenna? - In...

Q. What is Parabolic Dish Antenna? - Incoming signals means that the Signal bounces off of dish and is directed to focus - Outgoing signals signifies transmission is broadca

State about the bus topology, State about the Bus Topology Comments ...

State about the Bus Topology Comments 1 - Requires less cabling than a star network 2 - If there is a central cable problem entire netwo. rk goes down 3 - Less e

What is difference among rip and ripv2, RIP It is Broadcast There i...

RIP It is Broadcast There is no authentication It Support FLSM   RIPV2 It is Multicast There is Authentication It Support VLSM

Define interconnection network, Interconnection Network An interconnect...

Interconnection Network An interconnection network is designed for transferring data between two processors in a Multi-stage network. Memory bottleneck is a fundamental limitat

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