Download presentation
Presentation is loading. Please wait.
1
Protocol Programs that communicate across a network must agree on a protocol on how they will communicate High-level decisions must be made on which program is expected to initiate communications and when responses are expected
2
Web Server A web server is typically a long-running program called a daemon Web server sends network messages only in response to requests from the network.
3
Web Client The other side of the protocol is a Web client or browser. The browser initiates communication with the Web server.
4
Client/Server Model Client/Server model is used for most network communications If the client initiates all requests, the protocol is simplified – we will use this type of protocol More complex applications use asynchronous callback communication, where the server initiates a message to the client
5
Application Protocol Client Application Protocol Server
6
Communications Relationships Clients normally communicate with one server at a time Although, a browser could access several server web sites over a period of time Server regularly communicates with several clients at a time
7
Client/Server Relationship Client Server
8
Communications Paths Application communicates with TCP TCP communicates with IP IP communicates with datalink layer of some sort Communication goes down the stack on one side and up on the other Client and server are typically user processes
9
TCP Comm on the Same Ethernet User Process Web Client Application Protocol Web Server Application Layer Protocol TCP TCP Protocol TCP Transport Layer Stack Within Kernel IP IP Protocol IP Network Layer Ethernet Ethernet Protocol Ethernet Datalink Layer Actual Client/Server Flow Ethernet LAN
10
Connection-Oriented vs Connectionless Protocols Transmission Control Protocol (TCP) – connection oriented User Datagram Protocol (UDP) – Connectionless protocol
11
IP Protocol in use since early 1980s is IP version 4 (IPv4). A new version IP version 6 (IPv6) was developed mid 1990s This text covers network applications using IPv4 and IPv6
12
LANs Connected with WAN client application server application host with TCP/IP host with TCP/IP LAN router WAN router
13
Daytime Client IPv4 – Top #include "unp.h" int main(int argc, char **argv) { int sockfd, n; char recvline[MAXLINE + 1]; struct sockaddr_in servaddr; if (argc != 2) err_quit("usage: a.out "); if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) err_sys("socket error"); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(13); /* daytime server */ if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0) err_quit("inet_pton error for %s", argv[1]);
14
Daytime Client IPv4 – Bottom if (connect(sockfd, (SA *) &servaddr, sizeof(servaddr)) < 0) err_sys("connect error"); while ( (n = read(sockfd, recvline, MAXLINE)) > 0) { recvline[n] = 0; /* null terminate */ if (fputs(recvline, stdout) == EOF) err_sys("fputs error"); } if (n < 0) err_sys("read error"); exit(0);
15
Socket Function Returns a file descriptor AF_INET indicates the file descriptor can communicate with any internet IP – AF_UNIX indicates the file descriptor can communicate within a single UNIX system such as Chico’s ect-unix system. SOCK_STREAM indicates connection protocol like TCP – SOCK_DGRAM indicates connectionless protocol like UDP
16
Convention if ( (sockfd = socket(AF_INET, SOCK_STREAM,0)) < 0) The space in between parentheses at the beginning of socket call indicates that both the return of the socket FD and testing of FD for error are performed in the if statement condition Instead of: sockfd = socket(AF_INET, SOCK_STREAM,0); if (socketfd < 0)
17
Connect Function Establishes connection with server Parameters are: –File descriptor returned by client sock function –A structure of type sockaddr_in –The size of the sockaddr_in structure Connect associates File descriptor with communications port
18
struct sockaddr_in Fields initially set to 0 by bzero Fields are then set with information about how to communicate with server –sin_family = AF_INET –sin_port = htons(13) –inet_pton sets sin_addr to argv[1]
19
Reading Data and Printing the Result Notice that read and fputs are in a while loop. Some socket systems will return all data in one read call and others will return data one byte at a time. While loop handles both cases.
20
Daytime Client IPv6 - Top #include "unp.h" int main(int argc, char **argv) { int sockfd, n; char recvline[MAXLINE + 1]; struct sockaddr_in6 servaddr; if (argc != 2) err_quit("usage: a.out "); if ( (sockfd = socket(AF_INET6, SOCK_STREAM, 0)) < 0) err_sys("socket error"); bzero(&servaddr, sizeof(servaddr)); servaddr.sin6_family = AF_INET6; servaddr.sin6_port = htons(13); /* daytime server */ if (inet_pton(AF_INET6, argv[1], &servaddr.sin6_addr) <= 0) err_quit("inet_pton error for %s", argv[1]);
21
Daytime Client IPv6 - Bottom if (connect(sockfd, (SA *) &servaddr, sizeof(servaddr)) < 0) err_sys("connect error"); while ( (n = read(sockfd, recvline, MAXLINE)) > 0) { recvline[n] = 0; /* null terminate */ if (fputs(recvline, stdout) == EOF) err_sys("fputs error"); } if (n < 0) err_sys("read error"); exit(0);
22
Error Conditions Important to check every function for error For example, we check socket, inet_pton, connect, read, and fputs Our functions err_quit and err_sys print an error message and terminate the program
23
Wrapper Function int Socket (int family, int type, int protocol) { int n; if ( (n = socket (family, type, protocol)) < 0) err_sys(“socket error”); return (n); }
24
errno Most functions involving processes return –1 on error and set a global variable called errno If the function does not return –1, errno is undefined Errno values are always uppercase and always begin with E, such as ETIMEDOUT Errno values are normally defined in When we say something like, “the connect function returns ECONNREFUSED” we mean that connect returns –1 and sets errno to ECONNREFUSED.
25
More Wrappers int n; if ( (n = pthread_mutex_lock(&ndone_mutex)) != 0) errno = n, err_sys(“pthread_mutex_lock error”); or void Pthread_mutex_lock(pthread_mutex_t *mptr) { int n; if ( (n = pthread_mutex_lock(mptr)) == 0) errno = n; err_sys(“pthread_mutex_lock error”); }
26
Daytime Server IPv4 (Top) #include "unp.h" #include int main(int argc, char **argv) { int listenfd, connfd; struct sockaddr_in servaddr; char buff[MAXLINE]; time_t ticks; listenfd = Socket(AF_INET, SOCK_STREAM, 0); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(13); /* daytime server */ Bind(listenfd, (SA *) &servaddr, sizeof(servaddr)); Listen(listenfd, LISTENQ);
27
Daytime Server IPv4 (Bottom) for ( ; ; ) { connfd = Accept(listenfd, (SA *) NULL, NULL); ticks = time(NULL); snprintf(buff, sizeof(buff), "%.24s\r\n", ctime(&ticks)); Write(connfd, buff, strlen(buff)); Close(connfd); }
28
Socket Function Identical to client code Return value is a file descriptor for server communication
29
sockaddr_in Structure AF_INET indicates communication is over internet INADDR_ANY indicates that the server can receive from any IP address htons(13) indicates server receives over port 13
30
Bind Bind has socket FD and sockaddr_in structure as parameters It uses them to associate the socket FD with a port number
31
Listen Sets the queue size for the number of client requests The name “Listen” leads you to believe that it is a function that listens for client requests, but that is what the Accept function does
32
Accept TCP uses what is called a three-way handshake to establish a connection When handshake completes, accept returns Accept returns a new file descriptor for server to communicate daytime data to client
33
time/ctime The time function returns the current time and date since the UNIX Epoch 00:00:00 January 1, 1970, Coordinated Universal Time (UTC) The ctime function converts the current time to human readable form, i.e., Mon May 26 20:58:40 2003
34
snprintf Prints ctime result to buffer Both sprintf and snprintf print result to a buffer, but snprintf has an extra parameter that allows for checking of buffer overflow
35
Close Server closes down connection with close Initiates normal TCP termination sequence FIN is sent in each direction and each FIN is acknowledged on the other end
36
Analysis Server is IPv4 dependent Server handles only one client at a time – we refer to this as an iterative server If multiple clients arrive at one time, pending clients are queued to be serviced by the server when free It would be nice to overlap service to multiple clients – we refer to this as a concurrent server We can either have the server fork child processes to handle the clients concurrently, or have the server create threads to handle them It would be appropriate to invoke the server as an infinate daemon process
37
Client/Server Examples in Text Daytime client/server Echo client/serve (begins in Chapter 5)
38
OSI Model International Organization for Standardization (ISO) Open Systems Interconnection (OSI)
39
OSI Layers and Internet Protocol Suite 7 application details 6 presentationapplication user process 5 session Sockets 4 transportTCP or UDP XTI 3 networkIPv4 or IPv6 kernel 2 datalink device driver comm details 1 physical and hardware OSI model Internet Protocol Suite
40
Analysis Bottom two layers are device driver and networking hardware IPv4 or IPv6 occurs at Nework layer TCP or UDP occurs at Transport layer Application layer handles Web client (browser), Telnet client, Web server, FTP server, etc. Sockets are interfaces from the top 3 layers of the OSI model to the transport layer – a later example will show the application can bypass the Transport layer
41
Why have Socket API between Session and Transport Layers? Upper layers handle details of application (i.e., FTP, Telnet, HTTP) but know little about communications details Lower layers handle communications details such as send data, ack, sequencing,calculating and verifying checksums, but know little about application details Upper layers know about user processes Lower layers know about kernel processes
42
BSD Networking History First Implementation 4.2BSD became available 1983 We are using 4.5BSD which became available 1993 Many other versions are available LINUX does not fit into the Berkeley- derived classification – it was developed from scratch
43
macosx freebsd4 aix freebsd Test Networks hpux linuxsolaris 135.197.17.100 192.6.38.100 12.106.32.254 206.168.112.96 Internet
44
Network Topology Machines are spread across the Internet Virtual Private Networks (VPNs) or Secure Shell (SSH) connections provide connectivity between machines regardless of where they live
45
Discovering Network Topology There are no current UNIX standards with respect to network configuration and administration Two basic commands can be used to discover details: –netstat (located in /usr/bin) –ifconfig (located in /usr/sbin) Make sure /usr/bin and /usr/sbin are in your normal shell search path (PATH)
46
netstat netstat -i provides information on interfaces using name addresses for networks netstat -ni provides information on interfaces using numeric addresses for networks netstat -r shows routing table using name addresses for networks netstat -nr shows routing table using numeric addresses for networks
47
ifconfig Given interface names, ifconfig is used to obtain details about the interface Shows: –IP address –Broadcast address –Subnet mask MULTICAST flag indicates host supports multicasting Some implementations provide an -a flag that prints information on all configured interfaces
48
ping ping checks to see if IP address is alive ping –s sends one datagram per second that will be acknowledged by the IP address if it is alive – only need to receive one acknowledgement to know IP is alive ping –s is responded to by all IP addresses in broadcast network
49
Portable Operating System Interface (POSIX) Not a single standard, but a family of standards being developed by IEEE POSIX has been adopted as international standard by ISO and International Electrotechnical Commission called ISO/IEC POSIX components: –Part 1: System API (C language) – POSIX.1 –Part 2: Shell and Utilities – POSIX.2 –Part 3: System Administration – POSIX.3 Current status of POSIX standards are at: http://www.pasc.org/standing/sd11.html
50
64-Bit Architectures Trend since 1990s has been towards 64-bit architectures and 64-bit software 64-bit pointers can address large amounts of memory
51
Comparison of 32-Bit and 64- Bit Datatypes DatatypeILP32 ModelLP64 Model char 88 short 16 int 32 long 3264 pointer 3264
52
Specifying Type ANSI C invented size_t datatype as argument to malloc to determine the number of bytes to allocate In different systems, size_t can be 32 bits or 64 bits By mistake size of socket address was made size_t, but 64 bit size is unneeded Therefore, in sockets API use socklen_t datatype and in XTI use t_scalar_t and t_uscalar_t datatypes
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.