|
|
|
|
| |
|
Verce |
Codice Java |
25-01-2005 21:59 |
|
|
Verce |
.amico.
Registered: Dec 2003
Posts: 30 (0.00 al dì)
Location: Biella
Corso: Comunicazione Digitale
Anno: 2
Time Online: 18:10:32 [...]
Status: Offline
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 |
|
|
| |
|
yeah |
Per favore, usa i tag {code}{/code} (con parentesi ... |
25-01-2005 22:05 |
|
|
yeah |
.grande:maestro.
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
Edit | Report | IP: Logged |
Per favore, usa i tag {code}{/code} (con parentesi quadre), l'assenza di indentazione mi confonde
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 |
|
|
| |
|
0m4r |
oppure, invece di 10 o 13, usa
... |
25-01-2005 22:13 |
|
|
0m4r |
.grande:maestro.
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
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 |
|
|
| |
|
Verce |
Ok, cambiando 13 con 10 ho risolto il problema!
... |
27-01-2005 17:14 |
|
|
Verce |
.amico.
Registered: Dec 2003
Posts: 30 (0.00 al dì)
Location: Biella
Corso: Comunicazione Digitale
Anno: 2
Time Online: 18:10:32 [...]
Status: Offline
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
|
27-01-2005 17:14 |
|
|
| |
|
All times are GMT. The time now is 10:37. |
|
|
|
|
|
|
|
| |
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
|
|
|
|
|
|