Software Engineering I Sept 25, 2013 Notes Last time we discussed slots and hooks We also started the client-server architecture and design of the client and server. Concurrency in Client-Server Systems - inherently concurrent - client has at least the appearance of running at the same time as the server Client does the following concurrently - waits for interaction with the end user and responds - waits for and responds to messages from the server - implemented as multiple threads Server does the following concurrently - Waits for interaction with the server user and responds as necessary - Waits for clients to connect and establishes connections as necessary - For each client connected, waits for and responds to messages from that client Server generally runs as n + 2 threads, where n is the number of clients Thin vs fat clients Thin clients are as small as possible and do little work - reliant upon the server Fat clients are delegated as much work as possible Proper choice depends upon network resources Messages in the client-server system are sent via protocol - Languages and rules of conversation Job of the software engineer when developing a client server system 1. Determine work to be performed by client and server 2. How will the work be distributed? Thin, intermediate, fat client? 3. Communications Protocol 4. Startup, handling connections, send/receive messages, and termination Technology needed to build a client-server system -Java sockets -network concepts -TCP/IP <-- Transmission Control Protocol / Internet Protocol Route messages from one computer to another -Computers using IP called hosts -IP address in numeric form - v4 - 4 numbers separated by dots - v6 - 6 numbers separated by colons -IP address may be in host name form, too. Servers are identified by a port number from 0 to 65535 ports 0-1023 are reserved for specific use e.g. 80 is the http port, 443 is the https (ssl) port The hostname or ip address + port number gives the server address e.g. Tomcat and Glassfish web servers run on port 8080 by default If run locally, navigate to http://localhost:8080 or http://127.0.0.1:8080 to access those servers Java Sockets java.net is used for many TCP/IP operations The Socket class is central to the java.net package Both client and server must have a socket to connect and exchange information 1. Server starts listening on a port ServerSocket ss = new ServerSocket(port); //The port is an int //such as 5555 2. Client connects (client socket) Socket cs = new Socket(host, port); 3. Server must have a thread for listening for connections Socket scs = ss.accept(); Waits indefinitely for a client to connect, then creates a Socket to handle the connection. IOException results from connection problems. Be aware that the connection is symmetric. There are two streams of information - client to server - server to client InputStream and OutputStream are used from java.io output = cs.getOutputStream(); input = cs.getInputStream(); It is possible to use PrintWriter and Scanner with both. You can use filters, too DataOutputStream <-- send primitives ObjectOutputStream ObjectInputStream <-- send and receive objects Must use serialization to transmit objects Must implement java.io.Serializable Example: output = new ObjectOutputStream(cs.getOutputStream()); output.writeObject(msg); input = new ObjectInputStream(clientSocket.getInputStream()); msg = input.readObject(); //Waits until object is real (received) over //socket. Get I/O error if connection is //terminated.