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
GinoPilotino
.grande:maestro.

User info:
Registered: Dec 2002
Posts: 1403 (0.17 al dì)
Location: Segrate
Corso: TI.COM
Anno: Secondo
Time Online: 13 Days, 8:59:42 [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged
Esercizio thread

Ragazzi avete provato a casa a creare la connessione client - server tramite thread e a compilarla?
Probabilmente ho preso male appunti io però non mi va. Io ho scritto il codice come segue:



in linea teorica mi sembra giusto quello che ho fatto però in fase di compilazione mi dice che il metodo run non accetta le eccezioni; se lo tolgo però succede un mezzo casino.
qualche suggerimento? a voi va?

code:
import java.net.*; import java.io.*; public class Servert { public static void main(String args[]) throws Exception { // variabili String frase="Ciao sono il server"; ServerSocket server=null; Socket servizio=null; boolean connessione=true; OutputStream canale; int i; // programma Smista t; try { server = new ServerSocket(1000); } catch(IOException e) { System.out.println("Connessione Fallita"); connessione=false; } while(connessione) { System.out.println("\n\nServer in attesa di connessione"); servizio = server.accept(); t = new Smista(servizio, frase); t.start(); } } } class Smista extends Thread { // variabili OutputStream canale; Socket serviziothread=null; String frasethread; // programma public Smista(Socket servizio, String frase) { this.serviziothread = servizio; this.frasethread = frase; } public void run() throws Exception { canale = serviziothread.getOutputStream(); for (int i=0; i<frasethread.length();i++) { canale.write((int)frasethread.charAt(i)); sleep(1000); } serviziothread.close(); System.out.println("Servizio erogato"); } }

Last edited by GinoPilotino on 09-10-2004 at 17:54

09-10-2004 16:57
Click Here to See the Profile for GinoPilotino Click Here to See the Blog of GinoPilotino Click here to Send GinoPilotino a Private Message Find more posts by GinoPilotino Add GinoPilotino to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
Collapse
0m4r
.grande:maestro.

User info:
Registered: Mar 2002
Posts: 7287 (0.87 al dì)
Location: Düsseldorf (DE)
Corso:
Anno: ESAMI FINITI
Time Online: 49 Days, 0:42:50 [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged

Off-Topic:
usa il tag {code}{/code} oviamente cone le [] al posto delle {}, il codice sarà piu leggibile

__________________
http://www.twitter.com/0m4r

09-10-2004 17:39
Click Here to See the Profile for 0m4r Click Here to See the Blog of 0m4r Click here to Send 0m4r a Private Message Find more posts by 0m4r Add 0m4r to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
Collapse
futurbaggio
U Frikkettone

User info:
Registered: Feb 2003
Posts: 774 (0.10 al dì)
Location: Foggia
Corso: TIC
Anno: 1° Magistrale
Time Online: 13 Days, 16:44:00: [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged
Re: Esercizio thread

code:
while(connessione) { System.out.println("\n\nServer in attesa di connessione"); servizio = server.accept(); t = new Smista(servizio, frase); t.start(); }


Non è che questo va dentro ad un try...catch???

Cmq se riporti proprio quello che ti restituisce il compilatore è meglio.

Roberto

__________________
E lo sceriffo americano non lo sa / ma quando parla ride tutta la città / vile fellone vieni a singolar tenzone / unza unza umpappà troppe truppe mariscià.
Non ci voleva l'eruzione della lava / e due piccioni si contendono la fava / sposa bagnata, bambina svergognata / la questione è delicata / come bolle la patata.
Basta! E' finita la festa
[L'ovile delle vanità, Folkabbestia]

09-10-2004 19:50
Click Here to See the Profile for futurbaggio Click Here to See the Blog of futurbaggio Click here to Send futurbaggio a Private Message Visit futurbaggio's homepage! Find more posts by futurbaggio Add futurbaggio to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
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

:)

Non puoi mettere a run() il throws, mi ci sono imbattuto anch'io.

Metti il codice di run() in un try-catch e ricompila :approved:

__________________
?

09-10-2004 20:34
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
Collapse
GinoPilotino
.grande:maestro.

User info:
Registered: Dec 2002
Posts: 1403 (0.17 al dì)
Location: Segrate
Corso: TI.COM
Anno: Secondo
Time Online: 13 Days, 8:59:42 [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged

Originally posted by yeah
:)

Non puoi mettere a run() il throws, mi ci sono imbattuto anch'io.

Metti il codice di run() in un try-catch e ricompila :approved:


ho seguito il tuo consiglio ( :approved: ) è ho ricostruito la classe che gestisce il thread come segue:

public void run() {
try {
canale = serviziothread.getOutputStream();
for (int i=0; i<frasethread.length();i++) {
canale.write((int)frasethread.charAt(i));
// sleep(1000);
}
serviziothread.close();
System.out.println("Servizio erogato");
}
catch(IOException e){}
}

Così com'è funziona anche se mi da un problema quando chiamo sleep che mi dice che deve essere per forza in un throws Exception.
In ogni caso non riesco a capire perchè al prof andava mettendo throws exception nel metodo run e a me no :shock:

Last edited by GinoPilotino on 10-10-2004 at 02:18

10-10-2004 02:16
Click Here to See the Profile for GinoPilotino Click Here to See the Blog of GinoPilotino Click here to Send GinoPilotino a Private Message Find more posts by GinoPilotino Add GinoPilotino to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
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

Ecco: sleep() lancia InterruptedException, che nella gerarchia sta sotto ad Exception, quindi probabilmente nella catch() ci va Exception (o InterruptedException) anzichè IOException


In ogni caso non riesco a capire perchè al prof andava mettendo throws exception nel metodo run e a me no

Boh! Mi sa che mi sono distratto a lezione e non ho visto questo particolare.

Cmq appena ho un po' di tempo posto il mio codice e vediamo cosa c'è di diverso.

:)

P.S.:
Oggi alle 03:22
:zzz:

__________________
?

Last edited by yeah on 10-10-2004 at 08:24

10-10-2004 08:14
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
Collapse
GinoPilotino
.grande:maestro.

User info:
Registered: Dec 2002
Posts: 1403 (0.17 al dì)
Location: Segrate
Corso: TI.COM
Anno: Secondo
Time Online: 13 Days, 8:59:42 [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged

ok perfetto ho fatto come hai detto tu e mettendo Exception solo nel catch funziona tutto perfettamente ;)

10-10-2004 09:17
Click Here to See the Profile for GinoPilotino Click Here to See the Blog of GinoPilotino Click here to Send GinoPilotino a Private Message Find more posts by GinoPilotino Add GinoPilotino to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
Collapse
Principessa
.fedelissimo.

User info:
Registered: May 2004
Posts: 46 (0.01 al dì)
Location:
Corso: Tecnologie dell'Informazione e della Comunicazione
Anno: Secondo
Time Online: 12 Days, 4:50:16 [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged

catch(Exception e) {
e.printStackTrace();
}

questo quanto appreso dal corso di SOD (Programmazione Concorrente in Java)

10-10-2004 13:39
Click Here to See the Profile for Principessa Click Here to See the Blog of Principessa Click here to Send Principessa a Private Message Find more posts by Principessa Add Principessa to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
Collapse
GinoPilotino
.grande:maestro.

User info:
Registered: Dec 2002
Posts: 1403 (0.17 al dì)
Location: Segrate
Corso: TI.COM
Anno: Secondo
Time Online: 13 Days, 8:59:42 [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged

si ora il codice funziona perfettamente. In ogni caso sto cercando di capire come mai al professore andava tutto bene in aula mettendo throws Exception al metodo run() del thread mentre io se lo faccio sul pc java mi smadonna dietro:? :D

10-10-2004 14:02
Click Here to See the Profile for GinoPilotino Click Here to See the Blog of GinoPilotino Click here to Send GinoPilotino a Private Message Find more posts by GinoPilotino Add GinoPilotino to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
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

Originally posted by Principessa
catch(Exception e) {
e.printStackTrace();
}


:schoked:

:) Quello fa si che in caso si verifichi una eccezione, la Java Virtual Machine stampi lo stack delle chiamate (cioè quale funzione ha chiamato quale funzione che ha chiamato quella che ha generato l'eccezione)

Quindi diciamo che è buona norma metterlo: :approved:

__________________
?

Last edited by yeah on 10-10-2004 at 14:12

10-10-2004 14:03
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
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

[edit] ma che ancora? Stavolta non ho postato! Stavo editanto, poi sono tornato indietro e tac! Doppio post. Bug?

__________________
?

Last edited by yeah on 10-10-2004 at 14:13

10-10-2004 14:07
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
Collapse
Dodo
lunatico

User info:
Registered: Jul 2002
Posts: 2340 (0.28 al dì)
Location: in una fibra ottica
Corso: Com Dig
Anno: 3+1 :P
Time Online: 42 Days, 7:55:56 [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged

Originally posted by yeah
[edit] ma che ancora? Stavolta non ho postato! Stavo editanto, poi sono tornato indietro e tac! Doppio post. Bug?

Off-Topic:

http://www.dsy.it/forum/announcement.php?forumid=7

__________________
E' meglio essere ottimisti ed avere torto piuttosto che pessimisti ed avere ragione.

Albert Einstein

10-10-2004 14:23
Click Here to See the Profile for Dodo Click Here to See the Blog of Dodo Click here to Send Dodo a Private Message Find more posts by Dodo Add Dodo to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
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

Visto che hai quotato il mio post, spero almeno tu l'abbia letto :-/

Non ho postato, stavo editando. Invece che inviare le modifiche ho cliccato su "Indietro" nel browser :)

__________________
?

10-10-2004 14:55
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
Collapse
0m4r
.grande:maestro.

User info:
Registered: Mar 2002
Posts: 7287 (0.87 al dì)
Location: Düsseldorf (DE)
Corso:
Anno: ESAMI FINITI
Time Online: 49 Days, 0:42:50 [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged

Originally posted by GinoPilotino
si ora il codice funziona perfettamente. In ogni caso sto cercando di capire come mai al professore andava tutto bene in aula mettendo throws Exception al metodo run() del thread mentre io se lo faccio sul pc java mi smadonna dietro:? :D


io non ho visto il codice che avete usato a lezione, ma forse posso azzardare perchè al lui funzionava e a te no.

Usando throws l'eccezione viene sollevata e lasciata da gestire al metodo chiamante. Quindi probabilmente, nel cosice del professore, il metodo che solleva l'eccezione (cioè quello con throws) era racchiuso in un try/catch o a sua volta aveva un throws che rimandava la gestione dell'eccezione al suo metodo chiamante. Forse non è chiarissimo, ma più o meno si capisce dai...

__________________
http://www.twitter.com/0m4r

10-10-2004 16:27
Click Here to See the Profile for 0m4r Click Here to See the Blog of 0m4r Click here to Send 0m4r a Private Message Find more posts by 0m4r Add 0m4r to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
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

Farò delle prove, ma se la memoria non m'inganna (il che non è da escludersi ;)) quando l'ho fatto in Silab mi diceva qualcosa del tipo che run() non può fare il throws, perchè è un metodo in overload. Proverò e comunicherò.

__________________
?

Last edited by yeah on 11-10-2004 at 20:32

11-10-2004 01:11
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 02:25.    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.121 seconds (81.57% PHP - 18.43% MySQL) con 28 query.