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
Connections in ATM are known virtual circuits or virtual channels These are known virtual, since connections are created in ATM by starting values in memory locations (tables) in A

To stop Service Advertisements (SAPs) from flooding a network, Cisco routers do not forward them. How are services advertised to other networks? Ans) Every router builds its own

Configure VTP on the Switches VTP permits the network administrator to control the instances of VLANs on the network by creating VTP domains. Within each VTP domain, one or mor

What is Unacknowledged connectionless service This service is a datagram-style service. It is a very simple service that does not involve any of the flow- and error-control mec

Question 1 Write a long note on computers in television development Question 2 Write a long note on the ‘Steadicam' Question 3 Write a long note on Internet and pr

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

State about the Packet filter firewalls They remain the most common type of firewall in use as of today. They were the earliest firewalls developed and were capable of permitt

Q. How is computer networks used in teleconferencing? Teleconferencing: Teleconferencing allows conference to happen without the participants being in the same place. Applica

PING - Packet Internet Gopher A utility that shows connections to one or more remote hosts. The ping command uses the ICMP echo request and echo reply packets to verify whether

FDDI Media Class B or SAS (single-attachment stations) attach to one ring Class A or DAS (dual attachment stations) attach to both rings. SASs is attached to the primary rin