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

What is a web server, This new model of Client/Server having of thin, porta...

This new model of Client/Server having of thin, portable, "universal" clients those talks to super fat servers. In the easiest form, a web server returns documents when clients ask

Analyze snort or pcap data from a public wifi hotspot, The project idea is ...

The project idea is : Analyze Snort or PCAP data from a public WIFI hotspot (describe traffice seen and what it is, protocols, ports, etc). This is what is expected. Initially r

What is domains in active directory, In Windows 2000, a domain describes bo...

In Windows 2000, a domain describes both an administrative boundary and a security boundary for a collection of objects that are relevant to a particular group of users on a networ

What is icmp, What is ICMP? ICMP is Internet Control Message Protocol. ...

What is ICMP? ICMP is Internet Control Message Protocol. It gives messaging and communication for protocols within the TCP/IP stacks. This is also the protocol that manages err

Define post office and lightweight directory access protocol, Post Office P...

Post Office Protocol and Lightweight Directory Access Protocol POP: Post Office Protocol is a used by mail clients to download messages from a mail server on the Internet. L

Show the seven layers of iso''s osi model, Q. Show the seven layers of ISO'...

Q. Show the seven layers of ISO's OSI model? Ans:- The seven layers are given below:   -  Application Layer -  Presentation Layer -  Session Layer -  Transport Laye

Determine about the tree topology, Determine about the Tree Topology A ...

Determine about the Tree Topology A generalization of the bus topology. The transmission medium is a branching cable with no closed loops. The tree layout begins at a point kno

Protocol software and ethernet wiring, PROTOCOL SOFTWARE AND ETHERNET WIRIN...

PROTOCOL SOFTWARE AND ETHERNET WIRING:  All wiring methods use identical Ethernet specifications. e.g. they need same message format. They need same CSMA/CD algorithems. They m

Advantage of ip sec tunnelling mode, Advantage of IP sec  Tunnelling Mode ...

Advantage of IP sec  Tunnelling Mode IP Sec  tunnelling mode  has the  following  advantages: a.Security : Ts IP sec tunnelling mode  secure somewhat but  not the best. In a

Name the strategies devised by ietf, Question: a) Show the shortest for...

Question: a) Show the shortest form of the following IPv6 address: 2340:0000:0000:000F:7000:119A:A001:0000 b) The IPv6 header includes a Class field and a Flow ID field. Bri

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