Visualizzazione dei risultati da 1 a 3 su 3
  1. #1

    scambio messaggi client-server

    Salve,

    allora

    SERVER

    codice:
    class server2 {
        
        
        public static void main(String args[]) throws IOException {
            String data = "Welcome to My Server"; //welcome message
            String data1 = "enter username"; //welcome message
            String message,servermessage;
    
            int idstats1 = 0; //verifyed username
            int idstats2 = 0; //verifyed password 
            int verify1 = 0;
            try {
                //Detecting the localhost's ip address
                InetAddress localaddr = InetAddress.getLocalHost();
                System.out.println ("Local IP Address : " + localaddr );
                System.out.println ("Local hostname : " + localaddr.getHostAddress());
    
                //Creating a server socket for connection
                ServerSocket srvr = new ServerSocket(1234);
                System.out.println("Waiting for connection on "+localaddr);
                
                
    while(true){
                //Accept incoming connection
                Socket skt = srvr.accept();
                System.out.print("Server has connected!\n");
    
                //get Input and Output streams
                PrintWriter out = new PrintWriter(skt.getOutputStream(), true);
                out.flush();
                BufferedReader in = new BufferedReader(new InputStreamReader(skt.getInputStream()));
                
                
                System.out.print("Sending string: '" + data + "'\n");
                out.println(data); //sends welcome message
                System.out.print("Sending string: '" + data1 + "'\n");
                out.println(data1); //sends welcome message
    
                
               
                
                 
                  
                  
    }
            } catch(BindException e) {
                e.printStackTrace();
                System.out.print("A server is already running on the same port.");        
            } catch(SocketException e) {
                e.printStackTrace();
                System.out.print("Client has disconnected rudely.");
            } catch(Exception e) {
                e.printStackTrace();
                System.out.print(e);    
            }
        }}

    CLIENT

    codice:
    class LongClient {
        public static void main(String args[]) {
            
            
            String data="ciao";
            String data1="ciao2";
            try {
                
                
                Socket skt = new Socket("localhost", 1234);
                BufferedReader in = new BufferedReader(new InputStreamReader(skt.getInputStream()));
                
                            BufferedWriter in2 = new BufferedWriter(new OutputStreamWriter(skt.getOutputStream()));
                            
    
                Scanner kbReader = new Scanner(System.in);
                PrintWriter out = new PrintWriter(skt.getOutputStream(), true);
                out.flush();
    
                String message, servermessage;
                InetAddress clientip = InetAddress.getLocalHost();
                System.out.println("Preparing to chat...");
                out.println("Client IP Address: " + clientip); // sends message to client giving ip
                out.println("Client hostname: " + clientip.getHostAddress()); // send message to client giving computer name
    
                
                
                
                    if (in.ready()) {
                        servermessage = in.readLine();
                        System.out.println("server>: " + servermessage);
                        
                     
                    }
     if (in.ready()) {
                        servermessage = in.readLine();
                        System.out.println("server>: " + servermessage);
                        
                     
                    }
     
     
     
      ServerSocket srvr = new ServerSocket(1234);
                
                
                //Accept incoming connection
                Socket skt2 = srvr.accept();
                System.out.print("Server has connected!\n");
    
                //get Input and Output streams
                
                out.flush();
                
                
                System.out.print("Sending string: '" + data + "'\n");
                out.println(data); //sends welcome message
                    message = kbReader.nextLine();
                    out.println(message);
    
                    out.println("Done");
    
                    //message="bye";
                    //out.println("bye");
                    Thread.currentThread().sleep(300);
                
                
                
                    //get Input and Output streams
               
                
                
                
                
                
                
                
                System.out.print("Sending string: '" + data + "'\n");
                out.println(data); //sends welcome message
                System.out.print("Sending string: '" + data1 + "'\n");
                out.println(data1); //sends welcome message
    
                in2.write(data, 200, 200);
                
    //reads the line typed in
                
                
    
                out.close();
                in.close();
            } catch(Exception e) {
                System.out.print(e);
            }
        } 
    }
    Ora il mio problema e che come fa il client ad inviare messaggi al server.

    Il server invia correttamente i due messaggi il client ma non riesco a inviare i messagi dal client al server. Come posso risolvere?

  2. #2
    Quote Originariamente inviata da signfra Visualizza il messaggio
    Salve,

    allora

    SERVER

    codice:
    class server2 {
        
        
        public static void main(String args[]) throws IOException {
            String data = "Welcome to My Server"; //welcome message
            String data1 = "enter username"; //welcome message
            String message,servermessage;
    
            int idstats1 = 0; //verifyed username
            int idstats2 = 0; //verifyed password 
            int verify1 = 0;
            try {
                //Detecting the localhost's ip address
                InetAddress localaddr = InetAddress.getLocalHost();
                System.out.println ("Local IP Address : " + localaddr );
                System.out.println ("Local hostname : " + localaddr.getHostAddress());
    
                //Creating a server socket for connection
                ServerSocket srvr = new ServerSocket(1234);
                System.out.println("Waiting for connection on "+localaddr);
                
                
    while(true){
                //Accept incoming connection
                Socket skt = srvr.accept();
                System.out.print("Server has connected!\n");
    
                //get Input and Output streams
                PrintWriter out = new PrintWriter(skt.getOutputStream(), true);
                out.flush();
                BufferedReader in = new BufferedReader(new InputStreamReader(skt.getInputStream()));
                
                
                System.out.print("Sending string: '" + data + "'\n");
                out.println(data); //sends welcome message
                System.out.print("Sending string: '" + data1 + "'\n");
                out.println(data1); //sends welcome message
    
                
               
                
                 
                  
                  
    }
            } catch(BindException e) {
                e.printStackTrace();
                System.out.print("A server is already running on the same port.");        
            } catch(SocketException e) {
                e.printStackTrace();
                System.out.print("Client has disconnected rudely.");
            } catch(Exception e) {
                e.printStackTrace();
                System.out.print(e);    
            }
        }}

    CLIENT

    codice:
    class LongClient {
        public static void main(String args[]) {
            
            
            String data="ciao";
            String data1="ciao2";
            try {
                
                
                Socket skt = new Socket("localhost", 1234);
                BufferedReader in = new BufferedReader(new InputStreamReader(skt.getInputStream()));
                
                            BufferedWriter in2 = new BufferedWriter(new OutputStreamWriter(skt.getOutputStream()));
                            
    
                Scanner kbReader = new Scanner(System.in);
                PrintWriter out = new PrintWriter(skt.getOutputStream(), true);
                out.flush();
    
                String message, servermessage;
                InetAddress clientip = InetAddress.getLocalHost();
                System.out.println("Preparing to chat...");
                out.println("Client IP Address: " + clientip); // sends message to client giving ip
                out.println("Client hostname: " + clientip.getHostAddress()); // send message to client giving computer name
    
                
                
                
                    if (in.ready()) {
                        servermessage = in.readLine();
                        System.out.println("server>: " + servermessage);
                        
                     
                    }
     if (in.ready()) {
                        servermessage = in.readLine();
                        System.out.println("server>: " + servermessage);
                        
                     
                    }
     
     
     
      ServerSocket srvr = new ServerSocket(1234);
                
                
                //Accept incoming connection
                Socket skt2 = srvr.accept();
                System.out.print("Server has connected!\n");
    
                //get Input and Output streams
                
                out.flush();
                
                
                System.out.print("Sending string: '" + data + "'\n");
                out.println(data); //sends welcome message
                    message = kbReader.nextLine();
                    out.println(message);
    
                    out.println("Done");
    
                    //message="bye";
                    //out.println("bye");
                    Thread.currentThread().sleep(300);
                
                
                
                    //get Input and Output streams
               
                
                
                
                
                
                
                
                System.out.print("Sending string: '" + data + "'\n");
                out.println(data); //sends welcome message
                System.out.print("Sending string: '" + data1 + "'\n");
                out.println(data1); //sends welcome message
    
                in2.write(data, 200, 200);
                
    //reads the line typed in
                
                
    
                out.close();
                in.close();
            } catch(Exception e) {
                System.out.print(e);
            }
        } 
    }
    Ora il mio problema e che come fa il client ad inviare messaggi al server.

    Il server invia correttamente i due messaggi il client ma non riesco a inviare i messagi dal client al server. Come posso risolvere?
    Per far si che il client invia messaggi il server ho utilizzato il

    codice:
                       
     BufferedWriter in2 = new BufferedWriter(new OutputStreamWriter(skt.getOutputStream()));
                Socket skt = new Socket("localhost", 1234);
    
    
                in2.write("ciao", 200, 200);
    ed nel server ho utilizzato il BufferedReader ed evidentemente non funziona

  3. #3
    Quote Originariamente inviata da signfra Visualizza il messaggio
    Per far si che il client invia messaggi il server ho utilizzato il

    codice:
                       
     BufferedWriter in2 = new BufferedWriter(new OutputStreamWriter(skt.getOutputStream()));
                Socket skt = new Socket("localhost", 1234);
    
    
                in2.write("ciao", 200, 200);
    ed nel server ho utilizzato il BufferedReader ed evidentemente non funziona

    La cosa strana e se nel server mado una stringa con
    codice:
    out.println(data);
    il client la riceve con

    codice:
       
                  if (in.ready()) {
                        servermessage = in.readLine();
                        System.out.println("server>: " + servermessage);
                        
                     
                    }
    mentre poi dopo aver ricevuto questo messaggio faccio la prova a inviare con out.println("prova") il server con

    codice:
     if (in.ready()) {
                        servermessage = in.readLine();
                        System.out.println("client>: " + servermessage);
                        
                     
                    }

    il server non riceve nessun messaggio, come mai?

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.