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("", "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 (a) Concerning agents that reduce workload, there are four agents developed using a learning approach among which is the News Filtering Agent. Name the other three age

What is meant by Middleware? Middleware is a distributed software needed to support interaction between clients and servers. In short, it is the software that is in the middle

Hardware difficulty It refers to the price of hardware logic like connectors, wires, switches, arbiter etc. that are required for execution of interconnection network.

Q. Explain about Multiplicative Decrease ? Multiplicative Decrease (MD) if a time-out take places the threshold is set to one maximum segment size (TCP Tahoe, TCP Reno).

Q. Describe about Data Communications? - Data - Information presented in whatsoever form is agreed upon by the parties creating and using the data - Data Communications - ex

Question : (a) Describe how ‘prototyping' is important to RAD. (b) List main features of ‘prototyping'. (c) List main principles of DSDM. (d) Describe the DSDM life-

Hyper  Text Transfer Protocols ( HTTP) The Hyper text  protocols ( HTTP) is at  the heart  of the  web. If a browser  developer follows  the rules  of the HTTP, the  browser w

In FLSM subnet mask of all subnets will b same. But in FLSM it changes.

Q. How can data to be exchanged between Networks? Internetwork Links in an internetwork

Importace of Clientsin intranet server The most interesting thing is that almost every computer can be connected as a client to the Intranet server. The client could be based