Transcript PowerPoint

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
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
TCP Socket Operations
Server process
socket ()
bind ()
listen ()
Client process
socket ()
accept ()
Block waiting
for a request
read ()
connect ()
write ()
Process request
write ()
read ()
TCP in Java
Server process
ServerSocket ()
accept ()
Client process
Socket ()
Block waiting
for a request
Socket ()
write ()
read ()
Process request
write ()
read ()
TCP in Java
• Implemented in java.net.*
• Two main classes
– java.net.ServerSocket
– java.net.Socket
- for server
- for client
• Provides abstractions over
– Socket and connect operations
– Bind, listen & accept
• Remember to close the socket at the end
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)
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
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) {}
}
}
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) {}
}
}
Issues
• Can only deal with one request at once
• Performance
– Have to wait until the guy in front has finished
with the server
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 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 TThread ().start ();
}
}
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 ();
}
}
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) {}
}
}
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 ();
}
}
Issues
• Performance
– Thread creation at client connection
– Creating threads takes time
• Possible deadlock
– Due to multithreaded
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) {}
}
}
Issues
•
•
•
•
•
•
Possible deadlock
Resource thrashing
Concurrency errors
Thread leakage
Request overload
Performance
– Thread creation still has to be performed in the
main loop
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
Summary