Threads in der Client/Server-Programmierung mit Java



Similar documents
Creating a Simple, Multithreaded Chat System with Java

Advanced Network Programming Lab using Java. Angelos Stavrou

!"# $ %!&' # &! ())*!$

Division of Informatics, University of Edinburgh

Transport layer protocols. Message destination: Socket +Port. Asynchronous vs. Synchronous. Operations of Request-Reply. Sockets

Introduction to Java. Module 12: Networking (Java Sockets) Prepared by Costantinos Costa for EPL 233. ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 1

Lesson: All About Sockets

Application Development with TCP/IP. Brian S. Mitchell Drexel University

String sentence = new String(receivePacket.getData()); InetAddress IPAddress = receivepacket.getaddress(); int port = receivepacket.

Network/Socket Programming in Java. Rajkumar Buyya

TP1 : Correction. Rappels : Stream, Thread et Socket TCP

Network Communication

The difference between TCP/IP, UDP/IP and Multicast sockets. How servers and clients communicate over sockets

Socket-based Network Communication in J2SE and J2ME

Socket Programming. Announcement. Lectures moved to

Building a Multi-Threaded Web Server

An Android-based Instant Message Application

ExempleRMI.java. // Fichier de defintion des droits et proprietes // System.setProperty("java.security.policy","../server.java.

Communicating with a Barco projector over network. Technical note

CISC 4700 L01 Network & Client- Server Programming Spring Harold, Chapter 8: Sockets for Clients

Corso di Reti di Calcolatori. java.net.inetaddress

Manual. Programmer's Guide for Java API

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Mail User Agent Project

Java Network Programming. The java.net package contains the Socket class. This class speaks TCP (connection-oriented protocol).

Java Network. Slides prepared by : Farzana Rahman

INPUT AND OUTPUT STREAMS

Network Programming using sockets

Tobias Flohre codecentric AG. Ein Standard für die Batch- Entwicklung JSR-352

OBJECT ORIENTED PROGRAMMING LANGUAGE

DEVELOPING CONTRACT - DRIVEN WEB SERVICES USING JDEVELOPER. The purpose of this tutorial is to develop a java web service using a top-down approach.

Agenda. Network Programming and Java Sockets. Introduction. Internet Applications Serving Local and Remote Users

CS506 Web Design and Development Solved Online Quiz No. 01

Remote Method Invocation

public static void main(string[] args) { System.out.println("hello, world"); } }

core 2 Basic Java Syntax

Socket Programming in Java

Question1-part2 What undesirable consequences might there be in having too long a DNS cache entry lifetime?

Reading Input From A File

Deadlock Victim. dimanche 6 mai 12

! "# $%&'( ) * ).) "%&' 1* ( %&' ! "%&'2 (! ""$ 1! ""3($

Capario B2B EDI Transaction Connection. Technical Specification for B2B Clients

Java Programming Unit 10. Stock Price Quotes with URL, Sockets, and RMI

Network Traffic based Application Identification Demonstrator (Net AI) GUI. Kenny Nguyen

Brazil + JDBC Juin 2001, douin@cnam.fr

ECE 122. Engineering Problem Solving with Java

Data Communication & Networks G

J2EE-Application Server

Rootbeer: Seamlessly using GPUs from Java

Threads & Tasks: Executor Framework

Explain the relationship between a class and an object. Which is general and which is specific?

Building a Java chat server

NETWORK PROGRAMMING IN JAVA USING SOCKETS

The Java I/O System. Binary I/O streams (ascii, 8 bits) The decorator design pattern Character I/O streams (Unicode, 16 bits)

Internet and Intranet Protocols and Applications

Socket programming. Socket Programming. Languages and Platforms. Sockets. Rohan Murty Hitesh Ballani. Last Modified: 2/8/2004 8:30:45 AM

DNS: Domain Names. DNS: Domain Name System. DNS: Root name servers. DNS name servers

What is an I/O Stream?

Probability and statistical hypothesis testing. Holger Diessel

e ag u g an L g ter lvin v E ram Neal G g ro va P Ja

Assignment 4 Solutions

Chapter 8 Implementing FSP Models in Java

TITLE NETWORK PROGRAMMING (SECURED CLIENT-SERVER CHAT APPLICATION) A PROJECT REPORT PRESENTED ACHILE UGBEDE JOHN CE/2007/165

Programmation RMI Sécurisée

Chapter 2 Introduction to Java programming

Database Applications Recitation 10. Project 3: CMUQFlix CMUQ s Movies Recommendation System

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

API for java.util.iterator. ! hasnext() Are there more items in the list? ! next() Return the next item in the list.

Parallel Programming

Mobila applikationer och trådlösa nät

Computer Security - Tutorial Sheet 3: Network & Programming Security

Tutorial for Creating Resources in Java - Client

Distributed Embedded Systems

CS108, Stanford Handout #33. Sockets

Programming the Flowoid NetFlow v9 Exporter on Android

Lecture J - Exceptions

Consuming a Web Service(SOAP and RESTful) in Java. Cheat Sheet For Consuming Services in Java

CS 111 Classes I 1. Software Organization View to this point:

This material is built based on, Patterns covered in this class FILTERING PATTERNS. Filtering pattern

Tutorial Reference Manual. Java WireFusion 4.1

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

Clustering with Tomcat. Introduction. O'Reilly Network: Clustering with Tomcat. by Shyam Kumar Doddavula 07/17/2002

Hadoop Streaming coreservlets.com and Dima May coreservlets.com and Dima May

Learning Outcomes. Networking. Sockets. TCP/IP Networks. Hostnames and DNS TCP/IP

Lecture 7: Java RMI. CS178: Programming Parallel and Distributed Systems. February 14, 2001 Steven P. Reiss

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

