Java for Interfaces and Networks

Size: px
Start display at page:

Download "Java for Interfaces and Networks"

Transcription

1 Java for Interfaces and Networks Threads and Networking Federico Pecora School of Science and Technology Örebro University Federico Pecora Java for Interfaces and Networks Lecture 4 1 / 25

2 Outline 1 2 Introduction to Networking in Java Federico Pecora Java for Interfaces and Networks Lecture 4 2 / 25

3 Outline 1 2 Introduction to Networking in Java Federico Pecora Java for Interfaces and Networks Lecture 4 3 / 25

4 Threads and processes Processes: several executing program instances (multi-tasking) Threads: several paths of execution within the same process (multi-threading) Processes maintain considerable state information, have separate address spaces, and interact only through IPC (inter-process communication) mechanisms Multiple threads typically share the state information of a process and share memory and other resources directly Context switching between threads in the same process is typically faster than context switching between processes Federico Pecora Java for Interfaces and Networks Lecture 4 4 / 25

5 : simple example BoringThread.java 1 public class BoringThread extends Thread { 2 public void run() { 3 while (true) {} 4 } //run 5 public static void main(string args) { 6 BoringThread t1 = new BoringThread(); 7 t1.start(); 8 BoringThread t2 = new BoringThread(); 9 t2.start(); 10 } //main 11 } //class BoringThread Federico Pecora Java for Interfaces and Networks Lecture 4 5 / 25

6 : simple example Let the class that should be a thread inherit from Thread Define what should happen when the thread runs in the run() method Invoke the start() method in order to start the thread (this results in calling run()) Whe run() returns, the thread dies Federico Pecora Java for Interfaces and Networks Lecture 4 5 / 25

7 : a more interesting example BoringThread.java 1 public class BoringThread2 extends Thread { 2 static int numberofthreads = 0; 3 int threadnumber = ++numberofthreads; 4 public void run() { 5 int nrreps = 0; 6 while (true) { 7 System.out.println("Thread " + threadnumber + 8 " has run " + ++nrreps + " iterations."); 9 } 10 } //run 11 public static void main(string args) { 12 BoringThread2 t1 = new BoringThread2(); 13 BoringThread2 t2 = new BoringThread2(); 14 t1.start(); 15 t2.start(); 16 } //main 17 } //class BoringThread2 Federico Pecora Java for Interfaces and Networks Lecture 4 6 / 25

8 : a more interesting example linux> java BoringThread2 Thread 1 has run 1 iterations. Thread 1 has run 2 iterations. Thread 1 has run 3 iterations.... Thread 2 has run 1155 iterations. Thread 2 has run 1156 iterations. Thread 1 has run 1173 iterations. Thread 2 has run 1157 iterations. Thread 2 has run 1158 iterations. Thread 1 has run 1174 iterations. Thread 1 has run 1175 iterations.... Federico Pecora Java for Interfaces and Networks Lecture 4 6 / 25

9 Thread scheduling Do not design your threads under the assumption that they will run in any particular order if that is desired, you must synchronize them Uses of threads performance gain if multiple processors are available facilitating communication between logical elements of your programs (e.g., servers, users, multiple clients) doing several things at once (e.g., running the GUI while waiting for data) You need to think about how threads are scheduled only when at least one of your threads heavily uses the processor for long time periods intermediate results of that thread s execution are important Federico Pecora Java for Interfaces and Networks Lecture 4 7 / 25

10 Thread scheduling Federico Pecora Java for Interfaces and Networks Lecture 4 7 / 25

11 Thread scheduling The thread scheduler decides when a thread gets to run Green thread scheduling, for OSs which do not support threads (e.g., Windows 3.1) the JVM supplies the threading logic, based on thread priorities threads are called user threads Native thread scheduling, e.g., Linux, Windows XP the OS supplies the threading logic, based on thread priorities and (often) time-slicing threads are called native threads Federico Pecora Java for Interfaces and Networks Lecture 4 7 / 25

12 Thread synchronization: motivation Let s see an example in which thread synchronization is important An application that repeatedly writes a string to the console every write will be presented further ahead on the line astring astring astring astring astring three threads, one writing every 100ms, one every 200ms and one every 300ms Federico Pecora Java for Interfaces and Networks Lecture 4 8 / 25

13 Thread synchronization: motivation Presenter.java 1 public class Presenter extends Thread { 2 private String message; private double sleeptime; 3 private int steps; 4 public Presenter(String message, double sleeptime) { 5 this.message = message; 6 this.sleeptime = sleeptime; 7 this.steps = 0; 8 this.start(); 9 } 10 public void run() { 11 while (true) { 12 for (int i = 0; i < steps; ++i) System.out.print(" "); 13 ++this.steps; 14 System.out.println(this.message); 15 try { Thread.sleep((int)(this.sleepTime * 1000)); } 16 catch (InterruptedException e) { 17 System.out.println("Oh! Someone woke me up!"); 18 } } } //catch, while, run 19 //continues... Federico Pecora Java for Interfaces and Networks Lecture 4 8 / 25

14 Thread synchronization: motivation Presenter.java (cont.) 20 public static void main(string[] args) { 21 Presenter t1 = new Presenter("FirstThread", 0.1); 22 Presenter t2 = new Presenter("SecondThread", 0.2); 23 Presenter t3 = new Presenter("ThirdThread", 0.3); 24 } //main 25 } //class Presenter linux> java Presenter FirstThread SecondThread ThirdThread FirstThread SecondThread FirstThread ThirdThread FirstThread SecondThread FirstThread FirstThread ThirdThread SecondThread FirstThread FirstThread SecondThread FirstThread ThirdThread FirstThread SecondThread FirstThread FirstThread ThirdThread SecondThread FirstThread FirstThread SecondThread Federico Pecora Java for Interfaces and Networks Lecture 4 8 / 25

15 Thread synchronization: motivation It is possible that the thread printouts do not occur in the order they are expected to linux> java Presenter... FirstThread... ThirdThread SecondThread SecondThread FirstThread FirstThread FirstThread SecondThread This is because the threads are not synchronized Federico Pecora Java for Interfaces and Networks Lecture 4 8 / 25

16 Thread synchronization: motivation Presenter.java (frag.) 1 String syncobject = "foo"; 2 public void run() { 3 while (true) { 4 synchronized(syncobject) { 5 for (int i = 0; i < steps; ++i) System.out.print(" "); 6 ++this.steps; 7 System.out.println(this.message); 8 } //synchronized 9 try { Thread.sleep((int)(this.sleepTime * 1000)); } 10 catch (InterruptedException e) { 11 System.out.println("Oh! Someone woke me up!"); 12 } //catch 13 } //while 14 } //run Every object has an intrinsic lock associated with it Critical sections can be defined over shared objects Federico Pecora Java for Interfaces and Networks Lecture 4 8 / 25

17 Extending Thread vs. implementing Runnable Java provides two ways of implementing threaded applications by extending the Thread class by implementing the Runnable interface We have seen the former in the previous examples Extending Thread can be a problem when we our class should already inherit from another class A way around the impossiblity to extend multiple classes is to implement interface Runnable Federico Pecora Java for Interfaces and Networks Lecture 4 9 / 25

18 Threads as Runnable classes: example BoringRunnable.java 1 public class BoringRunnable implements Runnable { 2 public void run() { 3 while (true) {} 4 } //run 5 public static void main(string[] args) { 6 BoringRunnable r1 = new BoringRunnable(); 7 Thread t1 = new Thread(r1, "Thread 1"); 8 t1.start(); 9 BoringRunnable r2 = new BoringRunnable(); 10 Thread t2 = new Thread(r2, "Thread 2"); 11 t2.start(); 12 } //main 13 } //class BoringRunnable Federico Pecora Java for Interfaces and Networks Lecture 4 10 / 25

19 Threads as Runnable classes: example Let the class that needs to be a thread implement the Runnable interface Define what should happen when the thread runs in the run() method Instantiate your implementation of Runnable Create Thread objects around the Runnables Invoke the start() method of the created Threads Federico Pecora Java for Interfaces and Networks Lecture 4 10 / 25

20 are part of the language, not external libraries as in C++ The JVM and/or the OS provide thread scheduling Can be defined by extending the class Thread Can be defined by implementing the interface Runnable Thread synchronization obtained by synchronizing against any object with the synchronize keyword Federico Pecora Java for Interfaces and Networks Lecture 4 11 / 25

21 Thread states Initial state: A program has created a thread s Thread object, but the thread object s start() method has not yet been called Runnable state: This is a thread s default state after the call to start() completes, a thread becomes runnable whether or not that thread is using the processor Blocked state: Occurs when a thread executes the sleep(), wait(), or join() methods, when a thread attempts to read data not yet available from a network, and when a thread waits to acquire a lock NB: Thread schedulers determine which runnable thread to assign to the processor Terminating state: Occurs when a thread returns from the run() method Federico Pecora Java for Interfaces and Networks Lecture 4 12 / 25

22 Synchronized One can use synchronized to isolate critical sections, i.e., parts of code that should not be interrupted this can be a block of code that synchronizes over an Object s lock synchronize can be used to indicate that an entire method is a critical section i.e., the Object whose lock is used is this This occurs typically when different threads need to access shared data structures An example: a Buffer contains data a WriterThread puts data into the Buffer a ReaderThread picks data out of the Buffer Federico Pecora Java for Interfaces and Networks Lecture 4 13 / 25

23 Buffer management example Buffer.java 1 class Buffer { 2 private String[] queue; 3 private int number; 4 public Buffer (int size) { 5 queue = new String[size]; 6 number = 0; 7 } //Buffer 8 public synchronized void put(string str) { 9 queue[number++] = str; 10 } //put 11 public synchronized String get() { 12 String s = queue[0]; 13 for (int i = 1; i < number; ++i) 14 queue[i - 1] = queue[i]; 15 --number; 16 return s; 17 } //get 18 } //class Buffer Federico Pecora Java for Interfaces and Networks Lecture 4 14 / 25

24 Buffer management example WriterThread.java 1 class WriterThread extends Thread { 2 Buffer b; 3 public WriterThread(Buffer b) { 4 this.b = b; 5 } 6 public void run() { 7 int nrreps = 0; 8 while (true) { 9 String s = "String number " + ++nrreps; 10 System.out.println("WriterThread: wrote " + s); 11 b.put(s); 12 } 13 } //run 14 } //class WriterThread Federico Pecora Java for Interfaces and Networks Lecture 4 14 / 25

25 Buffer management example ReaderThread.java 1 class ReaderThread extends Thread { 2 Buffer b; 3 public ReaderThread(Buffer b) { 4 this.b = b; 5 } 6 public void run() { 7 while (true) { 8 String s = b.get(); 9 System.out.println("ReaderThread: read " + s); 10 } 11 } //run 12 } //class ReaderThread Federico Pecora Java for Interfaces and Networks Lecture 4 14 / 25

26 Buffer management example BufferExample.java 1 public class BufferExample { 2 public static void main(string[] args) { 3 Buffer b = new Buffer(100); 4 WriterThread w = new WriterThread(b); 5 ReaderThread r = new ReaderThread(b); 6 w.start(); 7 r.start(); 8 } //main 9 } //class BufferExample Federico Pecora Java for Interfaces and Networks Lecture 4 14 / 25

27 Buffer management example BufferExample.java 1 public class BufferExample { 2 public static void main(string[] args) { 3 Buffer b = new Buffer(100); 4 WriterThread w = new WriterThread(b); 5 ReaderThread r = new ReaderThread(b); 6 w.start(); 7 r.start(); 8 } //main 9 } //class BufferExample Although synchronize ensures that reading and writing never occur at the same time, there remains a problem... Federico Pecora Java for Interfaces and Networks Lecture 4 14 / 25

28 Buffer management example BufferExample.java 1 public class BufferExample { 2 public static void main(string[] args) { 3 Buffer b = new Buffer(100); 4 WriterThread w = new WriterThread(b); 5 ReaderThread r = new ReaderThread(b); 6 w.start(); 7 r.start(); 8 } //main 9 } //class BufferExample The reader can attempt to read when the buffer is empty, and the writer can attempt to write when the buffer is full Federico Pecora Java for Interfaces and Networks Lecture 4 14 / 25

29 Buffer management example BufferExample.java 1 public class BufferExample { 2 public static void main(string[] args) { 3 Buffer b = new Buffer(100); 4 WriterThread w = new WriterThread(b); 5 ReaderThread r = new ReaderThread(b); 6 w.start(); 7 r.start(); 8 } //main 9 } //class BufferExample Solution: the reader should wait for the writer to write when the buffer is empty, and the writer should wait for the reader to read when the buffer is full Federico Pecora Java for Interfaces and Networks Lecture 4 14 / 25

30 Buffer management example Buffer.java (put() method) 1 public synchronized void put(string str) { 2 while(number == queue.length) { 3 try { 4 System.out.println("Buffer.put: Waiting.."); 5 wait(); 6 } 7 catch (InterruptedException e) { 8 System.out.println("Buffer.put: Oh!"); 9 } 10 } 11 queue[number++] = str; 12 notify(); 13 } //put Federico Pecora Java for Interfaces and Networks Lecture 4 14 / 25

31 Buffer management example Buffer.java (get() method) 1 public synchronized String get() { 2 while(number == 0) { 3 try { 4 System.out.println("Buffer.get: Waiting.."); 5 wait(); 6 } 7 catch (InterruptedException e) { 8 System.out.println("Buffer.get: Oh!"); 9 } 10 } 11 String s = queue[0]; 12 for (int i = 1; i < number; ++i) 13 queue[i - 1] = queue[i]; 14 --number; 15 notify(); 16 return s; 17 } // get Federico Pecora Java for Interfaces and Networks Lecture 4 14 / 25

32 wait and notify A thread forces itself to wait for a prerequisite for continued execution to exist before it continues The waiting thread assumes that some other thread will create that condition and then notify it wait() induces the thread to wait for a notification puts a thread in blocked state notify() notify a waiting thread that it can stop waiting puts the notified thread in runnable state notifyall() notify all waiting threads to go to runnable state Federico Pecora Java for Interfaces and Networks Lecture 4 15 / 25

33 wait and notify Observe the following race condition 1 thread A tests a condition and discovers it must wait 2 thread B sets the condition and calls notify() to inform A to resume execution, but since A is not yet waiting, nothing happens 3 thread A waits, by calling wait() 4 because of the prior notify() call, A waits indefinitely Why can the race condition not happen in our buffer management example? Because our threads invoke wait() and notify() from within a synchronized context if you do not do this, you will get an IllegalMonitorStateException! Federico Pecora Java for Interfaces and Networks Lecture 4 16 / 25

34 wait and notify Observe the following race condition 1 thread A tests a condition and discovers it must wait 2 thread B sets the condition and calls notify() to inform A to resume execution, but since A is not yet waiting, nothing happens 3 thread A waits, by calling wait() 4 because of the prior notify() call, A waits indefinitely Why can the race condition not happen in our buffer management example? Because our threads invoke wait() and notify() from within a synchronized context if you do not do this, you will get an IllegalMonitorStateException! Federico Pecora Java for Interfaces and Networks Lecture 4 16 / 25

35 Introduction to Networking in Java Outline 1 2 Introduction to Networking in Java Federico Pecora Java for Interfaces and Networks Lecture 4 17 / 25

36 Introduction to Networking in Java Networking in Java Java provides good support for networking Unlike C/C++, networked applications in Java are not platform dependent Support for applets Support for TCP and UDP through classes in java.net URL, URLConnection, Socket, and ServerSocket classes all use TCP to communicate over the network DatagramPacket, DatagramSocket, and MulticastSocket classes are for use with UDP Provides programmatic access to network parameters (via class java.net.networkinterface) Federico Pecora Java for Interfaces and Networks Lecture 4 18 / 25

37 Introduction to Networking in Java Networking basics Computers communicate to each other using either TCP (Transmission Control Protocol) or UDP (User Datagram Protocol) When you write Java programs that communicate over the network, you are programming at the application layer TCP provides a reliable flow of data at the transport level UDP provides an unreliable flow of data at the transport level (e.g., where the order of data is not important) Server PORT TCP Client Federico Pecora Java for Interfaces and Networks Lecture 4 19 / 25

38 Introduction to Networking in Java Networking basics: TCP and UDP Applications for which order and integrity of data is important (e.g., a browser) are built on TCP e.g., the application level protocols HTTP, FTP, SSH Data packets sent over UDP should not depend on each other e.g., the ping command, a network time server The TCP and UDP protocols use ports to map incoming data to a particular process running on a computer Server PORT TCP Client Federico Pecora Java for Interfaces and Networks Lecture 4 20 / 25

39 Introduction to Networking in Java Networking basics: Ports A server is a process that listens and responds to requests on a specific port Ports are identified by port number (16 bits, that is up to 65535) Ports can also have a name (e.g., smtp = 25, ssh = 22, http = 80) Ports are reserved ( Well Known Ports ) Ports can be reserved for various services ( Registered Ports, e.g., 1433 for Microsoft SQL Server, 6000 for the X11, for Quake) Ports are dynamic or private Server PORT TCP Client Federico Pecora Java for Interfaces and Networks Lecture 4 21 / 25

40 Introduction to Networking in Java A simple example: get-a-web-page applet An applet that provides a button Pressing the button induces the page to load the URL ShowURLApplet.java 1 import java.applet.*; 2 import java.awt.*; 3 import java.awt.event.*; 4 import java.net.*; 5 import javax.swing.*; 6 //continues... Federico Pecora Java for Interfaces and Networks Lecture 4 22 / 25

41 Introduction to Networking in Java A simple example: get-a-web-page applet ShowURLApplet.java (cont.) 7 public class ShowURLApplet extends JApplet 8 implements ActionListener { 9 public void actionperformed(actionevent event) { 10 this.handle_button(); 11 } //actionperformed 12 private void handle_button() { 13 try { 14 URL onionurl = new URL(" 15 getappletcontext().showdocument(onionurl); 16 } //try 17 catch (java.net.malformedurlexception e) { 18 //Error in web address 19 } 20 } //handle_button 21 //continues... Federico Pecora Java for Interfaces and Networks Lecture 4 22 / 25

42 Introduction to Networking in Java A simple example: get-a-web-page applet ShowURLApplet.java (cont.) 22 public void init() { 23 Container cp = getcontentpane(); 24 cp.setlayout(new FlowLayout()); 25 JButton button = new JButton("Load The Onion"); 26 button.addactionlistener(this); 27 cp.add(button); 28 } //init 29 } //ShowURLApplet Federico Pecora Java for Interfaces and Networks Lecture 4 22 / 25

43 Introduction to Networking in Java Network communication with sockets A simple echo server server listens on port 2000 (using ServerSocket) client connects to the port (using Socket) user enters strings on the client... client sends each string to the server... server prints the string and sends it back to the client server terminates connection if client sends quit or empty string Federico Pecora Java for Interfaces and Networks Lecture 4 23 / 25

44 Introduction to Networking in Java Network communication with sockets Client.java 1 import java.net.*; 2 import java.io.*; 3 public class Client { 4 public static final int PORT = 2000; 5 public static void main(string[] args) 6 throws IOException { 7 InetAddress addr; 8 if (args.length >= 1) 9 addr = InetAddress.getByName(args[0]); 10 else 11 addr = InetAddress.getByName(null); 12 Socket socket = new Socket(addr, PORT); 13 System.out.println("The new socket: " + socket); 14 //continues... Federico Pecora Java for Interfaces and Networks Lecture 4 23 / 25

45 Introduction to Networking in Java Network communication with sockets Client.java (cont.) 15 BufferedReader in = new BufferedReader( 16 new InputStreamReader(socket.getInputStream())); 17 PrintWriter out = new PrintWriter( 18 new BufferedWriter( 19 new OutputStreamWriter( 20 socket.getoutputstream())), true); 21 //true: PrintWriter is line buffered 22 BufferedReader kbd_reader = new BufferedReader( 23 new InputStreamReader(System.in)); 24 String buf; 25 //continutes... Federico Pecora Java for Interfaces and Networks Lecture 4 23 / 25

46 Introduction to Networking in Java Network communication with sockets Client.java (cont.) 26 while (true) { 27 buf = kbd_reader.readline(); 28 System.out.println("From keyboard: " + buf); 29 System.out.println("To server: " + buf); 30 out.println(buf); 31 String inline = in.readline(); 32 if (inline == null) break; 33 System.out.println("From server: " + inline); 34 } 35 } //main 36 } //Client Federico Pecora Java for Interfaces and Networks Lecture 4 23 / 25

47 Introduction to Networking in Java Network communication with sockets Server.java 1 import java.io.*; 2 import java.net.*; 3 public class Server { 4 public static final int PORT = 2000; 5 public static void main(string[] args) 6 throws IOException { 7 ServerSocket s = new ServerSocket(PORT); 8 System.out.println("Server-socket: " + s); 9 System.out.println("Servern listening..."); 10 Socket socket = s.accept(); 11 System.out.println("Connection accepted"); 12 System.out.println("The new socket: " + socket); 13 //continues... Federico Pecora Java for Interfaces and Networks Lecture 4 23 / 25

48 Introduction to Networking in Java Network communication with sockets Server.java (cont.) 14 BufferedReader in = new BufferedReader( 15 new InputStreamReader(socket.getInputStream())); 16 PrintWriter out = new PrintWriter( 17 new BufferedWriter( 18 new OutputStreamWriter( 19 socket.getoutputstream())), true); 20 //true: PrintWriter is line buffered 21 while (true) { 22 String inline = in.readline(); 23 System.out.println("Server received: " + inline); 24 if (inline == null inline.equals("quit")) 25 break; 26 out.println("you said " + inline + " "); 27 } 28 } //main 29 } //Server Federico Pecora Java for Interfaces and Networks Lecture 4 23 / 25

49 Introduction to Networking in Java Network communication with sockets linux> java Server Server-socket: ServerSocket[addr=\ / ,port=0,\ localport=2000] Servern listening... Connection accepted The new socket: Socket[addr=/ ,\ port=38893,localport=2000] Server received: Hello Server received: My name is Kalle Server received: See ya... Server received: quit linux> linux> java Client The new socket: Socket[addr=\ localhost/ ,port=2000,\ localport=38893] Hello From keyboard: Hello To server: Hello From server: You said Hello My name is Kalle From keyboard: My name is Kalle To server: My name is Kalle From server: You said My name is Kalle See ya... From keyboard: See ya... To server: See ya... From server: You said See ya... quit From keyboard: quit To server: quit linux> Federico Pecora Java for Interfaces and Networks Lecture 4 23 / 25

50 Introduction to Networking in Java Multi-thread client/server applications In the previous example, both client and server are single-thread server listens to on a port, and then gives all its attention to the first client that connects client is either reading form keyboard or sending/waiting for response from server Multi-threaded version server has N+1 threads, one for listening and one for dealing with each of the N clients client has two threads, one for reading from keyboard, one for sending/receiving to/from server Federico Pecora Java for Interfaces and Networks Lecture 4 24 / 25

51 Introduction to Networking in Java Threads and Networking Thank you! Federico Pecora Java for Interfaces and Networks Lecture 4 25 / 25

Java for Interfaces and Networks (DT3029)

Java for Interfaces and Networks (DT3029) Java for Interfaces and Networks (DT3029) Lecture 3 Threads and Networking Federico Pecora federico.pecora@oru.se Center for Applied Autonomous Sensor Systems (AASS) Örebro University, Sweden Capiscum

More information

Java for Interfaces and Networks (DT3010, HT10)

Java for Interfaces and Networks (DT3010, HT10) Java for Interfaces and Networks (DT3010, HT10) Inner Classes Federico Pecora School of Science and Technology Örebro University federico.pecora@oru.se Federico Pecora Java for Interfaces and Networks

More information

Java for Interfaces and Networks (DT3010, HT11)

Java for Interfaces and Networks (DT3010, HT11) Java for Interfaces and Networks (DT3010, HT11) Networking with Threads and Federico Pecora School of Science and Technology Örebro University federico.pecora@oru.se Federico Pecora Java for Interfaces

More information

Java for Interfaces and Networks (DT3029)

Java for Interfaces and Networks (DT3029) Java for Interfaces and Networks (DT3029) Lecture 4 Networking with Threads and Containers Federico Pecora federico.pecora@oru.se Center for Applied Autonomous Sensor Systems (AASS) Örebro University,

More information

Introduction to Sockets

Introduction to Sockets Introduction to Sockets Sockets in Java 07/02/2012 EPL 602 1 Socket programming Goal: learn how to build client/server application that communicate using sockets Socket API o introduced in BSD4.1 UNIX,

More information

Java for Interfaces and Networks (DT3010, HT10)

Java for Interfaces and Networks (DT3010, HT10) Java for Interfaces and Networks (DT3010, HT10) Mouse Events, Timers, Serialization Federico Pecora School of Science and Technology Örebro University federico.pecora@oru.se Federico Pecora Java for Interfaces

More information

Interprocess Communication

Interprocess Communication Interprocess Communication Nicola Dragoni Embedded Systems Engineering DTU Informatics 4.2 Characteristics, Sockets, Client-Server Communication: UDP vs TCP 4.4 Group (Multicast) Communication The Characteristics

More information

Advanced Java Programming. Networking

Advanced Java Programming. Networking Advanced Java Programming Networking Eran Werner and Ohad Barzilay Tel-Aviv University Advanced Java Programming, Spring 2006 1 Overview of networking Advanced Java Programming, Spring 2006 2 TCP/IP protocol

More information

Basic Java Network Programming. CS211 July 30 th, 2001

Basic Java Network Programming. CS211 July 30 th, 2001 Basic Java Network Programming CS211 July 30 th, 2001 The Network and OSI Model IP Header TCP Header TCP/IP: A Paradox TCP Connection Oriented and Connectionless Protocols Reliable: no loss, or duplication,

More information

public static void main(string[] args) throws IOException { sock = new Socket(args[0], Integer.parseInt(args[1]));

public static void main(string[] args) throws IOException { sock = new Socket(args[0], Integer.parseInt(args[1])); Echo Client&Server Application EchoClient import java.net.*; import java.io.*; class EchoClient public static void main(string[] args) throws IOException if (args.length < 2) number>"); System.err.println("Usage:

More information

Java Networking (sockets)

Java Networking (sockets) Java Networking (sockets) Rui Moreira Links: http://java.sun.com/docs/books/tutorial/networking/toc.html#sockets http://www.javaworld.com/javaworld/jw-12-1996/jw-12-sockets_p.html Networking Computers

More information

Reading from URL. Intent - open URL get an input stream on the connection, and read from the input stream.

Reading from URL. Intent - open URL  get an input stream on the connection, and read from the input stream. Simple Networking Loading applets from the network. Applets are referenced in a HTML file. Java programs can use URLs to connect to and retrieve information over the network. Uniform Resource Locator (URL)

More information

CSCD 330 Network Programming Spring 2018

CSCD 330 Network Programming Spring 2018 CSCD 330 Network Programming Spring 2018 Lecture 7 Application Layer Socket Programming in Java Reading: Chapter 2, Java links Relevant Links page Some Material in these slides from J.F Kurose and K.W.

More information

CSCD 330 Network Programming Winter 2019

CSCD 330 Network Programming Winter 2019 CSCD 330 Network Programming Winter 2019 Lecture 7 Application Layer Socket Programming in Java Reading: Chapter 2, Java links Relevant Links page Some Material in these slides from J.F Kurose and K.W.

More information

Network Programming. Powered by Pentalog. by Vlad Costel Ungureanu for Learn Stuff

Network Programming. Powered by Pentalog. by Vlad Costel Ungureanu for Learn Stuff Network Programming by Vlad Costel Ungureanu for Learn Stuff Java Network Protocols 2 Java Network Protocols 3 Addresses Innet4Address (32-bit) 85.122.23.145 - numeric pentalog.com symbolic Innet6Address

More information

Tommy Färnqvist, IDA, Linköping University

Tommy Färnqvist, IDA, Linköping University Lecture 4 Threads and Networking in Java TDDC32 Lecture notes in Design and Implementation of a Software Module in Java 23 January 2013 Tommy Färnqvist, IDA, Linköping University 4.1 Lecture Topics Contents

More information

Java Threads. COMP 585 Noteset #2 1

Java Threads. COMP 585 Noteset #2 1 Java Threads The topic of threads overlaps the boundary between software development and operation systems. Words like process, task, and thread may mean different things depending on the author and the

More information

CSCD 330 Network Programming Spring 2018

CSCD 330 Network Programming Spring 2018 CSCD 330 Network Programming Spring 2018 Lecture 6 Application Layer Socket Programming in Java Reading for Java Client/Server see Relevant Links Some Material in these slides from J.F Kurose and K.W.

More information

MIT AITI Swing Event Model Lecture 17

MIT AITI Swing Event Model Lecture 17 MIT AITI 2004 Swing Event Model Lecture 17 The Java Event Model In the last lecture, we learned how to construct a GUI to present information to the user. But how do GUIs interact with users? How do applications

More information

Multithread Computing

Multithread Computing Multithread Computing About This Lecture Purpose To learn multithread programming in Java What You Will Learn ¾ Benefits of multithreading ¾ Class Thread and interface Runnable ¾ Thread methods and thread

More information

Chapter 2 Application Layer

Chapter 2 Application Layer Chapter 2 Application Layer A note on the use of these ppt slides: We re making these slides freely available to all (faculty, students, readers). They re in PowerPoint form so you can add, modify, and

More information

Networking Basics. network communication.

Networking Basics. network communication. JAVA NETWORKING API Networking Basics When you write Java programs that communicate over the network, you are programming at the application layer. Typically, you don't need to concern yourself with the

More information

JAVA - NETWORKING (SOCKET PROGRAMMING)

JAVA - NETWORKING (SOCKET PROGRAMMING) JAVA - NETWORKING (SOCKET PROGRAMMING) http://www.tutorialspoint.com/java/java_networking.htm Copyright tutorialspoint.com The term network programming refers to writing programs that execute across multiple

More information

Threads in Java (Deitel & Deitel)

Threads in Java (Deitel & Deitel) Threads in Java (Deitel & Deitel) OOutline 1 Introduction 1 Class Thread: An Overview of the Thread Methods 1 Thread States: Life Cycle of a Thread 1 Thread Priorities and Thread Scheduling 1 Thread Synchronization

More information

Previous lecture: threads G51PRG: Introduction to Programming Second semester Lecture 12 URL

Previous lecture: threads G51PRG: Introduction to Programming Second semester Lecture 12 URL Previous lecture: threads G51PRG: Introduction to Programming Second semester Lecture 12 What is a thread Why use multiple threads Issues and problems involved Java threads Natasha Alechina School of Computer

More information

URL Kullanımı Get URL

URL Kullanımı Get URL Networking 1 URL Kullanımı Get URL URL info 2 import java.io.*; import java.net.*; public class GetURL { public static void main(string[] args) { InputStream in = null; OutputStream out = null; // Check

More information

CPSC 441 UDP Socket Programming. Department of Computer Science University of Calgary

CPSC 441 UDP Socket Programming. Department of Computer Science University of Calgary CPSC 441 UDP Socket Programming Department of Computer Science University of Calgary Socket programming using UDP (vs TCP) UDP: no connection between client and server vno handshaking vsender explicitly

More information

Computation Abstractions. Processes vs. Threads. So, What Is a Thread? CMSC 433 Programming Language Technologies and Paradigms Spring 2007

Computation Abstractions. Processes vs. Threads. So, What Is a Thread? CMSC 433 Programming Language Technologies and Paradigms Spring 2007 CMSC 433 Programming Language Technologies and Paradigms Spring 2007 Threads and Synchronization May 8, 2007 Computation Abstractions t1 t1 t4 t2 t1 t2 t5 t3 p1 p2 p3 p4 CPU 1 CPU 2 A computer Processes

More information

CSCD 330 Network Programming

CSCD 330 Network Programming CSCD 330 Network Programming Lecture 8 Client-Server Programming Threads Spring 2018 Reading: Chapter 2, Relevant Links - Threads Some Material in these slides from J.F Kurose and K.W. Ross All material

More information

UDP Sockets TCP guarantees the delivery of packets and preserves their order on destination. Sometimes these features are not required, since they do

UDP Sockets TCP guarantees the delivery of packets and preserves their order on destination. Sometimes these features are not required, since they do UDP Sockets TCP guarantees the delivery of packets and preserves their order on destination. Sometimes these features are not required, since they do not come without performance costs, it would be better

More information

CSCD 330 Network Programming

CSCD 330 Network Programming CSCD 330 Network Programming Lecture 8 Client-Server Programming Threads Spring 2017 Reading: Chapter 2, Relevant Links - Threads Some Material in these slides from J.F Kurose and K.W. Ross All material

More information

Multithreaded OO. Questions:

Multithreaded OO. Questions: Multithreaded OO Questions: 1 1. What s the following Applet code snippet doing? Also, which statement is establishing the network connection? public void actionperformed( ActionEvent evt ) { try { String

More information

A socket is a software endpoint that establishes bidirectional communication between a server program and one or more client programs.

A socket is a software endpoint that establishes bidirectional communication between a server program and one or more client programs. PART 24 Java Network Applications 24.1 Java Socket Programming A socket is a software endpoint that establishes bidirectional communication between a server program and one or more client programs. A server

More information

Chapter 2 Application Layer

Chapter 2 Application Layer Internet and Intranet Protocols and Applications Lecture 4: Application Layer 3: Socket Programming Spring 2006 Arthur Goldberg Computer Science Department New York University artg@cs.nyu.edu Chapter 2

More information

Assignment 1. Due date February 6, 2007 at 11pm. It must be submitted using submit command.

Assignment 1. Due date February 6, 2007 at 11pm. It must be submitted using submit command. Assignment 1 Due date February 6, 2007 at 11pm. It must be submitted using submit command. Note: submit 4213 a1 . Read the manpages ("man submit") for more details on the submit command. It is

More information

Core Java SYLLABUS COVERAGE SYLLABUS IN DETAILS

Core Java SYLLABUS COVERAGE SYLLABUS IN DETAILS Core Java SYLLABUS COVERAGE Introduction. OOPS Package Exception Handling. Multithreading Applet, AWT, Event Handling Using NetBean, Ecllipse. Input Output Streams, Serialization Networking Collection

More information

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2015

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2015 CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2015 Name: This exam consists of 5 problems on the following 7 pages. You may use your double- sided hand- written 8 ½ x 11 note sheet

More information

Communication Paradigms

Communication Paradigms Communication Paradigms Nicola Dragoni Embedded Systems Engineering DTU Compute 1. Interprocess Communication Direct Communication: Sockets Indirect Communication: IP Multicast 2. High Level Communication

More information

Java.net Package and Classes(Url, UrlConnection, HttpUrlConnection)

Java.net Package and Classes(Url, UrlConnection, HttpUrlConnection) Java.net Package and Classes(Url, UrlConnection, HttpUrlConnection) Sisoft Technologies Pvt Ltd SRC E7, Shipra Riviera Bazar, Gyan Khand-3, Indirapuram, Ghaziabad Website: www.sisoft.in Email:info@sisoft.in

More information

Networking: IPv6, UDP and TCP. Network Programming in Java UDP and TCP

Networking: IPv6, UDP and TCP. Network Programming in Java UDP and TCP Networking: IPv6, UDP and TCP Network Programming in Java UDP and TCP SCOMRED, November 2018 Instituto Superior de Engenharia do Porto (ISEP) Departamento de Engenharia Informática(DEI) SWitCH Computing

More information

Threads Chate Patanothai

Threads Chate Patanothai Threads Chate Patanothai Objectives Knowing thread: 3W1H Create separate threads Control the execution of a thread Communicate between threads Protect shared data C. Patanothai Threads 2 What are threads?

More information

Animation Part 2: MoveableShape interface & Multithreading

Animation Part 2: MoveableShape interface & Multithreading Animation Part 2: MoveableShape interface & Multithreading MoveableShape Interface In the previous example, an image was drawn, then redrawn in another location Since the actions described above can apply

More information

JAVA SOCKET PROGRAMMING

JAVA SOCKET PROGRAMMING JAVA SOCKET PROGRAMMING WHAT IS A SOCKET? Socket The combination of an IP address and a port number. (RFC 793 original TCP specification) The name of the Berkeley-derived application programming interfaces

More information

Principles of Software Construction. Introduction to networks and distributed systems School of Computer Science

Principles of Software Construction. Introduction to networks and distributed systems School of Computer Science Principles of Software Construction Introduction to networks and distributed systems Josh Bloch Charlie Garrod School of Computer Science 1 Administrivia Homework 5 Best Frameworks available tonight Or

More information

CMSC 132: Object-Oriented Programming II. Threads in Java

CMSC 132: Object-Oriented Programming II. Threads in Java CMSC 132: Object-Oriented Programming II Threads in Java 1 Problem Multiple tasks for computer Draw & display images on screen Check keyboard & mouse input Send & receive data on network Read & write files

More information

CS180 Review. Recitation Week 15

CS180 Review. Recitation Week 15 CS180 Review Recitation Week 15 Announcement Final exam will be held on Thursday(12/17) 8:00~10:00 AM The coverage is comprehensive Project 5 is graded. Check your score in Blackboard. Classes and Methods

More information

Java Programming Language Advance Feature

Java Programming Language Advance Feature Java Programming Language Advance Feature Peter.Cheng founder_chen@yahoo.com.cn http://www.huihoo.com 2004-04 Huihoo - Enterprise Open Source http://www.huihoo.com 1 Course Goal The main goal of this course

More information

Concurrent Programming using Threads

Concurrent Programming using Threads Concurrent Programming using Threads Threads are a control mechanism that enable you to write concurrent programs. You can think of a thread in an object-oriented language as a special kind of system object

More information

Java for Interfaces and Networks (DT3010, HT10)

Java for Interfaces and Networks (DT3010, HT10) Java for Interfaces and Networks (DT3010, HT10) More on Swing and Threads Federico Pecora School of Science and Technology Örebro University federico.pecora@oru.se Federico Pecora Java for Interfaces and

More information

Java Socket Workshop. July Purpose of this workshop:

Java Socket Workshop. July Purpose of this workshop: Java Socket Workshop July 2012 Purpose of this workshop: The objective of this workshop is to gain experience with writing and compiling programs using the Java programming language. The exercises provide

More information

CSCD 330 Network Programming

CSCD 330 Network Programming CSCD 330 Network Programming Lecture 8 Client-Server Programming Threads Winter 2015 Reading: Chapter 2, Relevant Links Some Material in these slides from J.F Kurose and K.W. Ross All material copyright

More information

Chair of Software Engineering. Java and C# in depth. Carlo A. Furia, Marco Piccioni, Bertrand Meyer. Java: concurrency

Chair of Software Engineering. Java and C# in depth. Carlo A. Furia, Marco Piccioni, Bertrand Meyer. Java: concurrency Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer Java: concurrency Outline Java threads thread implementation sleep, interrupt, and join threads that return values Thread synchronization

More information

Chapter 2: outline. 2.1 principles of network applications. 2.6 P2P applications 2.7 socket programming with UDP and TCP

Chapter 2: outline. 2.1 principles of network applications. 2.6 P2P applications 2.7 socket programming with UDP and TCP Chapter 2: outline 2.1 principles of network applications app architectures app requirements 2.2 Web and HTTP 2.3 FTP 2.4 electronic mail SMTP, POP3, IMAP 2.5 DNS 2.6 P2P applications 2.7 socket programming

More information

Outlines. Networking in Java. Internet hardware structure. Networking Diagram. IP Address. Networking in Java. Networking basics

Outlines. Networking in Java. Internet hardware structure. Networking Diagram. IP Address. Networking in Java. Networking basics G52APR Application programming Networking in Java Michael Li http://www.cs.nott.ac.uk/~jwl/g52apr Outlines Networking basics Network architecture IP address and port Server-client model TCP and UDP protocol

More information

Lab 1 : Java Sockets

Lab 1 : Java Sockets Lab 1 : Java Sockets 1. Goals In this lab you will work with a low-level mechanism for distributed communication. You will discover that Java sockets do not provide: - location transparency - naming transparency

More information

Introduction to Sockets 9/25/14

Introduction to Sockets 9/25/14 Introduction to Sockets 9/25/14 81 Remote communication Inter-process communication is at the heart of all distributed systems Using the network protocol stack on a node is the only way to communicate

More information

Java s Implementation of Concurrency, and how to use it in our applications.

Java s Implementation of Concurrency, and how to use it in our applications. Java s Implementation of Concurrency, and how to use it in our applications. 1 An application running on a single CPU often appears to perform many tasks at the same time. For example, a streaming audio/video

More information

Chapter 19 Multithreading

Chapter 19 Multithreading Chapter 19 Multithreading Prerequisites for Part VI Chapter 14 Applets, Images, and Audio Chapter 19 Multithreading Chapter 20 Internationalization 1 Objectives To understand the concept of multithreading

More information

Networking and Security

Networking and Security Chapter 03 Networking and Security Mr. Nilesh Vishwasrao Patil Government Polytechnic Ahmednagar Socket Network socket is an endpoint of an interprocess communication flow across a computer network. Sockets

More information

Java Technologies. Lecture VII. Valdas Rapševičius. Vilnius University Faculty of Mathematics and Informatics

Java Technologies. Lecture VII. Valdas Rapševičius. Vilnius University Faculty of Mathematics and Informatics Preparation of the material was supported by the project Increasing Internationality in Study Programs of the Department of Computer Science II, project number VP1 2.2 ŠMM-07-K-02-070, funded by The European

More information

Principles, Models, and Applications for Distributed Systems M

Principles, Models, and Applications for Distributed Systems M Università degli Studi di Bologna Facoltà di Ingegneria Principles, Models, and Applications for Distributed Systems M Exercitation 3 Connected Java Sockets Jacopo De Benedetto Distributed architecture

More information

Networking, Java threads and synchronization. PRIS lecture 4 Fredrik Kilander

Networking, Java threads and synchronization. PRIS lecture 4 Fredrik Kilander Networking, Java threads and synchronization PRIS lecture 4 Fredrik Kilander OSI Application Presentation Session Transport Network Data link Physical TCP/IP Application Transport Internet Host-to-network

More information

Communication in Distributed Systems: Sockets Programming. Operating Systems

Communication in Distributed Systems: Sockets Programming. Operating Systems Communication in Distributed Systems: Sockets Programming Operating Systems TCP/IP layers Layers Message Application Transport Internet Network interface Messages (UDP) or Streams (TCP) UDP or TCP packets

More information

Chapter 4: Processes

Chapter 4: Processes Chapter 4: Processes Process Concept Process Scheduling Operations on Processes Cooperating Processes Interprocess Communication Communication in Client-Server Systems 4.1 Process Concept An operating

More information

Multiple Choice Questions: Identify the choice that best completes the statement or answers the question. (15 marks)

Multiple Choice Questions: Identify the choice that best completes the statement or answers the question. (15 marks) M257 MTA Spring2010 Multiple Choice Questions: Identify the choice that best completes the statement or answers the question. (15 marks) 1. If we need various objects that are similar in structure, but

More information

Week 13 Lab - Exploring Connections & Remote Execution

Week 13 Lab - Exploring Connections & Remote Execution Week 13 Lab - Exploring Connections & Remote Execution COSC244 & TELE202 1 Assessment This lab is worth 0.5%. The marks are awarded for completing the programming exercise and answering the questions.

More information

Part IV Other Systems: I Java Threads

Part IV Other Systems: I Java Threads Part IV Other Systems: I Java Threads Spring 2019 C is quirky, flawed, and an enormous success. 1 Dennis M. Ritchie Java Threads: 1/6 Java has two ways to create threads: Create a new class derived from

More information

UNIT-3 : MULTI THREADED PROGRAMMING, EVENT HANDLING. A Multithreaded program contains two or more parts that can run concurrently.

UNIT-3 : MULTI THREADED PROGRAMMING, EVENT HANDLING. A Multithreaded program contains two or more parts that can run concurrently. UNIT-3 : MULTI THREADED PROGRAMMING, EVENT HANDLING 1. What are Threads? A thread is a single path of execution of code in a program. A Multithreaded program contains two or more parts that can run concurrently.

More information

Distributed Systems Recitation 2. Tamim Jabban

Distributed Systems Recitation 2. Tamim Jabban 15-440 Distributed Systems Recitation 2 Tamim Jabban Agenda Communication via Sockets in Java (this enables you to complete PS1 and start P1 (goes out today!)) Multi-threading in Java Coding a full Client-Server

More information

Multi-threading in Java. Jeff HUANG

Multi-threading in Java. Jeff HUANG Multi-threading in Java Jeff HUANG Software Engineering Group @HKUST Do you use them? 2 Do u know their internals? 3 Let s see File DB How can they service so many clients simultaneously? l 4 Multi-threading

More information

Topic 10: Network Programming

Topic 10: Network Programming Topic 10: Network Programming Client-Server Model Host and Port Socket Implementing Client Implementing Server Implementing Server for Multiple Clients Client-Server Model Clients Request a server to provide

More information

Sockets and RMI. CS151 Chris Pollett Dec. 5, 2005.

Sockets and RMI. CS151 Chris Pollett Dec. 5, 2005. Sockets and RMI CS151 Chris Pollett Dec. 5, 2005. Outline Echo Server with Multiple Clients Client pull/server push Remote Method Invocation Proxy Pattern Echo Server with Multiple Clients public class

More information

Software Practice 1 - Multithreading

Software Practice 1 - Multithreading Software Practice 1 - Multithreading What is the thread Life cycle of thread How to create thread Thread method Lab practice Prof. Joonwon Lee T.A. Jaehyun Song Jongseok Kim T.A. Sujin Oh Junseong Lee

More information

BSc ( Hons) Computer Science with Network Security. Examinations for / Semester 2

BSc ( Hons) Computer Science with Network Security. Examinations for / Semester 2 BSc ( Hons) Computer Science with Network Security Cohort: BCNS/16B/FT Examinations for 2017 2018 / Semester 2 Resit Examinations for BCNS/15B/FT & BCNS/16A/FT MODULE: NETWORK PROGRAMMING MODULE CODE:

More information

Chapter 2 Applications and

Chapter 2 Applications and Chapter 2 Applications and Layered Architectures Sockets Socket API API (Application Programming Interface) Provides a standard set of functions that can be called by applications Berkeley UNIX Sockets

More information

Threads and Locks. CSCI 5828: Foundations of Software Engineering Lecture 09 09/22/2015

Threads and Locks. CSCI 5828: Foundations of Software Engineering Lecture 09 09/22/2015 Threads and Locks CSCI 5828: Foundations of Software Engineering Lecture 09 09/22/2015 1 Goals Cover the material presented in Chapter 2, Day 1 of our concurrency textbook Creating threads Locks Memory

More information

Transport layer protocols. Lecture 15: Operating Systems and Networks Behzad Bordbar

Transport layer protocols. Lecture 15: Operating Systems and Networks Behzad Bordbar Transport layer protocols Lecture 15: Operating Systems and Networks Behzad Bordbar 78 Interprocess communication Synchronous and asynchronous comm. Message destination Reliability Ordering Client Server

More information

Java for Interfaces and Networks (DT3029)

Java for Interfaces and Networks (DT3029) Java for Interfaces and Networks (DT3029) Lecture 9 More on Swing and Threads Federico Pecora federico.pecora@oru.se Center for Applied Autonomous Sensor Systems (AASS) Örebro University, Sweden www.clipartlord.com

More information

Produced by. Design Patterns. MSc in Computer Science. Eamonn de Leastar

Produced by. Design Patterns. MSc in Computer Science. Eamonn de Leastar Design Patterns MSc in Computer Science Produced by Eamonn de Leastar (edeleastar@wit.ie)! Department of Computing, Maths & Physics Waterford Institute of Technology http://www.wit.ie http://elearning.wit.ie

More information

Principles, Models, and Applications for Distributed Systems M

Principles, Models, and Applications for Distributed Systems M Università degli Studi di Bologna Facoltà di Ingegneria Principles, Models, and Applications for Distributed Systems M Lab assignment 4 (worked-out) Connection-oriented Java Sockets Luca Foschini Winter

More information

COMP 213. Advanced Object-oriented Programming. Lecture 20. Network Programming

COMP 213. Advanced Object-oriented Programming. Lecture 20. Network Programming COMP 213 Advanced Object-oriented Programming Lecture 20 Network Programming Network Programming A network consists of several computers connected so that data can be sent from one to another. Network

More information

Object Oriented Programming (II-Year CSE II-Sem-R09)

Object Oriented Programming (II-Year CSE II-Sem-R09) (II-Year CSE II-Sem-R09) Unit-VI Prepared By: A.SHARATH KUMAR M.Tech Asst. Professor JAWAHARLAL NEHRU TECHNOLOGICAL UNIVERSITY, HYDERABAD. (Kukatpally, Hyderabad) Multithreading A thread is a single sequential

More information

CS September 2017

CS September 2017 Machine vs. transport endpoints IP is a network layer protocol: packets address only the machine IP header identifies source IP address, destination IP address Distributed Systems 01r. Sockets Programming

More information

Multiplayer Game Programming 2/26

Multiplayer Game Programming 2/26 Multiplayer Game Programming 2/26 1. Turn off Windows Firewall 2. Download and install Python and Notepad++ a. Python.org downloads/python/install b. Notepad-plus-plus.org download/install 3. Download

More information

COMP31212: Concurrency A Review of Java Concurrency. Giles Reger

COMP31212: Concurrency A Review of Java Concurrency. Giles Reger COMP31212: Concurrency A Review of Java Concurrency Giles Reger Outline What are Java Threads? In Java, concurrency is achieved by Threads A Java Thread object is just an object on the heap, like any other

More information

CSS 533 Program 2: Distributed Java Space Servers Professor: Munehiro Fukuda Due date: see the syllabus

CSS 533 Program 2: Distributed Java Space Servers Professor: Munehiro Fukuda Due date: see the syllabus CSS 533 Program 2: Distributed Java Space Servers Professor: Munehiro Fukuda Due date: see the syllabus 1. Purpose This assignment implements a collection of distributed Java Space servers, which exercises

More information

Chapter 2 outline. 2.1 Principles of app layer protocols

Chapter 2 outline. 2.1 Principles of app layer protocols Chapter 2 outline 2.1 Principles of app layer protocols clients and servers app requirements 2.2 Web and HTTP 2.3 FTP 2.4 Electronic Mail SMTP, POP3, IMAP 2.5 DNS 2.6 Socket programming with TCP 2.7 Socket

More information

CS2307 NETWORKS LAB 1. Programs using TCP Sockets (like date and time server & client, echo server & client, etc.) 2. Programs using UDP Sockets

CS2307 NETWORKS LAB 1. Programs using TCP Sockets (like date and time server & client, echo server & client, etc.) 2. Programs using UDP Sockets CS2307 NETWORKS LAB 1. Programs using TCP Sockets (like date and time server & client, echo server & client, etc.) 2. Programs using UDP Sockets (like simple DNS) 3. Programs using Raw sockets (like packet

More information

CS193k, Stanford Handout #8. Threads 3

CS193k, Stanford Handout #8. Threads 3 CS193k, Stanford Handout #8 Spring, 2000-01 Nick Parlante Threads 3 t.join() Wait for finish We block until the receiver thread exits its run(). Use this to wait for another thread to finish. The current

More information

Advanced Programming Methods. Lecture 6 - Concurrency in Java (1)

Advanced Programming Methods. Lecture 6 - Concurrency in Java (1) Advanced Programming Methods Lecture 6 - Concurrency in Java (1) Overview Introduction Java threads Java.util.concurrent References NOTE: The slides are based on the following free tutorials. You may want

More information

Java Support for developing TCP Network Based Programs

Java Support for developing TCP Network Based Programs Java Support for developing TCP Network Based Programs 1 How to Write a Network Based Program (In Java) As mentioned, we will use the TCP Transport Protocol. To communicate over TCP, a client program and

More information

Chapter 2. Network Chat

Chapter 2. Network Chat Chapter 2. Network Chat In a multi-player game, different players interact with each other. One way of implementing this is to have a centralized server that interacts with each client using a separate

More information

CS 556 Distributed Systems

CS 556 Distributed Systems CS 556 Distributed Systems Tutorial on 4 Oct 2002 Threads A thread is a lightweight process a single sequential flow of execution within a program Threads make possible the implementation of programs that

More information

Networking. Lecture 25 ish? COP 3252 Summer July 11, 2017

Networking. Lecture 25 ish? COP 3252 Summer July 11, 2017 Networking Lecture 25 ish? COP 3252 Summer 2017 July 11, 2017 Open-Read/Write-Close The Unix I/O system follows a paradigm usually referred to as Open-Read/Write-Close. Before a program/process can perform

More information

RAIK 183H Examination 2 Solution. November 10, 2014

RAIK 183H Examination 2 Solution. November 10, 2014 RAIK 183H Examination 2 Solution November 10, 2014 Name: NUID: This examination consists of 5 questions and you have 110 minutes to complete the test. Show all steps (including any computations/explanations)

More information

CPSC 441 Tutorial - 11 UDP Socket Programming Department of Computer Science University of Calgary

CPSC 441 Tutorial - 11 UDP Socket Programming Department of Computer Science University of Calgary CPSC 441 Tutorial - 11 UDP Programming Department of Computer Science University of Calgary TCP Vs UDP Input: receives packet (TCP receives byte stream ) Output: sends packet (TCP sends byte stream ) What

More information

Only one thread can own a specific monitor

Only one thread can own a specific monitor Java 5 Notes Threads inherit their priority and daemon properties from their creating threads The method thread.join() blocks and waits until the thread completes running A thread can have a name for identification

More information

Java in 21 minutes. Hello world. hello world. exceptions. basic data types. constructors. classes & objects I/O. program structure.

Java in 21 minutes. Hello world. hello world. exceptions. basic data types. constructors. classes & objects I/O. program structure. Java in 21 minutes hello world basic data types classes & objects program structure constructors garbage collection I/O exceptions Strings Hello world import java.io.*; public class hello { public static

More information

C19: User Datagram and Multicast

C19: User Datagram and Multicast CISC 3120 C19: User Datagram and Multicast Hui Chen Department of Computer & Information Science CUNY Brooklyn College 4/18/2018 CUNY Brooklyn College 1 Outline Recap Network fundamentals IPv4, IPv6 addresses

More information

Concurrent Computing CSCI 201 Principles of Software Development

Concurrent Computing CSCI 201 Principles of Software Development Concurrent Computing CSCI 201 Principles of Software Development Jeffrey Miller, Ph.D. jeffrey.miller@usc.edu Outline Threads Multi-Threaded Code CPU Scheduling Program USC CSCI 201L Thread Overview Looking

More information