Sockets, Computer Networking

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

Posted Date: 3/13/2013 6:21:17 AM | Location : United States







Related Discussions:- Sockets, Assignment Help, Ask Question on Sockets, Get Answer, Expert's Help, Sockets Discussions

Write discussion on Sockets
Your posts are moderated
Related Questions
What is source route and OSPF? Source route It is a series of IP addresses identifying the route a datagram must follow. A source route might optionally be included in an

A stored procedure is a named collection of SQL statements and procedural logic that is compiled, verified and kept in a server database. It is typically treated like any other dat

What is the implication of increasing and decreasing subnet Bits?

Q. Explain about Flow Control? - Signifies to ask the transmitter to stop/resume sending in data - Required when DTE to DCE speed > DCE to DCE speed - (Exampl

Why Routine security audits are essential It is essential that regular security audits are conducted to find out physically which computer is connected and permits access to

Question 1: a. Differentiate between the several types of switching networks together with illustration. b. With the help of a diagram explain the two modes of operation un

Switch Generally called  as two  layer switch . it  performs  on physical  and data  link  layers. It  is  a bridge it has many  ports  that allow  better  performance. Since

To monitor IP igrp traffic, we can use "debug IP igrp transaction" or "debug IP igrp events". How do we show information about IPX routing update packets? Ans) Bu using debug ip

This  is one of the models based on PRAM. In this, the processors access the memory location parallel for reading while exclusively for writing operations. In the algorithm which u

Satellite Radio Channels A communication  satellite links two or more  earth  microwave transmitter receiver, know  as ground  stations. The satellite receives transmission on