Presentation is loading. Please wait.

Presentation is loading. Please wait.

Advanced Network Programming spring 2007

Similar presentations


Presentation on theme: "Advanced Network Programming spring 2007"— Presentation transcript:

1 Advanced Network Programming spring 2007
Chapter 4 Elementary TCP Sockets

2 Contents 4.1 Introduction 4.2 “socket” Function 4.3 “connect” Function
4.4 “bind” Function 4.5 “listen” Function 4.6 “accept” Function 4.7 “fork” and “exec” Functions 4.8 Concurrent Servers 4.9 “close” Function 4.10 “getsockname” and “getpeername” Functions

3 Introduction

4 “socket” Function To perform network I/O, socket function is used to specify the type of communication protocol. For example TCP using IPv4 UDP using IPv6 Unix domain stream protocol Etc. #include <sys/socket.h> Int socket (int family, int type, int protocol); Returns: non-negative descriptor if ok, -1 on error

5 Argument - family family specifies the protocol family
This argument is often referred to as domain instead of family

6 Argument - type type specifies the socket types

7 Argument - protocol When this argument is set to 0, it selects the system’s default for the given combination of family and type

8 Combinations of family and type
Not all combinations of family and type are valid “Yes” means valid but do not have handy acronyms

9 “connect” Function The connect function is used by a TCP client to establish a connection with a TCP server #include <sys/socket.h> Int connect (int sockfd, const struct sockaddr *servaddr, socklen_t addrlen); Returns: 0 if OK, -1 on error

10 Arguments sockfd is a socket descriptor returned by the socket function *servaddr is a pointer to a socket address structure addrlen is the size of the address structure The socket address structure contains the IP address and port number of the server

11 Function’s return The function returns only when the connection is established or error occurs There are several error returns possible: If the client TCP receives no response to its SYN segment, ETIMEDOUT is returned If the server’s response SYN is a reset (RST), this indicate no process is waiting for connections, and it returns ECONNREFUSED If the client receives ICMP “destination unreachable” from intermediate router, it returns either ECHOSTUNREACH or ENETUNREACH

12 “bind” Function The bind function assigns a local protocol address to a socket The protocol address is the combination of either 32-bit IPV4 or 128-bit IPV6 address, along with a 16-bit TCP or UDP port # #include <sys/socket.h> Int bind (int sockfd, const struct sockaddr *myaddr, socklen_t addrlen); Returns: 0 if OK, -1 on error

13 Arguments sockfd is a socket descriptor returned by the socket function *myaddr is a pointer to a socket address structure addrlen is the size of the address structure With TCP, calling “bind” lets us specify a port number and IP address

14 IP Address and Port To specify IP address and port, the following values are used for sin_addr, sin_port, sin6_addr, and sin6_port With IPv4, the wildcard address is specified by the constant INADDR_ANY, whose value is normally 0

15 “listen” Function “listen” is called only by TCP server
It moves the socket from CLOSED state to the LISTEN state #include <sys/socket.h> Int listen (int sockfd, int backlog); Returns: 0 if OK, -1 on error

16 Arguments sockfd is a socket descriptor
backlog specifies the maximum number of connections the kernel should queue

17 Queues for Listening Socket
For a given listening socket, the kernel maintains two queues

18 Three-way handshake

19 “accept” Function “accept” is called by a TCP server to return the next completed connection from the completed connection queue #include <sys/socket.h> Int accept (int sockfd, struct sockaddr *cliaddr, socklen_t addrlen); Returns: non negative if OK, -1 on error

20 Arguments sockfd is a socket descriptor
cliaddr is used to return the protocol address of the connected peer process addrlen is a value-result argument: before the call, it is specified as the size of the socket address structure on return, this integer value contains the actual number of bytes stored by the kernel

21 Function’s return This function return up to three values
new socket descriptor error indication the protocol address of the client process and the size of this address If we are not interested in having the protocol address of the client return, both cliaddr and addrlen are set to null pointers

22 “fork” Function This function is the only way in Unix to create new process #include <unistd.h> pid_t fork (void); Returns: 0 in the child, process ID of the child in parent, -1 on error

23 Function’s return It is called once but return twice
In the calling process (parent) with a return value that is the process ID of the newly created process (child) It returns in the child, with a return value of 0 These two are used to differentiate between parent and child The parent’s process ID can be obtain easily by calling “getppid” function

24 Applications of “fork”
A process makes copy of itself so that one copy handle one operation while the other copy does another task A process wants to execute another program. “fork” is first called to make one copy of itself, and then one of the copy (typically child) calls exec to replace itself with the new program

25 “exec” Function The only way to execute a program in file by Unix is for an existing process to call one of the six “exec” functions “exec” replaces the current process image with the new program file, and this new program normally starts at the main function The process ID does not change

26 The six “exec”

27 Concurrent Servers To allow server to handle multiple clients at the same time The simplest way for Unix is to “fork” a child process to handle each client

28

29 “close” Function Used to close a socket and terminate TCP connection
#include <unistd.h> int close (int sockfd); Returns: 0 if OK, -1 on error

30 “getsockname” and “getpeername” Functions
These two functions return either the local protocol address associated with a socket (getsockname) or the foreign protocol address associated with a socket (getpeername) #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 returns: 0 if OK, -1 on error

31 Reasons for the two Functions
After “connect” successfully returns in a TCP client that doesn’t not call “bind”, “getsockname” returns the local IP address and port assignment to the connection by the kernel After calling bind with a port number of 0 (choose local port), “getsockname” returns the local port assinged “getsockname” can be called to obtain the address family of a socket

32 Reasons for the two Functions
In a TCP server that binds the wildcard IP address, once a connection is established with a client, the server can call “getsockname” to obtain the local IP address assigned to the connection. The socket descriptor argument is the connected socket, and not the listening socket When a server is “execed” by the process, the only way the server can obtain the identity of the client is to call “getpeername”


Download ppt "Advanced Network Programming spring 2007"

Similar presentations


Ads by Google