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

Connect a router and computer with a console cable- ccna, Connect a Router ...

Connect a Router and Computer with a Console Cable Step 1 : Set up a basic physical connection. Connect the console (rollover) cable to the console port on the router. Link

Repeaters, REPEATERS:  Repeaters are needed when copper wire communica...

REPEATERS:  Repeaters are needed when copper wire communication is takes place. According to the fact that electrical signal goes weaker while travelling over copper lines. A

Explain in brief about the throughput, Explain in brief about the Throughpu...

Explain in brief about the Throughput The medium access control protocol should make as well-organized use as possible of the wireless medium to maximize capacity. Number of

IP datagram, On a LAN where are IP Datagram transported?

On a LAN where are IP Datagram transported?

Determine teh term - selection of computing infrastructure, Selection of co...

Selection of computing infrastructure The computing equipment on an Intranet must be selected in such a manner that they do not get obsolete very quickly or become too costly f

Link layer process, Each link layer process will have two connections: one ...

Each link layer process will have two connections: one "up" to the appropriate IP layer process, and one "sideways" to the link layer process on the simulated machine at the other

How to protect computer hardware by threats, How to protect computer Hardwa...

How to protect computer Hardware by threats The first component in the computer system vulnerable to attacks or threats, and most important to be protected, is the hardware.

What are called transactions, The grouped SQL statements are known as Trans...

The grouped SQL statements are known as Transactions (or) A transaction is a collection of actions embused with ACID properties.

Name the protocol which is networking middleware, Name the protocol which i...

Name the protocol which is networking middleware SOCKS is a networking middleware: a circuit-level gateway, acting as a proxy and is placed at the session layer to mediate clie

Private network, Single company or organization owns this. They are usually...

Single company or organization owns this. They are usually LAN technologies. There can be multiple LANs in a campus or building linked together. They are sometimes known Intranet.

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