using tcp sockets in java created by m bateman, a ruddle & c allison as part of the tcp view...

19
Using TCP sockets in Java Created by M Bateman, A Ruddle & C Allison As part of the TCP View project

Upload: hugo-ryan

Post on 17-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Using TCP sockets in Java Created by M Bateman, A Ruddle & C Allison As part of the TCP View project

Using TCP sockets in Java

Created by

M Bateman, A Ruddle & C Allison

As part of the TCP View project

Page 2: Using TCP sockets in Java Created by M Bateman, A Ruddle & C Allison As part of the TCP View project

Overview

• TCP socket

• Client/Server

• Multithread server

• Thread pooling server

• Alternate thread pooling server

Page 3: Using TCP sockets in Java Created by M Bateman, A Ruddle & C Allison As part of the TCP View project

TCP Provides

• Process to process communication– Use tuple of IP address & port

• Reliable

• In order

• Socket is one end-point of a two way connection link

Page 4: Using TCP sockets in Java Created by M Bateman, A Ruddle & C Allison As part of the TCP View project

TCP Socket Operations

socket ()

listen ()

bind ()

accept ()

read ()

socket ()

connect ()

write ()

write ()read ()

Block waiting for a request

Process request

Server process

Client process

Page 5: Using TCP sockets in Java Created by M Bateman, A Ruddle & C Allison As part of the TCP View project

TCP in Java

ServerSocket ()

accept ()

read ()

Socket ()

write ()

write ()

read ()

Block waiting for a request

Process request

Client process

Socket ()

Server process

Page 6: Using TCP sockets in Java Created by M Bateman, A Ruddle & C Allison As part of the TCP View project

TCP in Java

• Implemented in java.net.*

• Two main classes– java.net.ServerSocket - for server– java.net.Socket - for client

• Provides abstractions over– Socket and connect operations– Bind, listen & accept

• Remember to close the socket at the end

Page 7: Using TCP sockets in Java Created by M Bateman, A Ruddle & C Allison As part of the TCP View project

ServerSocket

• Constructors– ServerSocket (port)– ServerSocket (port, backlog)– ServerSocket (port, backlog, bindAddress)

• Where– port = the TCP port to listen on– backlog = TCP queue length– bindAddress = interface to use (else all)

Page 8: Using TCP sockets in Java Created by M Bateman, A Ruddle & C Allison As part of the TCP View project

Socket

• Construction– Two important constructors– Socket () – random local port– Socket (host, port) – remote host and port

• Is the end point for communication– Used in both the client and server– Client connects to ServerSocket– ServerSocket returns Socket for

communications

Page 9: Using TCP sockets in Java Created by M Bateman, A Ruddle & C Allison As part of the TCP View project

Daytime serverimport java.net.*;import java.io.*public class DTServer {public static void main (String argv[]) { int dayTimePort = 13; try { ServerSocket dtserver = new ServerSocket (dayTimePort); while (Socket con = dtserver.accept ()) { PrintWriter out = new PrintWriter (con.getOutputStream (), true); Date now = new Date (); out.println (now.toString ()); con.close (); } } catch (Exception e) {} }}

Page 10: Using TCP sockets in Java Created by M Bateman, A Ruddle & C Allison As part of the TCP View project

Daytime clientimport java.net.*;import java.io.*public class DTClient {public static void main (String argv[]) { int dayTimePort = 13; try { Socket con = new Socket (argv[0], dayTimePort); InputStream is = con.getInputStream (); BufferedReader br = new BufferedReader (new InputStreamReader (is)); String time = br.readLine (); System.out.println (“The time at “ + argv[0] + “ is “ + time); con.close (); } catch (Exception e) {} }}

Page 11: Using TCP sockets in Java Created by M Bateman, A Ruddle & C Allison As part of the TCP View project

Issues

• Can only deal with one request at once

• Performance– Have to wait until the guy in front has finished

with the server

Page 12: Using TCP sockets in Java Created by M Bateman, A Ruddle & C Allison As part of the TCP View project

Solution Threads• Thread theory not covered here

– Basically allows server to do more than one thing at once (see operating system course to find out why this isn’t true)

• Two ways– Extend java.lang.Thread– Implement java.lang.Runnable

• Override public void run ()public class TThread extend Thread {

public void run () { for (int i = 0; i < 100; i++) { System.out.println (i); } }public static void main (String argv[]) { new TThread ().start (); }}

public class RThread implements Runnable {

public void run () { for (int i = 0; i < 100; i++) { System.out.println (i); } }public static void main (String argv[]) { new Thread (new TThread ()).start (); }}

Page 13: Using TCP sockets in Java Created by M Bateman, A Ruddle & C Allison As part of the TCP View project

Multithreaded Daytimeimport java.net.*;import java.io.*public class DTServer {public static void main (String argv[]) { int dayTimePort = 13; try { ServerSocket dtserver = new ServerSocket (dayTimePort); while (Socket con = dtserver.accept ()) { DTThread dtthread = new DTThread (); // create the new thread dtthread.setSocket (con); dtthread.start (); // start the thread now } } catch (Exception e) {} }}

Page 14: Using TCP sockets in Java Created by M Bateman, A Ruddle & C Allison As part of the TCP View project

Daytime Threadimport java.net.*;public class DTThread extends Thread {Socket con = null;

public void setSocket (Socket con) { this.con = con; }

public void run () { // Important work done here PrintWriter out = new PrintWriter (con.getOutputStream (), true); Date now = new Date (); out.println (now.toString ()); con.close (); }}

Page 15: Using TCP sockets in Java Created by M Bateman, A Ruddle & C Allison As part of the TCP View project

Issues

• Performance– Thread creation at client connection– Creating threads takes time

• Possible deadlock– Due to multithreaded

Page 16: Using TCP sockets in Java Created by M Bateman, A Ruddle & C Allison As part of the TCP View project

Thread Pools - Basicimport java.net.*;import java.io.*public class DTServer {public static void main (String argv[]) { int dayTimePort = 13; List list = Collections.synchronizedList(new LinkedList()); for (int i = 0; i < 10; i ++) { list.add (new DTThread ()); } // create 10 threads try { ServerSocket dtserver = new ServerSocket (dayTimePort); while (Socket con = dtserver.accept ()) { DTThread dtthread = (DTThread)list.remove (0); dtthread.setSocket (con); dtthread.start (); // start the thread now list.add (new DTThread ()); // create a thread to create the one we just used } } catch (Exception e) {} }}

Page 17: Using TCP sockets in Java Created by M Bateman, A Ruddle & C Allison As part of the TCP View project

Issues

• Possible deadlock

• Resource thrashing

• Concurrency errors

• Thread leakage

• Request overload

• Performance– Thread creation still has to be performed in the

main loop

Page 18: Using TCP sockets in Java Created by M Bateman, A Ruddle & C Allison As part of the TCP View project

Pool Alternative Implementation

• Create pool as before

• Create thread which makes sure the pool has n thread waiting

• On connection take thread from the pool– The above thread makes sure there are always

threads in the pool waiting to be used

Page 19: Using TCP sockets in Java Created by M Bateman, A Ruddle & C Allison As part of the TCP View project

Summary