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
Describe at one disadvantage of a peer to peer network. When you are accessing the resources that are shared by one of the workstations on the network, that workstation takes

QUESTION a) The aim of a computer network is to increase efficiency and reduce costs. Explain how networks of computers achieve the above. b) Describe the star topology. Su

Most networks give dynamic connections, which last for a relatively short period. To operate this, ATM can dynamically join a switched virtual circuit (SVC), allow it last as long

Question: (a) Short Message Service (SMS) is a service available on most mobile phones that allows the sending of short messages between mobile phones, other handheld devices

An ATM network is created from interconnected ATM switches. The attachment ports or points can be connected to computers or other ATM switches. As cells reach at an ATM switch, the

What is Project 802? It is a project begins by IEEE to set standards that enable intercommunication among equipment from a variety of manufacturers. It is a way for specifying

Describe the advantages of Intranet to the organisation The most obvious advantage is that everyone with access to a computer terminal connected to the Intranet can obtain the

State how the different IP-QoS needs for controling traffic, data and voice applications can be supported in your technology choice from Q7. Describe the specific IP-QoS schemes -

State the statements about PPP and SLIP for WAN communications?  Ans)    a) PPP uses its Network Control Programs (NCPs) component to encapsulate many protocols.  b) SLIP

Hold-Down Timers - Routers ignore network update information for a number of periods.