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
Normal 0 false false false EN-IN X-NONE X-NONE Networks A networks  consists of two  or m

Describe the term - stateful implies The term stateful implies that the firewall is wakeful and is capable of remembering the state of each session of packet exchange across it

Networks are more frequently being prepared to allow specification of the quality of service needed by users. For example: - a typical voice telephone call may give a QoS para

Sonet Frame Transaction Transmitted one subsequent to another without any gap in between. First 2 bytes - alignment bytes. F628 in Hex. - define the beginning of every

Computer Networking 1. Explain different network structures in use. 2. Elaborate the architecture and usage of ISDN. 3. Discuss the concept of framing in Data Link Layer

Control Frame: claim_token Consider first station turned on Station notices no tokens Sends claim_token No competitors, so makes a ring of just itself P

Question : (a) Describe, with the aid of a diagram, a hybrid topology of a network of your choice. (b) Briefly explain the similarity and differences between a switch and

Question 1 Describe any five different types of computers Question 2 Explain Bus Type LAN. Its advantages and disadvantages and examples Question 3 Explain the s

What is virtual channel? Virtual channel is normally a connection from one source to one destination, although multicast connections are also allowed. The other name for virtua

- EIA/ TIA specifies and RJ-45 connector for unshielded twisted-pair (UTP) cable. The letter RJ stand for registered jack and the number 45 refers to a specific wiring sequence.