Socket based Client/Server Systems Exercises in
Exercises Build a generic client Build an echo server Build a http client and server Build a proxy/firewall Using code pieces from the Java examples book (see resources) we will: We will discuss general design issues as well! (patterns etc)
Preparations Download zipped examples Create Jbuilder project Go to net directory and include all java files into the project.
Clients GenericClient (allows input to any server and displays responses. Use e.g. with the example server or a remote smtp mail server) HttpClient (connects to a web-server and downloads URL from command line)
Servers HttpMirror (sends http parameters back) Server (generic server, multithreaded with built in services like time, httpmirror, remote control, echo etc.) First step to servlet engine or application server Needed: a simple web-server (e.g. from Newards book)
Dynamic Server Serve() Time service, Port X http mirror service Port Y Server creates new socket, loads service dynamically and provides threads from a pool for the service connect If we change Serve() to ServiceRequest() and use a URL (e.g. ) which is parsed by server to load the proper service, then we have the basic servlet engine or application container. Tread
Dynamic Server (2) Separates application code from processing context (thread/socket combination) Allows call interception e.g. to enforce authentication or authorization Allows new services to be added dynamically These are the basic jobs of an application server or a servlet engine
Compilation Problems Clients: none Server: include all classes in project and specify the name of the service parameter as a fully qualified inner class name, e.g. ….net.Server$Time as a command line parameter.
A poor man’s directory service You will notice quickly that a client needs to know some parameters of a service it wants to use: -hostname and port number -Possibly also a service name Write all available servers and their names and ports on the white board! This works just like a directory service. You (client) can locate services now. You (server) need to offer your service here.
Advanced For advanced socket programmers the use of JSSE (Java Secure Sockets Extension) to support security between clients and servers is a good example. See: and download the article on “securing Java programs with JSSE” by Jamie… Create trust- and keystores, keys etc. Use dummy browser and web-server (both provided) in a secure mode!
Adding Proxy Servers Define a proxy as an intermediate instance that intercepts calls between client and server. The proxy may look like the real server to clients thereby hiding everything that is behind it from client view (allows transparent infrastructure changes) Other reasons for proxies: enforce authentication, check access rights, filter content, translate content, balance load between invisible servers
Persistent URLs They are a nice example of a proxy pattern in action: URLs from are persistent because purl.org will redirect a request for a certain PURL to a URL stored at purl.org. This URL really points to where the resource is located and can be changed without clients noticing.
Proxy Architecture client serverproxy The proxy looks like a server for the client and like a client to the server. It can manipulate both data streams. Problem: Encrypted content cannot be modified! Reverse Proxies frequently terminate the SSL session at the proxy.
Runtime Problems Missing debug or logging in the server and proxy code forced us to insert trace statements to see if both are running. The use of a logging/tracing framework like log4j.org became obvious. Some code uses reflection to load classes which could cause performance problems especially in servlet like environments with complicated class loader paths.
System Management You will notice quickly that your client sometimes does not work even though you believe your code works and host/port is OK. - A colleague stopped his server You (client): How can you know whether the server is up and running? You (server): If you need to stop your service, do you know which client will be affected? Which MIGHT be affected? Strategies you can use to warn clients? Something you can do to avoid service interruption at all? What are your service guarantees anyway?
Resources David Flanagan, Java Examples in a Nutshell, O’Reilly, chapter 5. Code: Ted Neward, Server Based Java Programming chapter 10, Code: Doug Lea, Concurrent Programming in Java (logging package)