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 > [PROBLEMA] Codice Java
  Last Thread   Next Thread
Author
Thread    Expand all | Contract all    Post New Thread    Post A Reply
Collapse
Verce
.amico.

User info:
Registered: Dec 2003
Posts: 30 (0.00 al dì)
Location: Biella
Corso: Comunicazione Digitale
Anno: 2
Time Online: 18:10:32 [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged
Codice Java

Ciao a tutti, ho un problema con questo pezo di codice...
In pratica x esercitarmi volevo creare un sistema che leggeva i dati da un client e glieli rispediva (una specie di base per una chat) ma ho un problema che non riesco a risolvere, in pratica il server dopo aver letto ciò che gli manda il client (i System.out sono per il debug) si blocca (e non so perchè) dentro il while della 'public static void In' se si fa girare il programa la cosa si nota in quanto non stampa la frase ("ora ritorno: " + r)
Non so che errore ho fatto.. qualcuno sa aiutarmi?
Posto quà sotto il codice..


import java.net.*;
import java.io.*;

class Ser extends Thread{

public static void main(String args[]) throws Exception {

ServerSocket s=(ServerSocket)null;
String frase;

try {
s = new ServerSocket(4500,300);
} catch (IOException e) { }

while (true) {
try {

Socket User;
User=s.accept();
System.out.println("son connesso!");
frase=In(User);
System.out.println(frase);
Out(User,frase);

} catch (IOException e) { }
}
}

public static void Out(Socket s,String r) throws Exception {
int slength;
OutputStream sOut;
sOut=s.getOutputStream();
slength = r.length();
for (int i=0; i<slength; i++) {
sOut.write((int)r.charAt(i));
}
}

public static String In(Socket s) throws Exception{
int c;
String r="";
InputStream sIn=s.getInputStream();
while ((c = sIn.read()) != 13) {
r = r + (char)c;
System.out.println("Server: " + r);
}
System.out.println("ora ritorno: " + r);
return r;
}
}

-----------------------------------------------------------------------------

import java.net.*;
import java.io.*;

class Cli extends Thread {

public static void main(String args[]) throws Exception {

Socket s;
int c;

String frase="";

s = new Socket("localhost",4500);

while (!frase.equals("exit")) {
while ((c=System.in.read()) != 13) {
frase=frase + (char)c;
}
Out(s,frase);
System.out.println("ho mandato: " + frase);
System.out.println(In(s));
}
s.close();
}


public static void Out(Socket s,String r) throws Exception {
int slength;
OutputStream sOut;
sOut=s.getOutputStream();
slength = r.length();
for (int i=0; i<slength; i++) {
sOut.write((int)r.charAt(i));
}
System.out.println("ho scritto: " + r);
}

public static String In(Socket s) throws Exception {
int c;
String r="";
InputStream sIn=s.getInputStream();
while ((c = sIn.read()) != -1) {
r = r + (char)c;
}
return r;
}
}

25-01-2005 21:59
Click Here to See the Profile for Verce Click here to Send Verce a Private Message Find more posts by Verce Add Verce 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

Per favore, usa i tag {code}{/code} (con parentesi quadre), l'assenza di indentazione mi confonde :D

Ora ci dò una occhiata...

[edit]

(Non testato)

Cambia

code:
while ((c = sIn.read()) != 13) {

in
code:
while ((c = sIn.read()) != 10) {


:)

__________________
?

Last edited by yeah on 25-01-2005 at 22:11

25-01-2005 22:05
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

oppure, invece di 10 o 13, usa
- '\r' che sta per carride return
- '\n' che sta per new line

che poi è lo stesso, ma a me sembra più "intuitivo"...

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

25-01-2005 22:13
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
Verce
.amico.

User info:
Registered: Dec 2003
Posts: 30 (0.00 al dì)
Location: Biella
Corso: Comunicazione Digitale
Anno: 2
Time Online: 18:10:32 [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged

Ok, cambiando 13 con 10 ho risolto il problema!
Beh ora si blocca il passo dopo.. ma dovrei riuscire a risolvermelo da solo :cool:

27-01-2005 17:14
Click Here to See the Profile for Verce Click here to Send Verce a Private Message Find more posts by Verce Add Verce to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
All times are GMT. The time now is 10:37.    Post New Thread    Post A Reply
  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.033 seconds (77.00% PHP - 23.00% MySQL) con 26 query.