Salve a tutti, ho un problema con i socket...
Ho creato due classi (Server e Client) da utilizzare eventualmente in altri programmi.
Con il server apparentemente non ho problemi e riesco a ricevere messaggi di prova che invio con telnet, il client invece mi genera diverse eccezioni, tra cui "java.net.SocketException: Broken pipe"
Vi posto il codice del server:
e del client:codice:import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; public class Server{ ServerSocket socket; Socket connection = null; DataOutputStream out; DataInputStream in; String message; Server(int port){ try { socket = new ServerSocket(port); System.err.println("In listening..."); connection = socket.accept(); System.err.println("Connessione avvenuta con " + connection.getInetAddress().getHostName()); in = new DataInputStream(connection.getInputStream()); out = new DataOutputStream(connection.getOutputStream()); out.flush(); } catch (IOException e) { e.printStackTrace(); }} String receive(){ String msg=""; try { msg = (String)in.readLine(); } catch (IOException e) { e.printStackTrace(); } return msg;} void send(String msg) { try{ out.writeChars(msg); out.flush(); } catch(IOException ioException){ ioException.printStackTrace(); }} void close(){ try { in.close(); out.close(); socket.close(); } catch (IOException e) { e.printStackTrace(); }} public static void main(String args[]) throws InterruptedException, IOException { String ric=""; Server server = new Server(8888); System.err.println("\nCONNESSO!"); server.send("Siamo collegati!\n"); while(true){ if(ric.equals("") || ric==null || ric.equals("chiudi")) break; ric=server.receive(); System.out.println("Ho ricevuto:"+ric); } System.err.println("Chiudo la connessione"); server.close(); } }
Dove può essere il problema?codice:import java.net.*; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.Socket; public class Client{ Socket socket; DataOutputStream out; DataInputStream in; Client(String address, int port){ try { socket = new Socket(address, port); System.err.println("Connesso a "+address+" alla porta "+port); in = new DataInputStream(socket.getInputStream()); out = new DataOutputStream(socket.getOutputStream()); out.flush(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { System.err.println("Connessione rifiutata"); }} String receive(){ String msg=""; try { msg=(String)in.readLine(); } catch (IOException e) { e.printStackTrace(); }return msg; } void send(String msg) { try{ out.write(2); out.flush(); } catch(IOException ioException){ ioException.printStackTrace(); }} void close(){ try { in.close(); out.close(); socket.close(); } catch (IOException e) { e.printStackTrace();}} public static void main(String args[]) throws InterruptedException { Client client = new Client("localhost", 8888); Thread.sleep(2000); client.send("ciaoo"); Thread.sleep(2000); client.send("come"); Thread.sleep(2000); client.send("stai??"); Thread.sleep(2000); client.send("chiudi"); client.close(); } }
Grazie per l'attenzione, ciao!

Rispondi quotando