NP-UNIT1_Introduction to Transport Layer

Download as pdf or txt
Download as pdf or txt
You are on page 1of 62

Network Programming

UNIT-1
•Introduction: Introduction, Client/server communication, OSI
Model, BSD Networking history, Test Networks and Hosts, Unix
Standards, 64-bit architectures.
• Transport Layer: TCP, UDP and SCTP, TCP Connection Establishment
and Termination.
• Self learning topics: TCP/IP Protocols in nut shell.
Network programming involves writing programs to
communicate with processes either on the same or on
other machines on the network using standard Protocols…
High-level decision must be made as to which program would initiate
the communication first and when responses are expected…

WebServer program waits for clients to send request and only after the
request is received it responds with a reply…
Single Server – Serving multiple Clients

https://www.youtube.com/watch?v=1z0ULvg_pW8
OSI - Model
TCP/IP Model
BSD
BSD
BSD History
BSD Network

Potentially Unwanted Applications Versatile Message Transaction Protocol


Test Network and Hosts
Test Network and Hosts
Communication over LAN
Communication over WAN
Discovering Network Topology – netstat –ni
and netstat -r
Options Meaning
-a All ( TCP, UDP, SCTP, ICMP )
-n Numeric addresses
-b Display executables
-o Process id
-f Fully Qualified domain name
-p proto Specific protocols
-r Routing table
-s Protocol statistics
-t Current connection network status
Unix Standard - POSIX

Posix.1c : Threads and Extensions


Unix Standard - POSIX
Unix APIs
64 Bit Architectures
64 Bit Architectures
Sockets : An end point for communication between processes
across the network
Sockets : An end point for communication between processes
across the network
Sockets : An end point for communication between processes
across the network

struct in_addr { unsigned


long s_addr;
};
Day Time Server at port no. 13
Example ::

Day Time Client….


}

#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
Source Code of Day Time Client
#include <netdb.h>
#include <stdio.h>

int main(int argc, char **argv)


{
int sockfd, n = 0;
char recvline[1000 + 1];
struct sockaddr_in servaddr;
int port = 13; AF_APPLETALK Apple Computer Inc. Appletalk network

if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {


err(1, "Socket Error");
} AF_INET Internet domain

bzero(&servaddr, sizeof(servaddr)); AF_PUP Xerox Corporation PUP internet


servaddr.sin_family = AF_INET;
Unix file system
servaddr.sin_port = htons(port); AF_UNIX
inet_pton(AF_INET,argv[1],&servAddress.sin_addr);
if (connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0) { 127.0.0.1 -- > b'\x7f\x00\x00\x01'
err(1, "Connect Error");
while ((n = read(sockfd, recvline, 1000)) > 0) {
recvline[n] = 0;
fputs(recvline, stdout);
}

return 0;
}
while ((n = read(sockfd, recvline, 1000)) > 0) {
recvline[n] = 0;
fputs(recvline, stdout);
}

return 0;
}

struct in_addr {

unsigned long s_addr;

};
DayTime Server…

#include <sys/socket.h>

#include <sys/types.h>

#include <netinet/in.h>

#include <netdb.h>

#include <stdio.h>

int main(int argc, char **argv)

int listenfd, connfd;

int port = atoi(argv[1]);


struct sockaddr_in servaddr;

char buff[1000];

time_t ticks;
CREATE A SOCKET
listenfd = socket(AF_INET, SOCK_STREAM, 0);

bzero(&servaddr, sizeof(servaddr));
Initialize Socket Address
servaddr.sin_family = AF_INET;

servaddr.sin_addr.s_addr = htonl(INADDR_ANY);

servaddr.sin_port = htons(port);
bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr)); Bind the SOCKET

listen(listenfd, 8);
Listen on the Port for connections
for (;;) {

connfd = accept(listenfd, (struct sockaddr *) NULL, NULL); Accept connection request from Client

ticks = time(NULL);

snprintf(buff, sizeof(buff), "%.24s\r\n", ctime(&ticks)); Write to socket… { Serve the Client }

write(connfd, buff, strlen(buff));

close(connfd);

}
}
int main()
{ Source Code of Day Time Client – IPV6
int s;
struct sockaddr_in6 addr;

s = socket(AF_INET6, SOCK_STREAM, 0);


addr.sin6_family = AF_INET6;
addr.sin6_port = htons(5000);
inet_pton(AF_INET6, "::1", &addr.sin6_addr);
connect(s, (struct sockaddr *)&addr, sizeof(addr));

while ((n = read(sockfd, recvline, 1000)) > 0) {


recvline[n] = 0;
fputs(recvline, stdout);
}

close(sockfd);
return 0;
}
}

