|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Senior Member
Iscritto dal: Jun 2002
Città: Milano
Messaggi: 959
|
server e client in Java...Help
allora ho questo server...
Codice:
import java.io.*; import java.net.*; public class server { public static void main(String args[]) { ServerSocket echoServer = null; String line; DataInputStream is; Socket clientSocket = null; PrintStream os = null; try { echoServer = new ServerSocket(9999); } catch (IOException e) { System.out.println(e); } try { clientSocket = echoServer.accept(); is = new DataInputStream(clientSocket.getInputStream()); os = new PrintStream(clientSocket.getOutputStream()); while (true) { line = is.readLine(); System.out.print(line); os.println("c"); } } catch (IOException e) { System.out.println(e); } } } Codice:
import java.io.*; import java.net.*; import javax.swing.*; public class client1 { public static void main(String[] args) { Socket smtpSocket = null; PrintStream os = null; DataInputStream is = null; String mess; try { smtpSocket = new Socket("localhost", 9999); os = new PrintStream(smtpSocket.getOutputStream()); is = new DataInputStream(smtpSocket.getInputStream()); } catch (UnknownHostException e) { System.err.println("Don't know about host: hostname"); } catch (IOException e) { System.err.println("Couldn't get I/O for the connection to: hostname"); } if (smtpSocket != null && os != null && is != null) { try { os.println("ciao"); String responseLine; while ((responseLine = is.readLine()) != null) { System.out.println("Server: " + responseLine); if (responseLine.indexOf("Ok") != -1) { break; } } os.close(); is.close(); smtpSocket.close(); } catch (UnknownHostException e) { System.err.println("Trying to connect to unknown host: " + e); } catch (IOException e) { System.err.println("IOException: " + e); } } } } grazie |
![]() |
![]() |
![]() |
#2 |
Senior Member
Iscritto dal: Jul 1999
Città: Torino
Messaggi: 2221
|
Il codice mi pare corretto... Personalmente l'ho provato e funziona... che jdk usi?? Che Sistema Operativo?
|
![]() |
![]() |
![]() |
#3 | |
Senior Member
Iscritto dal: Jun 2002
Città: Milano
Messaggi: 959
|
Quote:
infatti anche a me sembra corretto... non è che c'è un bug in questa versione???dubito però... |
|
![]() |
![]() |
![]() |
#4 |
Member
Iscritto dal: May 2003
Messaggi: 119
|
Lo ho compilato e mi funziona
Cmq con qella versione il metodo readLine() di java.io.DataInputStream è deprecato. Prova a sostituirlo. Nel tutorial delle API di Sun c'è scritto di sostituire DataInputStream d = new DataInputStream(in); con BufferedReader d = new BufferedReader(new InputStreamReader(in)); Ultima modifica di Molz : 12-11-2003 alle 14:03. |
![]() |
![]() |
![]() |
#5 |
Senior Member
Iscritto dal: Jul 1999
Città: Torino
Messaggi: 2221
|
Ovviamente lanci il client e il server da due console separate giusto?
|
![]() |
![]() |
![]() |
#6 | |
Senior Member
Iscritto dal: Jun 2002
Città: Milano
Messaggi: 959
|
Quote:
|
|
![]() |
![]() |
![]() |
#7 | |
Senior Member
Iscritto dal: Jun 2002
Città: Milano
Messaggi: 959
|
Quote:
![]() |
|
![]() |
![]() |
![]() |
#8 |
Senior Member
Iscritto dal: Jul 1999
Città: Torino
Messaggi: 2221
|
firewall??
|
![]() |
![]() |
![]() |
#9 | |
Senior Member
Iscritto dal: Jun 2002
Città: Milano
Messaggi: 959
|
Quote:
![]() ![]() non riesco proprio a capire perchè non funziona ho riprovato ancora con BufferedReader ma non va... |
|
![]() |
![]() |
![]() |
#10 |
Senior Member
Iscritto dal: Jul 1999
Città: Torino
Messaggi: 2221
|
Non so, prova a cambiare porta! e mettere della System.out di debug x capire qual è il pezzo che nn esegue...
|
![]() |
![]() |
![]() |
#11 | |
Senior Member
Iscritto dal: Jun 2002
Città: Milano
Messaggi: 959
|
Quote:
![]() ![]() ![]() |
|
![]() |
![]() |
![]() |
#12 |
Senior Member
Iscritto dal: Jul 1999
Città: Torino
Messaggi: 2221
|
Una cosa del genere:
Codice:
import java.io.*; import java.net.*; import javax.swing.*; public class MyClient { public static void main(String[] args) { Socket smtpSocket = null; PrintStream os = null; DataInputStream is = null; String mess; try { smtpSocket = new Socket("xxxx", 10000); os = new PrintStream(smtpSocket.getOutputStream()); is = new DataInputStream(smtpSocket.getInputStream()); } catch (UnknownHostException e) { System.err.println("Don't know about host: hostname"); } catch (IOException e) { System.err.println("Couldn't get I/O for the connection to: hostname"); } if (smtpSocket != null && os != null && is != null) { try { os.println("Messaggio al server: mi sono collegato!"); String responseLine; while ((responseLine = is.readLine()) != null) { System.out.println("Server: " + responseLine); if (responseLine.indexOf("Ok") != -1) { break; } } os.close(); is.close(); smtpSocket.close(); } catch (UnknownHostException e) { System.err.println("Trying to connect to unknown host: " + e); } catch (IOException e) { System.err.println("IOException: " + e); } } } } Codice:
import java.io.*; import java.net.*; public class MyServer { public static void main(String args[]) { ServerSocket echoServer = null; String line; DataInputStream is; Socket clientSocket = null; PrintStream os = null; try { echoServer = new ServerSocket(9999); System.out.println("Creato il ServerSocket: " + echoServer); } catch (IOException e) { System.out.println(e); } try { clientSocket = echoServer.accept(); System.out.println("Creato il ClientSocket: " + clientSocket); is = new DataInputStream(clientSocket.getInputStream()); os = new PrintStream(clientSocket.getOutputStream()); while (true) { line = is.readLine(); System.out.print(line); os.println("Benvenuto client " + clientSocket); } } catch (IOException e) { System.out.println(e); } } } |
![]() |
![]() |
![]() |
#13 |
Senior Member
Iscritto dal: Jun 2002
Città: Milano
Messaggi: 959
|
questo è il client Server: Benvenuto client Socket[addr=/indirizzoIp,port=1365,localport=9999]
e questo il server Creato il ServerSocket: ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=9999] Creato il ClientSocket: Socket[addr=/indirizzoIp,port=1365,localport=9999] Messaggio al server: mi sono collegato! quindi sembrerebbe funzionare! perchè il mio non andava?! |
![]() |
![]() |
![]() |
#14 |
Senior Member
Iscritto dal: Jul 1999
Città: Torino
Messaggi: 2221
|
Veramente io ho copiato ed incollato il tuo aggiungendo delle System.out
![]() |
![]() |
![]() |
![]() |
#15 | |
Senior Member
Iscritto dal: Jun 2002
Città: Milano
Messaggi: 959
|
Quote:
![]() vabbè tutto è bene ciò che finisce bene ![]() grazie! |
|
![]() |
![]() |
![]() |
#16 |
Senior Member
Iscritto dal: Jun 2002
Città: Milano
Messaggi: 959
|
due domande
![]() allora ho provato a fare una cosa così line = is.readLine(); if (line == "ciao"){ System.out.print(line); os.println("ciao client "); } però nn funziona...come mai?non posso fare il confronto fra stringhe in questa maniera?? un'altra cosa...come mai se io vado a inviare con os.println quando lo stampo dal server nn va a capo?? grazie |
![]() |
![]() |
![]() |
#17 |
Senior Member
Iscritto dal: Jun 2002
Città: Milano
Messaggi: 959
|
UP
![]() |
![]() |
![]() |
![]() |
#18 | |
Senior Member
Iscritto dal: Jul 2002
Città: Milano
Messaggi: 19148
|
Quote:
s.equals("ciao") restituisce true se la variabile s è "ciao" e false negli altri casi |
|
![]() |
![]() |
![]() |
#19 |
Senior Member
Iscritto dal: Jul 1999
Città: Torino
Messaggi: 2221
|
Scusa non mi connettevo da un po'! La soluzione ti è stata già postata... Il tuo è un errore comune, non preoccuparti
![]() La classe String offre numerosi metodi, dai un occhio alla documentazione (se l'avessi fatto non avresti postato questa domanda ![]() Approfitto per spiegarti perchè non funziona il confronto con ==. String mia_stringa = "Marco". quando fai questa dichiarazione viene allocata una zona di memoria con un certo indirizzo, puntata da mia_stringa. Praticamente mia:stringa è una variabile che contiene un valore numerico che indica la zona di memoria che contiene "Marco". Se tu fai il cofronto mia_stringa == "Marco" ti darà quindi sempre false! Lo stesso se dichiari String mia_stringa2 = "Marco" e poi fai il confronto mia_stringa2 == mia_stringa diverso se tu dichiari: String mia_stringa2 = mia_stringa. Questo perchè assegni al puntatore mia_stringa2 lo stesso calore che contiene mia_stringa e punteranno alla stessa zona di memoria. |
![]() |
![]() |
![]() |
#20 |
Senior Member
Iscritto dal: Jun 2002
Città: Milano
Messaggi: 959
|
avevo già provato con equals...ma nn va...
Codice:
while (true) { line = is.readLine(); if ( line.equals("ciao") == true){ System.out.print(line); os.println("Benvenuto client " + clientSocket); } } dove sbaglio?? ![]() |
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 19:50.