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
Virtual private network The virtual  private  networks takes the  user data  encrypts them and sends them  to their  destination over  the internet. the sender  and the receiv

I Have an exam Questions and i would like it to be answered. Thanks

What is the command to copy the IOS image to a TFTP server? Ans) Copy flash tftp

1.  A computer system has a two-level memory cache hierarchy. The L1 cache has a zero hit penalty, a miss penalty of 5 ns and a hit rate of 95 percent. The L2 cache has a miss pena

What are the data units at different layers of the TCP / IP protocol suite? The data unit formed at the application layer is known as a message, at the transport layer the data

What is difference between ARP and RARP? The address resolution protocol (ARP) is used to associate the 32 bit IP address with the 48 bit physical address, used by a host or a

State about the communications network 1) The communications network may only accept blocks of data up to a specific size.   (2) Error control might be more effective with a

Q. How to connect Remote LANs? - Remote bridges acting as connecting devices to connect LANs and point-to-point networks such as leased telephone lines or ADSL lines

Categorization according to kind of instruction and data According to types of instruction and data subsequent types are identified under this classification: Scalar Pi

what is man LAN wan