Elementary TCP Socket Functions-Network Programming-Lecture Slides, Slides of Network Programming

This lecture was delivered by Dr. Mstan Veer at Gautam Buddha University for Network Programming course. It includes: Elementary, TCP, Socket, Functions, Protocol, Argument, Connect, Bind, Listen, Concurrent, Server

Typology: Slides

2011/2012

Uploaded on 07/06/2012

umi
umi 🇮🇳

4.5

(13)

63 documents

1 / 19

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Elementary TCP Socket
functions
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13

Partial preview of the text

Download Elementary TCP Socket Functions-Network Programming-Lecture Slides and more Slides Network Programming in PDF only on Docsity!

Elementary TCP Socket

functions

Socket functions

TCP Server

TCP Client

socket()

bind()

listen()

accept()

read()

write()

close()

socket()

connect()

write()

read()

close()

Well known port

Blocks until connection from client

3WHS

read()

Data

Data

End of Data Indication

Connect function

#include <sys/socket.h>

int connect(int sockfd, const struct sockaddr *servaddr,

socklen_t addrlen);

Returns:0 if OK, -1 on error

==>this function used by a TCP client

bind function

#include <sys/socket.h>

int bind (int sockfd, const struct sockaddr *myaddr, socklen_t addrlen); Returns: 0 if OK, -1 on error

==>this function assigns a local protocol address to a socket.

listen function

#include <sys/socket.h>

int listen(int sockfd, int backlog);

Returns:0 if OK, -1 on error

==>This function is called only by a TCP server

  • backlog =>specify the maximum number of connections that the kernel should queue for this socket.
  • If the queues are full when client SYN arrives, TCP server ignore the SYN, it does not send RST.

accept function

#include <sys/socket.h>

int accept(int sockfd, struct sockaddr *cliaddr,

socklen_t *addrlen);

Returns:nonnegative descriptor if OK, -1 on error

=> return the next completed connection from the

front of the completed connection queue.

If queue is empty, the process is put to sleep.

Concurrent server

pid_t pid

int listenfd, connfd;

listenfd = Socket(...);

//fill in sockaddr_in{} with server’s well-known port

Bind(listenfd, LISTENQ);

for(;;){

connfd = Accept(listenfd, ...); doit(connfd); //process the request Close(); //done with this client exit(0); //child terminate } Close(connfd); // parent close connected socket

}

Outline for typical concurrent server

Close function

#include <unistd.h>

int close(int sockfd);

returns:0 if OK, -1 on error

getsockname and getpeername

function

#include<sys/socket.h>

int getsockname(int sockfd, struct sockaddr *localaddr,

socklen_t *addrlen);

int getpeername(int sockfd, struct sockaddr *peeraddr,

socklen_t *addrlen);

both return : 0 if OK, -1 on error

=>getsockname : return local address associated with a socket

getpeername : foreign protocol address associated with a socket

#include "unp.h"

int sockfd_to_family(int sockfd) { union { struct sockaddr sa; char data[MAXSOCKADDR]; } un; socklen_t len;

len = MAXSOCKADDR; if (getsockname(sockfd, (SA *) un.data, &len) < 0) return(-1); return(un.sa.sa_family); }

Return the address family of a socket