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
Question - 1 Define MIS and its objectives. What are the characteristics of MIS? Question - 2 Explain data processing Question - 3 What are the recent developments in database

The application for this assignment is somewhat arti?cial. Normally, an application would not interact directly with IP. Instead, it would create a UDP or TCP socket and interact w

So how packets do determines their way across the Internet? Does each computer linked to the Internet know where the other computers are? Do packets just get 'broadcast' to every c

Give the Description of security tools Software are coming up in which the user can select the kind of security measure required to be taken for encryption. One such example is

Determine the method to protect the Web servers The company should install additional levels of security measures since it is possible for hackers to generate and send data wit

It imposes hierarchy and a division of labor between processors. Only one designated processor, the master, controls (in a tightly coupled arrangement) slave processors dedicated t

PROTOCOL SOFTWARE AND ETHERNET WIRING:  All wiring methods use identical Ethernet specifications. e.g. they need same message format. They need same CSMA/CD algorithems. They m

I have a project on Information Communication Technology(I.C.T). So sir I want to know that what is internet? and how it works?

Suppose that frames are 1250 bytes long including 25 bytes of overhead. Also assume that ACK frames are 25 bytes long. Calculate the efficiency of stop-and-wait ARQ (a) Transmits a

The connection-oriented service process for networking is same to the manner in which telephones are used. This is shown as follows: A caller dials a number of the receiver. The