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 are the basic lan topologies, What are the basic LAN topologies? ...

What are the basic LAN topologies? The three simple LAN topologies that are combined to shape any practical topology are called as basic LAN topologies. They are, Bus Topology

Router igrp 71 network 10.0.0.0 router igrp 109 , What does the following s...

What does the following sequence of commands accomplish? router igrp 71 network 10.0.0.0 router igrp 109 network 172.68.7.0 Ans) It isolates network 10.0.0.0 and 172.68.7.0 and

Install, how to install xp via network

how to install xp via network

Authentication in ppp - point to point , Authentication in PPP PPP can ...

Authentication in PPP PPP can support  authentication operations at the  beginning of a connection. In some  cases reauthentication is also  possible  during  a session.  Endpo

System deadlock in shared programme structures, System Deadlock A dead...

System Deadlock A deadlock refers to the situation when simultaneous processes are holding resources and preventing each other from completing their implementation. The fol

Encoding process, Encoding Process c= uG u: binary da...

Encoding Process c= uG u: binary data sequence of length 4( input) G: Generator matrix which is 7*4 c: Codeword     D

What is the virtual path, Along any transmission path from a given source t...

Along any transmission path from a given source to a given destination, a group of virtual circuits can be grouped together into what is known as path.

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