140 likes | 280 Views
TCP/IP protocols. Communication over Internet is mostly TCP/IP (Transmission Control Protocol over Internet Protocol) TCP/IP "stack" is software which allows applications to communicate over network TCP/IP support is either built into OS (e.g... UNIX) or available as an add-on .
E N D
TCP/IP protocols • Communication over Internet is mostly TCP/IP (Transmission Control Protocol over Internet Protocol) • TCP/IP "stack" is software which allows applications to communicate over network • TCP/IP support is either built into OS (e.g... UNIX) or available as an add-on
The TCP/IP Protocol Architecture Application is what the users see, e.g. programs such as ftp, email, web browser, telnet, etc. TCP (transmission control protocol) takes messages from the application, breaks them up into packets and sends them to the remote system where the message is put back together and passed to the application - TCP corrects for errors in transmission (e.g. due to noise) and looks after flow control (a slow system talking to a fast one). IP (Internet protocol) looks after addressing of machines (each machine has its own unique address) and routing the packets over the underlying network. Low level stuff is the Network Interface Layer connecting to the underlying network(s) - TCP/IP was developed by the USA Dept of Defence to operate over multiple unreliable local or wide area networks connecting many different types of computer systems.
IP provides a datagram (connectionless) transport service across the network. UDP provides applications with an end-to-end datagram (connectionless) service, packets send individually with full destination address, may not arrive, may arrive out of order, e.g. used by ping, finger, etc. TCP provides applications with a a virtual circuit (connection-oriented) communication service across the network • Virtual circuit set up thru network (switch tables in each router) • Packets sent over virtual circuit, errors fixed, in order, flow control, etc • Virtual circuit closed Used by most applications (error free) HTTP, FTP, POP, SMTP, etc.
IPAddresses Every machine on a TCP/IP network requires a unique address so it can be identified and packets routed to it. IP addresses are 32 bits in length typically written as a sequence of four 8-bit numbers (range 0 to 255), representing the decimal value of each of the address bytes. e.g. 199.182.20.17. DHCP (Dynamic Host Configuration Protocol) Machines permanently connected to the internet have a static (fixed) IP address assigned by network manager Machines which are not permanently connected may have a dynamic IP address which is assigned on connection. DHCP is the protocol for assigning dynamic IP addresses – the ISP has a range of IP addresses available which are assigned when devices connect and become free on disconnection.
IP Domains and Host Names Most IP hosts have both a numeric IP address and a domain name. Internet hosts use a hierarchical naming structure comprising a top-level domain (TLD), domain and subdomain (optional), and host name, e.g. www.dmu.ac.uk = 146.227.1.23 Domain Name Servers (DNS) Domain names are convenient for people, however, the name must be translated back to a numeric address for routing purposes: names and numbers are stored by a "domain name server" (DNS) Client programs may query the DNS to find a number before making a connection, e.g. UNIX nslookup command e.g. www.dmu.ac.uk = 146.227.1.23
Clients and servers and TCP and UDP ports To send a message to the server (e.g. to collect email) the client has to send a packet to • a particular program, e.g. the email server • running on a particular machine, e.g. DMU’s email server Requirement b) is satisfied by knowing the machine’s Domain Name or IP address, e.g. DMU’s email server is helios.dmu.ac.uk on IP address 146.227.1.2. However, a particular machine may be running several servers (email, ftp, www, etc.) so how is a packet delivered to the correct program, i.e. requirement a) above? This achieved by ‘ports’ via which programs communicate, i.e. servers ‘listen’ on particular ports and clients send messages to that port. When TCP/IP is running on a particular machine (with a particular IP address) TCP and UDP each have 65536 ports numbered 0 to 65535 many of which are reserved for standard services (typically 0 to 1024).
A server using TCP (server.java) Client and server send simple text messages to each other to be displayed in a JTextArea In the server method main() (at end of file) • Creates a new Server object to create the GUI Server app = new Server(); 2. Calls the runServer method to do client communication app.runServer(); Server’s constructor creates the GUI (extends JFrame) • JTextField enter for text input to send to client • JTextArea display for text output
Method runServer creates a ServerSocket, waits for client to connect and process information Step 1: Create a ServerSocket. server = new ServerSocket( 15000, 100 ); Step 2: Wait for a connection. connection = server.accept(); Step 3: Get input and output streams. output = new ObjectOutputStream( connection.getOutputStream() ); output.flush(); input = new ObjectInputStream(connection.getInputStream() ); Step 4: Loop - process connection receiving messages from client message = (String) input.readObject(); casts to a String, appends to display, terminates on command Step 5: Closes connection.
Method sendData sends the text entered in the JTextField to the client private void sendData( String s ) { try { output.writeObject( "SERVER>>> " + s ); output.flush(); display.append( "\nSERVER>>>" + s ); } catch ( IOException cnfex ) { display.append("\nError writing object" ); } }
The client (client.java) connects to server and sends text In the server method main() (at end of file) • Creates a new Client object to create the GUI Client app = new Client(); 2. Calls the runClient method to do server communication app.runClient(); Client’s constructor creates the GUI (extends JFrame) • JTextField enter for text input to send to server • JTextArea display for text output
Method runClient creates a Socket to communicate with the server Step 1: Create a Socket to make connection client = new Socket(InetAddress.getByName( "127.0.0.1" ), 15000 ); First parameter is Internet address of server (localhost “127.0.0.1” in this case) and second is port number (15000) Step 2: Get the input and output streams. output = new ObjectOutputStream(client.getOutputStream() ); output.flush(); input = new ObjectInputStream(client.getInputStream() ); Step 3: Process connection – loops read messages from server message = (String) input.readObject(); until server sends terminate message Step 4: Close connection.
Method sendData sends the text entered in the JTextField to the client private void sendData( String s ) { try { output.writeObject( “CLIENT>>> " + s ); output.flush(); display.append( "\nCLIENT>>>" + s ); } catch ( IOException cnfex ) { display.append("\nError writing object" ); } }
Threaded client/server example To be able to handle multiple simultaneous clients a server needs to create a Thread (a lightweight process) for each client which connects The BarServer/BarClient is a simulation of a shop • BarServer.java is a server which maintains a database of products in the following classes • Product.java – holds product barcode, contents, etc • Inventory.java – stores a number of Products • BarClient.java simulates a till which sends barcodes to the server and receives product details