Agenda  Terminal Handling in Unix File Descriptors Opening/Assigning & Closing Sockets Types of Sockets – Internal(Local) vs. Network(Internet) Programming.

Slides:



Advertisements
Similar presentations
Lab 9 CIS 370 Umass Dartmouth.  A pipe is typically used as a one-way communications channel which couples one related process to another.  UNIX deals.
Advertisements

CSCI 1730 April 1 st, Materials Class notes slides & some “plain old” html & source code examples linked from course calendar board notes & diagrams.
© Janice Regan, CMPT 128, CMPT 371 Data Communications and Networking Socket Programming 0.
Data Communications and Networking (Third Edition)
A CHAT CLIENT-SERVER MODULE IN JAVA BY MAHTAB M HUSSAIN MAYANK MOHAN ISE 582 FALL 2003 PROJECT.
1 Java Networking – Part I CS , Spring 2008/9.
EECS122 Communications Networks Socket Programming January 30th, 2003 Jörn Altmann.
CSCI 4550/8556 Computer Networks Comer, Chapter 3: Network Programming and Applications.
Linux+ Guide to Linux Certification, Second Edition
CS3771 Today: network programming with sockets  Previous class: network structures, protocols  Next: network programming Sockets (low-level API) TODAY!
Inter Process Communication:  It is an essential aspect of process management. By allowing processes to communicate with each other: 1.We can synchronize.
CS 311 – Lecture 18 Outline IPC via Sockets – Server side socket() bind() accept() listen() – Client side connect() Lecture 181CS Operating Systems.
© 2007 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.1 Computer Networks and Internets with Internet Applications, 4e By Douglas.
McGraw-Hill©The McGraw-Hill Companies, Inc., 2004 Application Layer PART VI.
Client Server Model The client machine (or the client process) makes the request for some resource or service, and the server machine (the server process)
© Lethbridge/Laganière 2001 Chap. 3: Basing Development on Reusable Technology 1 Let’s get started. Let’s start by selecting an architecture from among.
Systems Architecture, Fourth Edition1 Internet and Distributed Application Services Chapter 13.
Guide To UNIX Using Linux Third Edition
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
INTRODUCTION TO WEB DATABASE PROGRAMMING
Socket Programming References: redKlyde ’ s tutorial set Winsock2 for games (gamedev.net)
Assignment 3 A Client/Server Application: Chatroom.
Distributed Computing Systems Project 2 – Distributed Shell Due: Friday, April 4 th.
TCP/IP protocols Communication over Internet is mostly TCP/IP (Transmission Control Protocol over Internet Protocol) TCP/IP "stack" is software which allows.
Chapter 17 Networking Dave Bremer Otago Polytechnic, N.Z. ©2008, Prentice Hall Operating Systems: Internals and Design Principles, 6/E William Stallings.
1 Chapter Client-Server Interaction. 2 Functionality  Transport layer and layers below  Basic communication  Reliability  Application layer.
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
Jozef Goetz, Application Layer PART VI Jozef Goetz, Position of application layer The application layer enables the user, whether human.
Server Sockets: A server socket listens on a given port Many different clients may be connecting to that port Ideally, you would like a separate file descriptor.
FTP Client Application CSC 8560 Brian Jorgage 4/27/2004.
Vassil Roussev 2 A socket is the basic remote communication abstraction provided by the OS to processes. controlled by operating system.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 6 System Calls OS System.
Linux+ Guide to Linux Certification, Third Edition
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
Dr. John P. Abraham Professor University of Texas Pan American Internet Applications and Network Programming.
Inter-process communication: Socket. socket Internet socket From Wikipedia, the free encyclopedia Jump to: navigation,
1 Welcome to CSC 301 Web Programming Charles Frank.
Chapter 2 Applications and Layered Architectures Sockets.
The Socket Interface Chapter 21. Application Program Interface (API) Interface used between application programs and TCP/IP protocols Interface used between.
The Socket Interface Chapter 22. Introduction This chapter reviews one example of an Application Program Interface (API) which is the interface between.
Computer Networking From LANs to WANs: Hardware, Software, and Security Chapter 13 FTP and Telnet.
Agenda  Redirection: Purpose Redirection Facts How to redirecting stdin, stdout, stderr in a program  Pipes: Using Pipes Named Pipes.
1 Computer Networks An Introduction to Computer Networks University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani Lecture 3: Sockets.
S -1 Pipes. S -2 Inter-Process Communication (IPC) Chapter Data exchange techniques between processes: –message passing: files, pipes, sockets.
CE Operating Systems Lecture 13 Linux/Unix interprocess communication.
1 Daemons & inetd Refs: Chapter Daemons A daemon is a process that: –runs in the background –not associated with any terminal Unix systems typically.
Processes CSCI 4534 Chapter 4. Introduction Early computer systems allowed one program to be executed at a time –The program had complete control of the.
Socket Programming Lab 1 1CS Computer Networks.
1 Client-Server Interaction. 2 Functionality Transport layer and layers below –Basic communication –Reliability Application layer –Abstractions Files.
Socket Programming Introduction. Socket Definition A network socket is one endpoint in a two-way communication flow between two programs running over.
The Client-Server Model And the Socket API. Client-Server (1) The datagram service does not require cooperation between the peer applications but such.
Socket Programming.
Internet Applications (Cont’d) Basic Internet Applications – World Wide Web (WWW) Browser Architecture Static Documents Dynamic Documents Active Documents.
CSI 3125, Preliminaries, page 1 Networking. CSI 3125, Preliminaries, page 2 Networking A network represents interconnection of computers that is capable.
Lecture 4 Mechanisms & Kernel for NOSs. Mechanisms for Network Operating Systems  Network operating systems provide three basic mechanisms that support.
Protocols Monil Adhikari. Agenda Introduction Port Numbers Non Secure Protocols FTP HTTP Telnet POP3, SMTP Secure Protocols HTTPS.
1 Tips for the assignment. 2 Socket: a door between application process and end- end-transport protocol (UDP or TCP) TCP service: reliable transfer of.
Agenda Overview of Seneca Computer System File Servers / Student Computer Accounts Telnet application How to Logon to Learn / Phobos accounts How to Change.
UNIT-6. Basics of Networking TCP/IP Sockets Simple Client Server program Multiple clients Sending file from Server to Client Parallel search server.
Linux+ Guide to Linux Certification, Second Edition
Inter-Process Communication 9.1 Unix Sockets You may regard a socket as being a communication endpoint. –For two processes to communicate, both must create.
BIF713 Introduction to Linux. Agenda Getting Started: Using Linux Unix and Linux - Structure / Features Elements of the Linux Philosophy Linux Command.
Introduction Contain two or more CPU share common memory and peripherals. Provide greater system throughput. Multiple processor executing simultaneous.
1 Network Communications A Brief Introduction. 2 Network Communications.
Linux Administration Working with the BASH Shell.
1 K. Salah Application Layer Module K. Salah Network layer duties.
Network Programming. These days almost all devices.
UNIX Introduction History Main Features UNIX Operating System
Chapter 3: Windows7 Part 4.
Application layer Lecture 7.
Presentation transcript:

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 involving file 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 &1 ;; 1) exec 3 &1 ;; 2) exec 3 $2 ;; *) printf "\nUsage: mycp [source][destination]\n\n" >&2 exit 1;; esac cat &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/0 Solution 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 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!) ClientServer read 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 Company Department 1 Company Department 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 Company Department 1 Incoming call Company Department 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 Company Department 1 Connection established Switchboard answers and accepts call. Company Department 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 Company Department 1 Connection established Switchboard determines department requested & creates direct line to department Company Department 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 Company Department 1 Switchboard disconnects initial connection & listens for incoming calls Company Department 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 Company Department 1 Switchboard listens for incoming calls Company Department 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 Company Department 1 Company Department 2 Direct line (connection) Customer B (client B) Connection established Switchboard answers and accepts 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 Company Department 1 Company Department 2 Direct line (connection) Customer B (client B) Connection established Direct line (connection) Switchboard determines department requested & 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 Company Department 1 Company Department 2 Direct line (connection) Customer B (client B) Direct line (connection) Switchboard disconnects initial connection & 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 (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 – ) 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