Sockets, Computer Networking

Assignment Help:

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


Related Discussions:- Sockets

Example of go back - N protocol, Q. Example of go back - N protocol? Th...

Q. Example of go back - N protocol? The window size is 4. A sender has sent frame 6 as well as the timer expires for frame 3 (frame 3 not ACKn'd). The sender goes back as we

Http request message, HTTP Request Message The first  line of  an HTTP...

HTTP Request Message The first  line of  an HTTP request message  is called the  line. The subsequent lines are called  the header lines. The  request  line has  fields the me

Determine the functioning of service support, Determine the functioning of ...

Determine the functioning of Service Support Service Support must be considered a strong point for hardware, especially for servers and should be available readily. Many brande

Network comp. architecture, Write discussion on Associative mapping - compu...

Write discussion on Associative mapping - computer architecture

Show VLAN membership and configuration, Q. Show VLAN Membership and Configu...

Q. Show VLAN Membership and Configuration? VLAN Membership - May be classified by - Switch port numbers - MAC addresses - IP addresses - IP multicast addresse

State about transcontinental ?ber, State about transcontinental ?ber A ...

State about transcontinental ?ber A transcontinental ?ber link may have many gigabits/sec of the bandwidth, but latency will also be high due to speed of light propagation abov

What do you mean by platform independence, What do you mean by platform ind...

What do you mean by platform independence? Platform independence means that we can write and compile the java code in one platform (eg Windows) and can implement the class in a

Describe flow and error control mechanisms, Q. Describe Flow and Error Cont...

Q. Describe Flow and Error Control Mechanisms? Flow and Error Control Mechanisms - Stop and Wait ARQ - Go-Back ARQ - Selective Repeat ARQ

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd