Presentation is loading. Please wait.

Presentation is loading. Please wait.

Network Programming CSC- 341

Similar presentations


Presentation on theme: "Network Programming CSC- 341"— Presentation transcript:

1 Network Programming CSC- 341
Instructor: Junaid Tariq, Lecturer, Department of Computer Science

2 14 Lecture Network Programming

3 Part 2 Elementary TCP Sockets Chapter 4

4 Socket functions socket() bind() listen() socket() accept() connect()
TCP Server socket() Well known port bind() TCP Client listen() socket() accept() 3WHS connect() Blocks until connection from client read() write() Data write() read() Data read() close() End of Data Indication close()

5 #include <sys/socket.h>
int socket(int family, int type, int protocol); returns: nonnegative descriptor if OK, -1 on error =>Normally the protocol argument to the socket function is set to 0 except for raw socket.

6

7 CONNECT FUNCTION

8 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. This function initiates TCP’s three-way hand-shake. This function returns only when the connection is established or an error occurs.

9 Connect function Return error ETIMEDOUT : no response from server
Send SYN when connect is called, another after 6 and 24 seconds, return error if no response after 75 seconds ECONNREFUSED: RST (reset) : server process is not running Hard error. Three conditions that generates an RST: When a SYN arrives for a port that has no listening server. When TCP wants to abort an existing connections. When TCP receives a segment for a connection that does not exist. EHOSTUNREACH : client’s SYN elicits destination unreachable from some intermediate router. Soft error Client’s kernel saves the message and keeps sending SYN and sends error message if no response is received after 75 seconds.

10 BIND FUNCTION

11 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. Protocol address is combination of either a 32 bit IPv4 or 128bit IPv6 address, along with 16 bit TCP or UDP port number When TCP call bind, specify a port number, an IP address, both or neither. Server bind well-known port when they start. If server does not do this, kernel chooses an ephemeral port when either connect or listen is called.

12 bind function cont. 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. 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.

13 bind function cont. With IPv4, the wildcard address is specified by the constant INADDR_ANY, whose value is normally 0. This tells the kernel to choose the IP address. we cannot use this technique with IPv6, since the 128-bit IPv6 address is stored in a structure. The value of INADDR_ANY (0) is the same in either network or host byte order, so the use of htonl is not really required. But, since all the INADDR_constants defined by the <netinet/in.h> header are defined in host byte order, we should use htonl with any of these constants

14 bind function cont. If we tell the kernel to choose an ephemeral port number, notice that bind does not return the chosen value. To obtain the value of the ephemeral port assigned by the kernel, we must call getsockname to return the protocol address.


Download ppt "Network Programming CSC- 341"

Similar presentations


Ads by Google