Dunque ho un'applicazione client server fatta con i socket.
Il mio problema è questo:
Ogni secondo il client invia in automatico al server due double però poichè il client ha un'interfaccia grafica il client può anche inviare al server degli interi seguiti da delle stringhe.
Io non so come gestirli sul server. Il server che ho fatto è questo:
tulipancodice:public class Server { private static int port=2000; private static int T=0; private Socket client; private DataInputStream input; private DataOutputStream output; private String idFile="2000"; private boolean isDigit(String n) throws NumberFormatException{ try{ Integer.parseInt(n); return true; }catch(NumberFormatException nfe){ return false; } } public Server(int port){ this.port=port; } private void Autenticate(){ SecurityService s=new SecurityService(); try{ int c=input.readInt(); System.out.println("idship= "+c); boolean risposta=s.login(c); if (risposta){ System.out.println("OK autenticazione riuscita"); output.writeUTF("ACK"); } else output.writeUTF("NAK"); output.flush(); }catch(IOException ioex){ System.err.println(ioex.getMessage()); } } public void Start() throws IOException { int c=-1; try{ System.out.println ("Server in partenza sulla porta " + port); ServerSocket server = new ServerSocket(port); System.out.println ("Server partito sulla porta " + server.getLocalPort() ) ; while(true){ System.out.println ("In attesa di connessioni..."); client = server.accept (); System.out.println ("Richiesta di connessione da " + client.getInetAddress ()); input =new DataInputStream(new BufferedInputStream (client.getInputStream())); output = new DataOutputStream( new BufferedOutputStream(client.getOutputStream())); do{ try{ c=input.readInt(); switch(c){ case 10: output.writeUTF("OK"); output.flush(); Autenticate(); case 30: System.out.println("Client terminate"); client=null; default: ; } }catch(IOException ioex){ System.err.println(ioex.getMessage()); } }while(!(c==30)); input.close(); output.close(); } } catch(EOFException ex){ System.out.println("Client ha terminato la connessione"); }catch(IOException ioe){ System.err.println(ioe.getMessage()); } } }

Rispondi quotando