Dsy Network www | forum | my | didattica | howto | wiki | el goog | stats | blog | dona | rappresentanti
Homepage
 Register   Calendar   Members  Faq   Search  Logout 
.dsy:it. : Powered by vBulletin version 2.3.1 .dsy:it. > Didattica > Corsi N - Z > Reti di calcolatori > [ESERCIZIO] Thread
Pages (2): « 1 [2]   Last Thread   Next Thread
Author
Thread    Expand all | Contract all    Post New Thread    Post A Reply
Collapse
yeah
.grande:maestro.

User info:
Registered: Nov 2003
Posts: 1644 (0.21 al dì)
Location: Cologno Monzese
Corso: Informatica Magistrale
Anno: II
Time Online: 12 Days, 21:36:41 [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged

Posto la mia soluzione, naturalmente non è l'unica ;)

Server - Server.java

code:
import java.net.*; import java.io.*; class Server { public static void main(String args[]) { ServerSocket listener = (ServerSocket) null; //necessario altrimenti potrebbe non essere inizializzato Socket established; try { //Apertura socket di ascolto listener = new ServerSocket(7000, 300); } catch(IOException e) {} while(true) { try { System.out.println("Server: In attesa di connessioni..."); established = listener.accept(); /* * Accettazione richiesta di connessione * e passaggio del socket creato ad un nuovo thread */ System.out.println("Server: Connessione accettata e lancio nuovo thread"); (new CliManager(established)).start(); } catch(Exception e) {} } //listener.close(); //codice irraggiungibile a causa del while(true) } }


Gestione tramite thread - CliManager.java
code:
import java.net.*; import java.io.*; import java.util.Random; class CliManager extends Thread { private Socket passedSocket; private String toSend; private OutputStream sock_out; private static Random rand; // Generatore di numeri casuali public CliManager(Socket s) throws Exception { System.out.println("CliManager: Creazione nuovo CliManager " + getName()); toSend = "Hello Net World!\n"; /* * Ricezione del socket creato dal server * e ottenimento dello stream di output */ passedSocket = s; sock_out = s.getOutputStream(); rand = new Random(); } public void run() { int slength; try { System.out.println(getName() + ": Inizio invio dati al client"); slength = toSend.length(); //Invio carattere per carattere della stringa for(int i = 0; i < slength; i++) { sock_out.write((int) toSend.charAt(i)); sleep((rand.nextInt(4) + 1) * 500); //Attesa casuale tra 0.5 e 2 secondi } System.out.println(getName() + ": Fine invio dati al client"); passedSocket.close(); } catch(Exception e) {} } }


Come client ho usato quello visto a lezione

Tuttavia non sono riuscito a mettere il 'throws' a run(), in quanto il compilatore dice che non si può porre in overload il metodo run() poichè la versione ereditata non fa il 'throws'.

__________________
?

11-10-2004 20:31
Click Here to See the Profile for yeah Click here to Send yeah a Private Message Find more posts by yeah Add yeah to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
All times are GMT. The time now is 18:02.    Post New Thread    Post A Reply
Pages (2): « 1 [2]   Last Thread   Next Thread
Show Printable Version | Email this Page | Subscribe to this Thread | Add to Bookmarks

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is ON
 

Powered by: vBulletin v2.3.1 - Copyright ©2000 - 2002, Jelsoft Enterprises Limited
Mantained by dsy crew (email) | Collabora con noi | Segnalaci un bug | Archive | Regolamento | Licenze | Thanks | Syndacate
Pagina generata in 0.052 seconds (65.40% PHP - 34.60% MySQL) con 26 query.