Agenda Terminal Handling in Unix
File Descriptors
Opening/Assigning & Closing
Sockets
Types of Sockets – Internal(Local) vs. Network(Internet) Programming for Local Sockets Programming for Network (Internet) Sockets Issues involving Server/Client models on different
computer architecture
Communication Among Devices One of the major features of the UNIX / Linux operating
system is that every element of the OS is interpreted as files:
Regular files Directory files Linked files Hardware (special character) files
Devices are files that are normally contained in the /dev directory.
Unix / Linux have mechanisms to work with files – thus work among terminals: pipes and redirection involvingfile descriptors.
Communication Among Devices File Descriptors
Each running program, called a process has file descriptors associated with it:
0 stdin 1 stdout 2 stderr
File descriptors 0, 1 & 2 are reserved by the OS, but other file descriptors from 3 onwards can be associated with processes by opening these descriptors and using them to redirect stdin, stdout and stderr.
View samples file_descriptor1, file_descriptor2, and mycp in ~msaul/pro511/sockets directory
Communication Among Devices
mycp script:#!/bin/bash
case $# in
0) exec 3<&0 4>&1 ;;
1) exec 3<$1 4>&1 ;;
2) exec 3<$1 4>$2 ;;
*) printf "\nUsage: mycp [source][destination]\n\n" >&2
exit 1;;
esac
cat <&3 >&4
exec 3<&- 4<&-
exit 0
Set up file descriptors (fd)
redirect stdin to fd3, fd4 to stdout
redirect stdin of arg1 to fd3, fd4 to stdout
redirect stdin of arg1 to fd3, fd4 to arg2
redirect stdin from fd3 and redirect stdout to fd4
Close file descriptors 3 & 4
Communication Among Devices
Examples:
mycp <- redirect stdin from terminal to terminal
mycp file1 <- redirect stdin from terminal to “file1” (copy console)
mycp file1 file2 <- redirect stdin from file1 and redirect stdout to file2
mycp /dev/pts/1 /dev/pts/0 (try to send msg to user)
Q: will this work?A: Depends on owner of terminals logged in.
Communication Among Devices
A neat little “work-around”
owner of /dev/pts/0 issues the commands:
ln –s /dev/pts/0 /tmp/back_door <- symlink to terminal
chmod 777 /tmp/back_door <- allow permissions
owner of /dev/pts/1 issues the command(s):
mycp /dev/pts/1 /tmp/back_door
or
Mycp /dev/pts/1 /dev/pts/0Solution works, but is “clumsy” and prone to security issues…In this case it is better to use sockets…
Sockets The Berkeley versions of UNIX (BSD) created the
socket interface for UNIX.
The socket interface was considered to be an extension of the “pipe” concept already used.
The socket communication mechanism allows client/server programs to work locally on a single machine (Internal or “Local” sockets) or across networks or the Internet (Network sockets).
Local Sockets Local sockets uses a named “file” stored on the server
(eg. in the /tmp directory) to allow communication between server and client processes.
Advantages: Simple Useful for inter-process communication for server & client
within the same system.
Disadvantages: Cannot provide inter-process communication if server &
client on different servers (machines) Not practical or possible for server handling multiple
clients
Local Sockets Server Program – Local Sockets:
Remove old named sockets (eg. unlink (“/tmp/unx511a01”);)
Create a socket using socket system call to assign resource to sever “process” (type of socket & Internet Protocol specified)
Name a socket using bind system call (associates the server’s file descriptor with the address of the un-named socket, upon successful operation, bind assigns the socket a name)eg. for local sockets, a pathname – eg. “/tmp/unx511a01”
Wait for clients to connect using listen system call (creates a queue for incoming connections)
TIP: use you own sigma account name for naming sockets!!
Sockets Server Program – Local Sockets:
Accept connection from client using accept system call (creates a separate socket for process communication between server & client, hangs up original socket to accept connect to client & re-enters listen mode)
During connection use read, write, ioctl & close system call(s) to communicate among server & client
Never use the open system call when writing C programs involving sockets (sockets use other special system calls)
Local Sockets Client Program – Local Sockets:
Create a socket using socket system call to assign resource to sever “process” (type of socket & Internet Protocol specified)
Connect to server using connect system call using the server’s named socket
Once connected, local sockets are used like low-level file descriptors, redirecting data between server’s process and client’s process. During connection use read, write, ioctl & close system call(s) to communicate among server & client
Local SocketsTIP: try to setup a consistent “back & forth” communication involving read and write commands to prevent lockups(eg. both reading or writing at same time!)
ClientServer
read
read
read
read
write
write
write
write
Tip:
Setup logic within a loop to have server and client do specific actions based on messages sent to each other
Useful Techniques / Commands:
Send message as first character in string (strcpy, strcmp, sprintf, sscanf)
Refer to examples in Sigma:~msaul/unx511/sockets
Sockets The server/client method of using sockets is similar to a
company switchboard (server) accepting incoming calls from customers (clients)
Customer A(client A)
Company Server
switchboard
CompanyDepartment 1
CompanyDepartment 2
Switchboard waits (listen)for a call from a client
Sockets The server/client method of using sockets is similar to a
company switchboard (server) accepting incoming calls from customers (clients)
Customer A(client A)
Company Server
switchboard
CompanyDepartment 1
Incoming call
CompanyDepartment 2
Switchboard waits (listen)for a call from a client
Sockets The server/client method of using sockets is similar to a
company switchboard (server) accepting incoming calls from customers (clients)
Customer A(client A)
Company Server
switchboard
CompanyDepartment 1
Connectionestablished
Switchboard answers andaccepts call.
CompanyDepartment 2
Sockets The server/client method of using sockets is similar to a
company switchboard (server) accepting incoming calls from customers (clients)
Customer A(client A)
Company Server
switchboard
CompanyDepartment 1
Connectionestablished
Switchboard determines departmentrequested & creates direct line to department
CompanyDepartment 2
Direct line (connection)
Sockets The server/client method of using sockets is similar to a
company switchboard (server) accepting incoming calls from customers (clients)
Customer A(client A)
Company Server
switchboard
CompanyDepartment 1
Switchboard disconnects initialconnection & listens for incoming calls
CompanyDepartment 2
Direct line (connection)
Sockets The server/client method of using sockets is similar to a
company switchboard (server) accepting incoming calls from customers (clients)
Customer A(client A)
Company Server
switchboard
CompanyDepartment 1
Switchboard listens for incoming calls
CompanyDepartment 2
Direct line (connection)
Customer B(client B)
Incoming call
Sockets The server/client method of using sockets is similar to a
company switchboard (server) accepting incoming calls from customers (clients)
Customer A(client A)
Company Server
switchboard
CompanyDepartment 1
CompanyDepartment 2
Direct line (connection)
Customer B(client B)
Connectionestablished
Switchboard answers andaccepts call.
Sockets The server/client method of using sockets is similar to a
company switchboard (server) accepting incoming calls from customers (clients)
Customer A(client A)
Company Server
switchboard
CompanyDepartment 1
CompanyDepartment 2
Direct line (connection)
Customer B(client B)
Connectionestablished
Direct line (connection)
Switchboard determines departmentrequested & creates direct line to department
Sockets The server/client method of using sockets is similar to a
company switchboard (server) accepting incoming calls from customers (clients)
Customer A(client A)
Company Server
switchboard
CompanyDepartment 1
CompanyDepartment 2
Direct line (connection)
Customer B(client B)
Direct line (connection)
Switchboard disconnects initialconnection & listens for incoming calls
Sockets LOCAL SOCKETS - Used to create server/client applications
when run local to a specific machine (i.e. not across different servers or across the Internet). Refer to ~msaul/pro511/sockets in Sigma for examples:
local_server1.c / local_client1.c Sends character ‘a’ from client to server to client.
local_server2.c / local_client2.c Prompts user for characters to send in a loop.
local_server3.c / local_client3.c Allows user within client to enter “q” to shutdown server, but
will try to read data from shutdown server causing the dreaded “broken pipe” stderr message.
Local_server4.c / local_client4.c Allows user within client program to enter “q” to shutdown
server, without the “broken pipe” stderr message.
Sockets Network Sockets
Network sockets are specified by the “domain” parameter AF_INET as opposed to AF_UNIX for local sockets.Network sockets specify a static IP address and a port number (as opposed to just a filename for local sockets)
Various port numbers are used to perform networking services in UNIX. Port numbers less than 1024 are reserved for services such as telnet, ftp, http, etc. port numbers >= 1024 can be used for own programs. (refer to /etc/services for predefined port services)
For network sockets, we need to possibly convert the different byte ordering sequence of port and address numbers for different computers. These functions are: htonl() – host to network long, and htons – host to network short. Refer to examples discussed in next slide.
Sockets Network Sockets
network_server1.c / network_client1.c Same is local_server4.c/local_client4.c, but using network socket
over port 7400 and loop-back address 127.0.0.1 (loop-back port on same machine) instead of a local socket file. This is good for trouble-shooting in case of network problems.
network_server2.c / network _client2.c Same as network_server1.c / network_client1.c, but client can
connect from any server (in this case to Sigma – 142.204.57.76)
network _server3.c / network _client3.c Sending strings between client and server on different machines
via the Internet network _server3.c / network _client3.c
Converting an integer as string (and vise versa) on different machines via the Internet
Sockets Sockets Issues
A simple server cannot accept more than 1 client at a time (i.e. until the previous client disconnects).
Solutions: use fork system call to create a new process for each client (not good for databases)
use select system call to read from any of several file descriptors (useful to update databases and read from keyboard as well…)
Discussed in next class/lab
Sockets Sockets Issues
Parallel requests which access the same data can cause conflicts. For example, client A is writing the same database record as client B is reading.
Solutions: design server program to prevent this type of conflict from occurring.
use semaphores to allow only one process at a time to execute critical sections of code.
Covered later in course