Transcription:

Threads in der Client/Server-Programmierung mit Java

Zahlenraten: Protokoll CLIENT / Comm? Comm! / max / SERVER Comm? / Comm! / 100 trial / / cmp(trial) [ cmp(trial) = < or cmp(trial) = > ] [ answer = < or answer = > ] / trial answer / / cmp(trial) [ cmp(trial) = = ] [ answer = = ] Michael Barth, Alexander Knapp, Philipp Meier, Axel Rauschmayer, LMU München 1

Zahlenraten: Client socket = new Socket(host, port); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); out = new PrintWriter(socket.getOutputStream(), true); dialog(); catch (IOException e) { System.err.println(e); finally { if (in!= null) in.close(); catch (IOException e) { catch (IOException e) { System.err.println(e); Michael Barth, Alexander Knapp, Philipp Meier, Axel Rauschmayer, LMU München 2

Java-Implementierung: Client (contd.) protected void dialog() throws IOException { String line; int max; int trial; max = getmax(); System.out.println("Ich habe mir eine Zahl zwischen 1 und " + max + " ausgedacht!"); System.out.println("Kannst Du sie eraten?"); for (int trialno = 1; trialno!= 0; ) { trial = gettrial(trialno); puttrial(trial); switch (getanswer()) { case 0: { int maxtrialno = (int)math.ceil(math.log(max)/math.log(2.0)); System.out.println("Congratulations!"); System.out.println("Du hast " + trialno + " Versuche benötigt!"); if (trialno > maxtrialno) System.out.println("Es wäre in " + maxtrialno + " zu schaffen gewesen"); trialno = 0; break; Michael Barth, Alexander Knapp, Philipp Meier, Axel Rauschmayer, LMU München 3

Java-Implementierung: Client (contd.) case -1: { System.out.println("Meine Zahl ist kleiner!"); trialno++; break; case 1: { System.out.println("Meine Zahl ist größer!"); trialno++; break; catch (UserProtocolException e) { System.err.println(e); Michael Barth, Alexander Knapp, Philipp Meier, Axel Rauschmayer, LMU München 4

Java-Implementierung: Client (contd.) int getanswer() throws UserProtocolException, IOException { String line; line = in.readline(); if (line == null) throw new UserProtocolException("Server closed connection."); if (line.startswith("=")) return 0; else if (line.startswith("<")) return -1; else if (line.startswith(">")) return 1; else throw new UserProtocolException("Protocol error."); Michael Barth, Alexander Knapp, Philipp Meier, Axel Rauschmayer, LMU München 5

Java-Implementierung: Server listen_socket = new ServerSocket(port); while (true) { Socket client_socket = listen_socket.accept(); client_socket.setsotimeout(10000); client_in = new BufferedReader(new InputStreamReader( client_socket.getinputstream())); client_out = new PrintWriter(client_socket.getOutputStream(), true); dialog(); catch (IOException e) { System.err.println(e); finally { if (client_in!= null) client_in.close(); catch (IOException e) { catch (IOException e) { System.err.println(e); Michael Barth, Alexander Knapp, Philipp Meier, Axel Rauschmayer, LMU München 6

Java-Implementierung: Server (contd.) protected void dialog() throws IOException { int number; int trial; putmax(); number = (int)(math.random() * 100.0) + 1; do { trial = gettrial(); if (number == trial) putanswer("="); else if (number < trial) putanswer("<"); else putanswer(">"); while (number!= trial); catch (UserProtocolException e) { System.err.println(e); Michael Barth, Alexander Knapp, Philipp Meier, Axel Rauschmayer, LMU München 7

Multi-Server public MultiServer(int port) { super("multiserver"); this.port = port; listen_socket = new ServerSocket(port); catch (IOException e) { fail(e, "Exception creating server socket"); threadgroup = new ThreadGroup("Server Connections"); connections = new Vector(); vulture = new Vulture(this); this.start(); Michael Barth, Alexander Knapp, Philipp Meier, Axel Rauschmayer, LMU München 8

Multi-Server (contd.) public void run() { while (true) { Socket client_socket = listen_socket.accept(); Connection c = new Connection(client_socket, threadgroup, 3, vulture); synchronized (connections) { connections.addelement(c); catch (IOException e) { fail(e, "Exception while listening for connections"); Michael Barth, Alexander Knapp, Philipp Meier, Axel Rauschmayer, LMU München 9

Multi-Server: Connection public Connection(Socket client_socket, ThreadGroup threadgroup, int priority, Vulture vulture) { super(threadgroup, "Connection-" + connection_number++); this.setpriority(priority); client = client_socket; this.vulture = vulture; client.setsotimeout(10000); client_in = new BufferedReader(new InputStreamReader( client.getinputstream())); client_out = new PrintWriter(client.getOutputStream(), true); catch (IOException e) { client.close(); catch (IOException e2) { System.err.println("Exception while getting socket streams:" + e); return; this.start(); Michael Barth, Alexander Knapp, Philipp Meier, Axel Rauschmayer, LMU München 10

Multi-Server: Connection public void run() { dialog(); catch (IOException e) { finally { client.close(); catch (IOException e2) { synchronized (vulture) { vulture.notify(); Michael Barth, Alexander Knapp, Philipp Meier, Axel Rauschmayer, LMU München 11

Multi-Server: Vulture public synchronized void run() { for (;;) { this.wait(5000); catch (InterruptedException e) { synchronized (server.connections) { for (int i = 0; i < server.connections.size(); i++) { Connection c; c = (Connection)server.connections.elementAt(i); if (!c.isalive()) { server.connections.removeelementat(i); i--; Michael Barth, Alexander Knapp, Philipp Meier, Axel Rauschmayer, LMU München 12