Download presentation
Presentation is loading. Please wait.
Published byHugo Ryan Modified over 9 years ago
1
Using TCP sockets in Java Created by M Bateman, A Ruddle & C Allison As part of the TCP View project
2
Overview TCP socket Client/Server Multithread server Thread pooling server Alternate thread pooling server
3
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
4
TCP Socket Operations
5
TCP in Java
6
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
7
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)
8
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
9
Daytime server import 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) {} }
10
Daytime client import 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) {} }
11
Issues Can only deal with one request at once Performance –Have to wait until the guy in front has finished with the server
12
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 (); }
13
Multithreaded Daytime import 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) {} }
14
Daytime Thread import 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 (); }
15
Issues Performance –Thread creation at client connection –Creating threads takes time Possible deadlock –Due to multithreaded
16
Thread Pools - Basic import 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) {} }
17
Issues Possible deadlock Resource thrashing Concurrency errors Thread leakage Request overload Performance –Thread creation still has to be performed in the main loop
18
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
19
Summary
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.