Error Handling and Wrapper functions


In any real-world program, it is essential to check every function call for an
error return we check for errors from socket, inet_pton, connect, read, and fputs, and
when one occurs, we call our own functions, err_quit and err_sys, to print an error
message and terminate the program.

We find that most of the time, this is what we want to do. Occasionally, we want to do
something other than terminate when one of these functions returns an error

int Socket(int family, int type, int protocol)

{ int n;
if ( (n = socket(family, type, protocol)) < 0)
err_sys("socket error");
return (n);
}
}

TCP AND UDP


}

TCP/IP – The Big-picture


TCP Connection Establishment and Termination
Three-Way Handshake
• The following scenario occurs when a TCP connection is established:

1. The server must be prepared to accept an incoming connection. This is normally done
by calling socket, bind, and listen and is called a passive open.

2. The client issues an active open by calling connect. This causes the client TCP
to send a "synchronize" (SYN) segment, which tells the server the client's
initial sequence number for the data that the client will send on the connection.
Normally, there is no data sent with the SYN; it just contains an IP header, a
TCP header, and possible TCP options

3. The server must acknowledge (ACK) the client's SYN and the server must also send
its own SYN containing the initial sequence number for the data that the server will
send on the connection. The server sends its SYN and the ACK of the client's SYN in a
single segment.

4. The client must acknowledge the server's SYN.


• The minimum number of packets required for this exchange is three;
hence, this is called TCP's three-way handshake.
• TCP Options
• Each SYN can contain TCP options. Commonly used options include the
following:
• MSS option. With this option, the TCP sending the SYN announces its
maximum segment size

• Window scale option. The maximum window that either TCP can
advertise to the other TCP is 65,535

• Timestamp option. This option is needed for high-speed connections to


prevent possible data corruption caused by old, delayed, or duplicated
segments..
TCP Connection Termination
• While it takes three segments to establish a connection, it takes four to
terminate a connection.
1. One application calls close first, and we say that this end performs the active
close. This end's TCP sends a FIN segment, which means it is finished sending
data.

2. The other end that receives the FIN performs the passive close. The received
FIN is acknowledged by TCP. The receipt of the FIN is also passed to the
application as an end-of-file (after any data that may have already been
queued for the application to receive), since the receipt of the FIN means the
application will not receive any additional data on the connection.

3. Sometime later, the application that received the end-of-file will close its
socket. This causes its TCP to send a FIN.

4. The TCP on the system that receives this final FIN (the end that did the active
close) acknowledges the FIN.
TCP State Transition Diagram
• The operation of TCP with regard to connection establishment and
connection termination can be specified with a state transition diagram.

• There are 11 different states defined for a connection and the rules of TCP
dictate the transitions from one state to another, based on the current
state and the segment received in that state.

• For example, if an application performs an active open in the CLOSED state,


TCP sends a SYN and the new state is SYN_SENT.

• If TCP next receives a SYN with an ACK, it sends an ACK and the new state is
ESTABLISHED. This final state is where most data transfer occurs.
• The two arrows leading from the ESTABLISHED state deal with the
termination of a connection.

• If an application calls close before receiving a FIN (an active close),


the transition is to the FIN_WAIT_1 state.

• But if an application receives a FIN while in the ESTABLISHED state (a


passive close), the transition is to the CLOSE_WAIT state.
TCP-Connection state diagram
TCP – Connection : Packet Exchange
}

TCP - Connection
TCP – Connection : Packet Exchange
TCP-Connection state diagram
}

TCP Use cases


}

UDP
}

UDP Use cases


SCTP
}

SCTP – 4 way Handshake


}

SCTP – Closing
}
SCTP – State Transition Diagram
}
SCTP – Packet Exchange
}

SCTP – 4 way handshake


Comparison
Review Questions
1. What is network programming? With neat diagram ,Explain the Client
and Server Communication over LAN and WAN
2. Write a program to implement TCP daytime client
3. Write a program to implement TCP daytime client for IPV6
4. Explain the Error Handling using Wrapper functions
5. Write a program to implement TCP daytime Server
6. Explain the layers in the OSI model and Internet Protocol suite
7. Write a brief note on BSD Networking History
8. Write a short note on various UNIX standards
9. With the neat diagram give the overview of TCP/IP Protocol
10. Write a short note on i) TCP ii) UDP iii) SCTP protocols
11. Explain in detail, TCP Connection Establishment and Termination
12. Explain TCP state Transition Diagram
13. With neat diagram, explain Packet Exchange for TCP connection
14. Explain Sockaddr_in structure and its parts.
15. Give the comparison on TCP, UDP and SCTP protocols

You might also like