Aplicatii client-server

Slides:



Advertisements
Similar presentations
Socket Programming By Ratnakar Kamath. What Is a Socket? Server has a socket bound to a specific port number. Client makes a connection request. Server.
Advertisements

 Ce e bravenet.COM ?  De ce bravenet.COM ?  Avantaje si dezavantaje in folosirea bravenet.COm Grupa 6: Lucian-Eduard Barticel Eduard Giurgiu Iany Ionut.
În general exist ă 2 forme mari de conservare : “in situ” şi “ex situ” 1. Conservarea “ in situ” Aceast ă metod ă de conservare const ă în.
Java Sockets Source:
Java Threads A tool for concurrency. OS schedules processes Ready Running 200 Blocked A process loses the CPU and another.
1 TCP socket application Architecture of Client-Server Applications Java Socket Programming Client Application Server Application.
Java Networking -- Socket Server socket class: ServerSocket wait for requests from clients. after a request is received, a client socket is generated.
Client/Server example. Server import java.io.*; import java.net.*; import java.util.*; public class PrimeServer { private ServerSocket sSoc; public static.
Java sockets. From waiting.
System Programming Practical session 11 Multiple clients server Non-Blocking I/O.
Projekt współfinansowany przez Unię Europejską w ramach Europejskiego Funduszu Społecznego „Networking”
TCP Socket Programming CPSC 441 Department of Computer Science University of Calgary.
1 Fall 2005 Socket Programming Qutaibah Malluhi Computer Science and Engineering Qatar University.
Practicum: - Client-Server Computing in Java Fundamental Data Structures and Algorithms Margaret Reid-Miller 13 April 2004.
Object Oriented Programming in Java Lecture 16. Networking in Java Concepts Technicalities in java.
Lecture 9 Network programming. Manipulating URLs URL is an acronym for Uniform Resource Locator and is a reference (an address) to a resource on the Internet.
Networks Sockets and Streams. TCP/IP in action server ports …65535 lower port numbers ( ) are reserved port echo7 time13 ftp20 telnet23.
CS390- Unix Programming Environment CS 390 Unix Programming Environment Java Socket Programming.
VII. Sockets. 1. What is a Socket? A socket is one end-point of a two-way communication link between two programs running on the network. Socket classes.
CSC 480 Software Engineering Socket. What is Socket? A socket is one end-point of a two-way communication link between two programs running on the network.
FALL 2005CSI 4118 – UNIVERSITY OF OTTAWA1 Computer Networks & PROTOCOLS (CSI 4118) FALL 2005 Professor Robert L. Probert.
By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server Paradigm – Sockets The Java Networking Package – The InetAddress.
1 Lecture 9: Network programming. 2 Manipulating URLs URL is an acronym for Uniform Resource Locator and is a reference (an address) to a resource on.
Java Server Sockets ServerSocket : Object to listen for client connection requests Throws IOException accept() method to take the client connection. Returns.
Network Programming: Servers. Agenda l Steps for creating a server Create a ServerSocket object Create a Socket object from ServerSocket Create an input.
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Java API for distributed computing.
第十一讲 分布式编程 Socket and RMI 李庆旭. 2 本章内容提要  Socket TCP /IP protocol suit Socket A TCP Server and a TCP Client Supporting Multiple Concurrent Clients A UDP.
Threads in Java Two ways to start a thread
Source: Java Sockets Source:
Network Programming in Java CS 1111 Ryan Layer May 3, 2010
CSE 341, S. Tanimoto Java networking-
Block 15 Developing the Calculator application
CS1101: Programming Methodology Recitation 7 – Exceptions
Client-server Programming
C12 – LPO: Operatii de Intrare/iEsire cu fisiere
ACTIVITATEA 1 -,, PROFESOR IT LA PAPI’’
Căutarea şi regăsirea informaţiei.
PASII INSTALARII SISTEMULUI DE OPERARE
Căutarea şi regăsirea informaţiei.
Paxos Made Simple Autor: Puşcaş Radu George
„Networking”.
Primirea si procesarea cererilor
Gestionarea datelor stiintifice
Structura bazei de date MS Access
C14 – LPO: Client - Server CUPRINS Reţele de calculatoare:
Reflexia luminii.
Cursul 3 Cautare peste siruri problema cautarea naiva
Software product management
WebSite Social Tema 2 WebSite Social.
Web Form BuilDer Coffee Cup.
Formatarea paragrafului
Funcții C/C++ continuare
prof. mrd. Negrilescu Nicolae Colegiul National Vlaicu Voda
Apache WEB Server.
INTERNET SERVICII INTERNET.
Past Perfect Continuous
Forms (Formulare).
A great way to create a channel of communication
Functia de documentare
Folosirea de către companii a Twitter, Facebook şi LinkedIn
Configurarea, deployment-ul automat si testarea serviciilor
Implementação de Socket UDP e TCP em Java
CSI 4118 – UNIVERSITY OF OTTAWA
Programarea in limbajul Java 2004 Lecturer: Gavrila Cristian
Crearea unei aplicatii Windows Forms simple
Conectivitate in AS 3.0 Ariel Chelsau.
Harti de imagini, Cadre, Stiluri
Distributed Computing
Multiplexing/Demux.
Programming TCP Clients
Presentation transcript:

Aplicatii client-server aplicatia utilizeaza Socket-uri si Thread-uri pentru a realiza o implementare client-server. server-ul se ruleaza pe o singura masina al carei IP (in acest caz localhost) este data la clientii care se leaga la ea. mai multi clienti, de pe aceeasi masina sau de pe altele, se pot lega simultan la server, conexiunea cu fiecare client fiind un Thread separat. implementarea are doua variante de server, una simpla, ce accepta o singura comexiune si una ce foloseste Thread-uri multiple ; in ambele cazuri, terminarea unui Thdread se face prin apasarea tastei <Enter>.

Clasele aplicatiei client-server

Conceptul de Thread Thread-ul permite ca pe o singura Masina Virtuala sa avem senzatia de mai multe Masini Virtuale ce lucreaza in paralel; o aplicatie cu un singur Thread are un singur punct de intrare, metoda main() si unul singur de iesire; o aplicatie cu mai multe Thread-uri (fire de executie) are un singur punct de intrare (metoda main()) urmat de puncte multiple de intrare sau iesire din alte metode; clasele pentru suportul lucrului cu Thread-uri sunt: java.lang.Thread; java.lang.Object; limbajul Java si Masina Virtuala Java; Thread-urile se executa prin apelul metodei start() ce nu lanseaza in executie neaparat Thread-ul dar il face sigur eligibil pentru executie prin inregistrarea lui in planificatorul de thread-uri (acesta decide care Thread pe care procesor se executa); un Thred poate fi in curs de rulare sau in mai multe stari ne-executabile.

Executia si oprirea Thread-ului cand Thread-ul se executa atunci executa o metoda numita run() ce poate fi proprie sau a unui obiect; pentru ca sa execute metoda run() proprie clasa Thread trebuie derivata iar metoda run() trebuie implementata; pentru a executa metoda run() a unui alt obiect se construieste o instanta a clasei Thread iar la apelul constructorului lui Thread trebuie specificat obiectul a carui run() se porneste; din acest motiv construnctia de Thread-uri se poate face prin extinderea lui Thread si implementarea lui Runnable; la revenirea din metoda run() Thread-ul este terminat si considerat mort (nu mai poate fi repornit desi datele si metodele sunt disponibile); Thread-ul poate fi terminat fortat prin folosirea metodelor stop() si interrupt().

Starile Thread-ului running: Thread-ul are toata atentia Masinii Virtuale pentru execuria metodei run(); suspended: permite ca orice Thread (arbitrar) sa faca un al Thread sa nu fie rulat o durata de timp nedefinita; sleeping: nu face nimic si nu foloseste procesorul; blocked: asteapta un eveniment din afara Thread-ului pentru a putra efectua operatii de intrare/iesire; ready: Thread-ul este gata sa treaca in starea de running imediat ce un procesor este disponibil; dead: executia metodei run() din Thread s-a terminat; monitor: poate bloca si debloca alte Thdread-uri. fiecare Thread are o prioritate de la 1 la 10 (implicit 5), cele prioritare sunt rulate inaintea celor mai putin prioritare; planificatorul de thread-uri decide care Thtead sa-l ruleze pe baza prioritatilor acestora (metoda setPriority() permite setarea prioritatii).

Aplicatia 1 /1 Urmeaza implementarea clasei Sever ce lucreaza cu o singura conexiune. import java.net.*; import java.io.*; import java.text.SimpleDateFormat; import java.util.Calendar; public class Server { public static String now(String dateFormat) { Calendar cal = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat(dateFormat); return sdf.format(cal.getTime()); } public static void main(String[] args) throws IOException { ServerSocket serverSocket = null; //System.out.println("1"); try { serverSocket = new ServerSocket(4444); } catch (IOException e) { System.err.println("Nu se poate asculta portul: 4444."); System.exit(1); Socket clientSocket = null; //System.out.println("2"); clientSocket = serverSocket.accept(); System.err.println("Accept nereusit.");

Aplicatia 1 / 2 System.out.println("3"); PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); String inputLine, outputLine; InetAddress thisIp = InetAddress.getLocalHost(); System.out.println("Server-ul ruleaza."); while ((inputLine = in.readLine()) != null) { System.out.print("Comunic ... "); outputLine = inputLine + now(" yyyy-MM-dd HH:mm:ss ")+"IP:"+thisIp.getHostAddress(); out.println(outputLine); System.out.println(outputLine); if(inputLine.length()==0) break; } System.out.println("Server OPRIT!"); out.close(); in.close(); clientSocket.close(); serverSocket.close();

Aplicatia 1 / 3 Urmeaza implementarea clasei Client. import java.io.*; import java.net.*; public class Client { public static void main(String[] args) throws IOException { Socket echoSocket = null; PrintWriter out = null; BufferedReader in = null; try { echoSocket = new Socket("localhost", 14444); out = new PrintWriter(echoSocket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream())); } catch (UnknownHostException e) { System.err.println("Nu stiu cine este host: localhost"); System.exit(1); } catch (IOException e) { System.err.println("Nu pot efecta operatii de I/E cu: localhost."); }

Aplicatia 1 / 4 BufferedReader stdIn = new BufferedReader( new InputStreamReader(System.in)); String userInput, serverInput; while ((userInput = stdIn.readLine()) != null) { out.println(userInput); serverInput=in.readLine(); System.out.println("ecou: " + serverInput); if(userInput.length()==0) break; } out.close(); in.close(); stdIn.close(); echoSocket.close();

Aplicatia 1 / 5 Urmeaza implementarea Server-ului ce lucreaza cu conexiuni multiple. Are doua clase ServerMClient (punctul de intrare) si ServerMThread (Thread-urile). import java.net.*; import java.io.*; public class ServerMClient { public static void main(String[] args) throws IOException { ServerSocket serverSocket = null; boolean listening = true; int i=0; try { serverSocket = new ServerSocket(14444); } catch (IOException e) { System.err.println("Server - Nu se poate asculta portul: 14444."); System.exit(-1); } System.out.println("Serverul ruleaza si poate primi comenzi."); while (listening) { Runnable r=new ServerMThread(serverSocket.accept(),i); Thread t = new Thread(r); t.start(); ++i; System.out.println("Serverul este oprit."); serverSocket.close();

Aplicatia 1 / 6 import java.net.*; import java.io.*; import java.text.SimpleDateFormat; import java.util.Calendar; public class ServerMThread extends Thread { private Socket clientSocket = null; int i; public ServerMThread(Socket socket, int contor) { super("ServerMThread"); i=contor; this.clientSocket = socket; } public static String now(String dateFormat) { Calendar cal = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat(dateFormat); return sdf.format(cal.getTime()); public void run() { try { PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); String inputLine, outputLine;

Aplicatia 1 / 7 InetAddress thisIp = InetAddress.getLocalHost(); System.out.println("Thread ruleaza (running)."); while ((inputLine = in.readLine()) != null) { System.out.print("Comunicare ... " + i+" -> "); outputLine = inputLine + now(" yyyy-MM-dd HH:mm:ss ")+" IP:"+thisIp.getHostAddress(); out.println(outputLine); System.out.println(outputLine); if(inputLine.length()==0) break; } out.close(); in.close(); clientSocket.close(); System.out.println("Thread-ul" +i+" este inchis (dead)."); } catch (IOException e) { e.printStackTrace();

Bibliografie http://www.east.utcluj.ro/mb/mep/antal/downloads.html > Java: course, IDE (JDeveloper), JDK and JRE, JDeveloper labs. http://docs.oracle.com/cd/E18941_01/tutorials/jdtut_11r2_50/jdtut_11r2_50.html > Getting Started With the JDeveloper IDE