




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of the transport layer in computer networks, focusing on tcp and udp socket programming. It covers the creation and closing of sockets, specifying local addresses, connecting sockets to destinations, and sending and receiving data. The document also discusses byte ordering and includes functions for converting byte order. Students can use this document as study notes, summaries, or cheat sheets for understanding transport layer concepts.
Typology: Study notes
1 / 8
This page cannot be seen from the preview
Don't miss anything!





process
TCP with buffers, variables
socket
host or server
process
TCP with buffers, variables
socket
host or server
internet
listen()
accept()
read()
bind()
Block until connection from client
Process requests
write()
read()
close()
socket()
write()
connect()
read()
close()
well-known port
Connection establishment TCP three-way handshake
Data (request)
Data (reply)
End-of-file notification
Implicit bind
socket( )
recvfrom ( )
bind ( )
socket ( )
sendto ( )
result = socket(pf, type, protocol)
close(socket)
bind(socket, localaddr, addrlen)
Address family (2) Protocol port
struct sockaddr_in { short sin_family; /* must be AF_INET / u_short sin_port; / protocol port, can ignore / struct in_addr sin_addr; / IP address / char sin_zero[8]; / Not used, must be zero / }; struct in_addr { in_addr_t s_addr; / 32 bit ipv4 address, network byte ordered */ };
connect(socket, destaddr, addrlen)
listen(socket, qlength)
Return anew socket with its destination connected to
newsock=accept(socket,addr,addrlen)
Original socket
Newly returned socket
Newly returned socket
Original socket
accept
accept
write(socket,buffer,length)
writev(socket,iovector,vectorlen)
send(socket,message,length,flags)
sendto(socket,message,length,flags, Destaddr, addrlen)
sendmsg(socket,messagestruct,flags)
#include <netinet.h> uint16_t htons(uint16_t host16bitvalue ) Converts a 16-bit integer from host to network byte order
uint32_t htonl(uint32_t host32bitvalue ) Converts a 32-bit integer from host to network byte order
Both return: value in network byte order
uint16_t ntohs(uint16_t net16bitvalue) uint32_t ntohl(uint32_t net32bitvalue)
Both return: value in host byte order
#include <strings.h> /* Berkeley-derived functions / void bzero(void _dest_ , size_t nbytes ) Set the first part of an object to null bytes
void bcopy(const void _src_* , void _dest_* , size_t nbytes ); int bcmp(const void _ptr1_* , const void _ptr2_* , size_t nbytes ) /* return:0 if equal, nonzero if unequal */
#include <string.h> /* ANSI C defined functions */ void memset(void _dest_ ,int c ,size_t len ) Sets the first len bytes in memory dest to the value of c
void memcpy(void _dest_ ,const void _src_* , size_t nbytes ) void memcmp(const void _ptr1_* , const void _ptr2_* , size_t nbytes )
#include <arpa/inet.h>
int inet_aton(const char *strptr, struct in_addr addrptr); / return 1 if string was valid,0 error */ Convert an IP address in string format (x.x.x.x) to the 32-bit packed binary format used in low-level network functions
in_addr_t inet_addr(const char strptr); / return 32-bit binary network byte ordered IPv address; INADDR_NONE if error, deprecated and replaced by inet_aton() */
char inet_ntoa(struct in_addr inaddr); / returns: pointer to dotted-decimal string */
/* A simple server in the internet domain using TCP */ #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h>
int main(int argc, char *argv[]) { int sockfd, newsockfd, portno, clilen; char buffer[256]; struct sockaddr_in serv_addr, cli_addr; int n;
sockfd = socket (PF_INET, SOCK_STREAM, 0);
bzero((char *) &serv_addr,sizeof(serv_addr)); portno = atoi(argv[1]); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(portno); if ( bind (sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) error("ERROR on binding");
listen (sockfd,5); clilen = sizeof(cli_addr); newsockfd = accept (sockfd, (struct sockaddr *) &cli_addr, &clilen); bzero(buffer,256); n = read (newsockfd,buffer,255); printf("Here is the message: %s\n",buffer); n = write (newsockfd,"I got your msg",18); return 0; }
/* Client program */ #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h>
int main(int argc, char *argv[]) { int sockfd, portno, n; struct sockaddr_in serv_addr; *struct hostent server; char buffer[256];
portno = atoi(argv[2]);
sockfd = socket (PF_INET, SOCK_STREAM, 0); server = gethostbyname (argv[1]); bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length); serv_addr.sin_port = htons(portno);
if ( connect (sockfd,&serv_addr, sizeof(serv_addr)) < 0) error("ERROR connecting"); printf("Please enter the message: "); bzero(buffer,256); fgets(buffer,255,stdin); n = write (sockfd,buffer,strlen(buffer));
bzero(buffer,256); n = read (sockfd,buffer,255); printf("%s\n",buffer);
return 0; }