Elementary TCP Sockets -Computers And Network Programming-Lecture Slides, Slides of Network Programming

This lecture was delivered by Dr. Ram Sai at Jaypee University of Engineering and Technology for Computers and Network Programming course. It includes: Network, Programming, Elementary, Tcp, Sockets, Functions, Client, Server, Protocol, Descriptor

Typology: Slides

2011/2012

Uploaded on 07/23/2012

gannesh
gannesh 🇮🇳

4.4

(12)

75 documents

1 / 23

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
NetworkProgramming
(Lecture10)
ElementaryTCPsockets
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17

Partial preview of the text

Download Elementary TCP Sockets -Computers And Network Programming-Lecture Slides and more Slides Network Programming in PDF only on Docsity!

Network

Programming

(Lecture

Elementary

TCP

sockets

Socket

functions

for

TCP

client/server

Socket

function

 There are other values for the family and type arguments. For example, 4.4BSD supports both AF_NS (the Xerox NS protocols, often called XNS) and AF_ISO (the OSI protocols).  Similarly, the type of SOCK_SEQPACKET, a sequenced ‐ packet socket, is implemented by both the Xerox NS protocols and the OSI protocols, describe its use with SCTP .  But, TCP is a byte stream protocol, and supports only SOCK_STREAM sockets.  The key socket, AF_KEY, is newer than the others. It provides support for cryptographic security. Similar to the way that a routing socket (AF_ROUTE) is an interface to the kernel's routing table, the key socket is an interface into the kernel's key table.  On success, the socket function returns a small non ‐ negative integer value, similar to a file descriptor. Call this a socket descriptor, or a sockfd.  To obtain this socket descriptor, all we have specified is a protocol family (IPv4, IPv6, or Unix) and the socket type (stream, datagram, or raw). Not yet specified either the local protocol address or the foreign protocol address. docsity.com

AF_xxx

Versus

PF_xxx

 The "AF_" prefix stands for "address family" and the "PF_" prefix stands for "protocol family."  Historically, the intent was that a single protocol family might support multiple address families and that the PF_ value was used to create the socket and the AF_ value was used in socket address structures.  But in actuality, a protocol family supporting multiple address families has never been supported and the <sys/socket.h> header defines the PF_ value for a given protocol to be equal to the AF_ value for that protocol.  While there is no guarantee that this equality between the two will always be true, should anyone change this for existing protocols, lots of existing code would break. To conform to existing coding practice, we use only the AF_ constants in this text, although you may encounter the PF_ value, mainly in calls to socket.

connect

functions

 The connect function initiates TCP's three ‐ way handshake .  The function returns only when the connection is established or an error occurs.  There are several different error returns possible  If the client TCP receives no response to its SYN segment, ETIMEDOUT is returned. 4.4BSD.  for example, sends one SYN when connect is called, another 6 seconds later, and another 24 seconds later.  If no response is received after a total of 75 seconds, the error is returned.

connect

function

 If the server's response to the client's SYN is a reset (RST), this indicates that no process is waiting for connections on the server host at the port specified (i.e., the server process is probably not running).  This is a hard error and the error ECONNREFUSED is returned to the client as soon as the RST is received.  An RST is a type of TCP segment that is sent by TCP when something is wrong.  Three conditions that generate an RST are:  when a SYN arrives for a port that has no listening server.  when TCP wants to abort an existing connection.  when TCP receives a segment for a connection that does not exist.

Possible

outputs

Sun Jul

connect error: Connection timed out  connect error: Connection refused  connect error: No route to host

bind

Function

The bind function assigns a local protocol address to a socket.  With the Internet protocols, the protocol address is the combination of either a

bit IPv address or a

bit IPv address, along with a

bit

TCP

or

UDP

port number. #include <sys/socket.h> int bind (int sockfd, const struct sockaddr *myaddr, socklen_t addrlen); Returns: 0 if OK, ‐ 1 on error docsity.com

bind

function

A

process can bind a specific

IP

address to its socket. The

IP

address must belong to an interface on the host.  For a

TCP

client, this assigns the source

IP

address that will be used for

IP

datagrams sent on the socket.  For a

TCP

server, this restricts the socket to receive incoming client connections destined only to that

IP

address

Bind

function

Normally, a

TCP

client does not bind an

IP

address to its socket.  The kernel chooses the source IP address when the socket is connected, based on the outgoing interface that is used, which in turn is based on the route required to reach the server  If a

TCP

server does not bind an

IP

address to its socket, the kernel uses the destination

IP

address of the client's

SYN

as the server's source

IP

address.

constant

INADDR_ANY

With IPv4, the wildcard address is specified by the constant

INADDR_ANY,

whose value is normally

This tells the kernel to choose the

IP

address.  struct sockaddr_in servaddr; servaddr.sin_addr.s_addr

htonl

(INADDR_ANY);

wildcard

IPv

struct sockaddr_in serv;  serv.sin6_addr

in6addr_any;

wildcard

The system allocates and initializes the in6addr_any variable to the constant IN6ADDR_ANY_INIT.  The <netinet/in.h> header contains the extern declaration for in6addr_any.

Listen function  The second argument to this function specifies the maximum number of connections the kernel should queue for this socket  An incomplete connection queue, which contains an entry for each SYN that has arrived from a client for which the server is awaiting completion of the TCP three ‐ way handshake. These sockets are in the SYN_RCVD state .  A completed connection queue, which contains an entry for each client with whom the TCP three ‐ way handshake has completed. These sockets are in the ESTABLISHED state .

Listen function  When a SYN arrives from a client, TCP creates a new entry on the incomplete queue and then responds with the second segment of the three ‐ way handshake: the server's SYN with an ACK of the client's SYN.  This entry will remain on the incomplete queue until the third segment of the three ‐ way handshake arrives (the client's ACK of the server's SYN), or until the entry times out.  (Berkeley ‐ derived implementations have a timeout of 75 seconds for these incomplete entries.) If the three ‐ way handshake completes normally, the entry moves from the incomplete queue to the end of the completed queue.  When the process calls accept, the first entry on the completed queue is returned to the process, or if the queue is empty, the process is put to sleep until an entry is placed onto the completed queue. docsity.com