1 / 26

Agenda

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

makan
Download Presentation

Agenda

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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.

  3. 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

  4. Communication Among Devices mycp script: Set up file descriptors (fd) #!/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 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

  5. 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.

  6. 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/0 Solution works, but is “clumsy” and prone to security issues…In this case it is better to use sockets…

  7. 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).

  8. 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

  9. 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!!

  10. 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)

  11. 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

  12. Local Sockets TIP: try to setup a consistent “back & forth” communication involving read and write commands to prevent lockups(eg. both reading or writing at same time!) Tip:Setup logic within a loop to have server and client do specific actions based on messages sent to each otherUseful Techniques / Commands: Send message as first character in string (strcpy, strcmp, sprintf, sscanf)Refer to examples in Sigma:~msaul/unx511/sockets Server Client read write write read read write write read

  13. Sockets • The server/client method of using sockets is similar to a company switchboard (server) accepting incoming calls from customers (clients) Switchboard waits (listen)for a call from a client Company Serverswitchboard CompanyDepartment 1 Customer A (client A) CompanyDepartment 2

  14. Sockets • The server/client method of using sockets is similar to a company switchboard (server) accepting incoming calls from customers (clients) Switchboard waits (listen)for a call from a client Company Serverswitchboard Incoming call CompanyDepartment 1 Customer A (client A) CompanyDepartment 2

  15. Sockets • The server/client method of using sockets is similar to a company switchboard (server) accepting incoming calls from customers (clients) Switchboard answers and accepts call. Company Serverswitchboard Connectionestablished CompanyDepartment 1 Customer A (client A) CompanyDepartment 2

  16. Sockets • The server/client method of using sockets is similar to a company switchboard (server) accepting incoming calls from customers (clients) Switchboard determines departmentrequested & creates direct line to department Company Serverswitchboard Connectionestablished CompanyDepartment 1 Customer A (client A) Direct line (connection) CompanyDepartment 2

  17. Sockets • The server/client method of using sockets is similar to a company switchboard (server) accepting incoming calls from customers (clients) Switchboard disconnects initialconnection & listens for incoming calls Company Serverswitchboard CompanyDepartment 1 Customer A (client A) Direct line (connection) CompanyDepartment 2

  18. Sockets • The server/client method of using sockets is similar to a company switchboard (server) accepting incoming calls from customers (clients) Switchboard listens for incoming calls Company Serverswitchboard Incoming call CompanyDepartment 1 Customer A (client A) Direct line (connection) CompanyDepartment 2 Customer B (client B)

  19. Sockets • The server/client method of using sockets is similar to a company switchboard (server) accepting incoming calls from customers (clients) Switchboard answers and accepts call. Company Serverswitchboard Connectionestablished CompanyDepartment 1 Customer A (client A) Direct line (connection) CompanyDepartment 2 Customer B (client B)

  20. Sockets • The server/client method of using sockets is similar to a company switchboard (server) accepting incoming calls from customers (clients) Switchboard determines departmentrequested & creates direct line to department Company Serverswitchboard Connectionestablished CompanyDepartment 1 Customer A (client A) Direct line (connection) CompanyDepartment 2 Customer B (client B) Direct line (connection)

  21. Sockets • The server/client method of using sockets is similar to a company switchboard (server) accepting incoming calls from customers (clients) Switchboard disconnects initialconnection & listens for incoming calls Company Serverswitchboard CompanyDepartment 1 Customer A (client A) Direct line (connection) CompanyDepartment 2 Customer B (client B) Direct line (connection)

  22. 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.

  23. 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.

  24. 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

  25. 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

  26. 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

More Related