Chapter 1

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

UNIX Network Programming

Chapter 1: Introduction

Rana AlQurem

Fall 2021
1
• Writing programs that communicate with each other over a
computer network
• We need a protocol to govern how data is exchanged
between applications/programs
• A given program can be a client (caller) or a server (callee)
• The client initiates a communication with the server
• The client-server architecture/model is used by most
networked applications

Request

Client Server

Response
2
3
1.1 Introduction (continued)
• Example : Client and Server on different LANs
connected through WAN.
client server
application application

Host Host
with with
TCP/IP TCP/IP

LAN LAN

router router

WAN
router router router router

Figure 1.4 Client and server on different LANs connected through a WAN

4
5
Sockets API
• We need socket API (or, simply a socket) to access (read from
/ write to) the network
• The API is provided by the Operating System
• Example library functions: read(), write(), close(), …
• We will cover the Socket API in UNIX-based systems using the
C programming language
• High-level applications/programs are built upon sockets
– Web browsers/servers; E-mail; Instant messaging; Peer-to-peer
file sharing systems
• Types of Sockets
– Stream sockets implement connection-oriented semantics (TCP)
– Datagram sockets implement connection-less semantics (UDP)
– Raw sockets bypass the transport layer & use IP directly
6
1.2 A Simple Daytime Client
• A TCP daytime client
• Many Unix machines run a daytime server on port #13
• Try on an appropriate UNIX machine
TCP Client
– telnet <machineIPaddress> 13
socket()

• Client Development Connection Est.


connect()
1. Create a socket 3-way handshake
2. Establish a connection with the server Data (request)
write
3. Send data to the server
4. Receive data back Data (reply)
read()
5. Repeat steps 3 & 4 (if needed)
6. Close the connection
EOF notification
close()
7
1.2 A Simple Daytime Client (continued)

8
1.2 A Simple Daytime Client (continued)
• The preamble (include required header files)
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>

1. Create a socket System call IPv4 TCP


Socket
Descriptor
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) { If creation is
perror("cannot create socket"); unsuccessful
return 1; print error &
exit
}
9
1.2 A Simple Daytime Client (continued)
• Preparing the address structure to the server
struct sockaddr_in servaddr; struct var for IP addr & port #
memset(&servaddr, 0, sizeof(servaddr)); Clearing the struct
servaddr.sin_family = AF_INET; Internet/IPv4 address
IP addr servaddr.sin_addr.s_addr = inet_addr("192.168.56.101");
servaddr.sin_port = htons(13); Port #

2. Establish a connection to server


connect( System call
sockfd, socket handle
(struct sockaddr *) &servaddr, pointer to address struct
(casted)
sizeof(servaddr) size of the address struct
)

10
1.2 A Simple Daytime Client (continued)
• 3. Send data to the server (Not needed in this example)
char sendline[500];
// Fill some text in the `sendline’ buffer
write(sockfd, sendline, strlen(sendline)); System call

4. Receive data from server System call


char recvline[1001]; Array of char to hold data from socket
while ( (n = read(sockfd, recvline, 1000)) > 0) {
recvline[n] = 0; Explicit null termination
printf(recvline); Print reply on stdout
}
6. Close the connection
close(sockfd); System call

11
1.4 Error Handling: Wrapper Functions
• Since terminating on an error is the common case, we can shorten our program
by defining a wrapper function that performs the actual function call, tests the
return value, and terminates on an error. (capitalize function name)
• sockfd = Socket(AF_INET, SOCK_STREAM, 0);
int
Socket(int family, int type, int protocol)
{
int n;
if( (n = socket( family, type, protocol)) < 0 )
err_sys(“socket error”);
return (n);
}

Figure 1.7 Our wrapper function for the socket function

• Unix errno Value: a global variable set to a positive value indicating the error when
function fails (the function returns -1)
– The value is undefined if the function returns no error
– Error values are defined in <sys/error.h> header file, and names start with E

12
1.5 A Simple Daytime Server
• A TCP daytime server TCP Server
• Server Development socket()
1. Create TCP socket: get a file descriptor
2. Bind the socket with its local (well-known) port Bind()
3. Listen: convert the socket to a listening socket
4. Accept client connection then send reply
listen()
1.Accept blocks to sleep
2.Accept returns a connected descriptor
Accept()
* (when 3-way handshaking finishes)
*( a new descriptor is returned for new client )
3.Read/write: to a client using the connected descriptor write()

5.Close socket
close()

13
1.5 A Simple Daytime Server

14
1.5 A Simple Daytime Server
– A daytime service simply sends the current date and time as a
character string without regard to the input
– A server listens for TCP connections on TCP port 13
– Once a connection is established the current date and time is
sent out the connection as an ASCII character string
– And any data received is thrown away
– The service closes the connection after sending the quote

15
Practice
• Download the full source code file (daytimetcpcli.c)
from elearning
• Change the code in line 26 to reflect the IP address of a
Linux/Unix machine running the daytime service
• Compile and run the program
$ gcc daytimetcpcli.c -o daytimetcpcli.o
$ ./daytimetcpcli.o

16

You might also like