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

    Socket: non riesco a chiudere

    codice:
    /**
     * @author menphisx
     * @copy (c) author 2007/2009
     * @version 1.0b
     *
     */
    
    import java.io.*;
    import java.net.*;
    
    public class RJClient {
    
    	/**
    	 * RJC is an acronim for Remote Java Compiler.
    	 * The idea is to create a class of server that can compile whit gcc,
    	 * on every type of OS and architeture.
    	 * The clients send sources and the server compile it.
    	 */
    	
    	Socket client;
    	
    	DataInputStream serverStream;
    	DataOutputStream clientStream;
    	
    	BufferedReader stdin;
    	
    	String request;
    	String response;
    	
    	public static void main(String[] args) {
    		
    		// TODO Auto-generated method stub
    		
    		try{
    			
    			new RJClient(args[0], Integer.parseInt(args[1]));
    			
    		}
    		catch(NumberFormatException nfe){
    			
    			System.out.println("The port you typed is not a number");
    			
    		}
    
    	}
    	
    	public RJClient(String ip, int port) {
    		
    		try{
    			
    			Connect(ip, port);
    		
    		}
    		catch(IOException ioe){
    			
    			System.out.println("IOException During Connect() : " + ioe.getMessage());
    			
    		}
    		
    	}
    	
    	public void Connect(String ip, int port) throws IOException {
    		
    		client = new Socket(ip, port);
    		
    		serverStream = new DataInputStream(client.getInputStream());
    		clientStream = new DataOutputStream(client.getOutputStream());
    		stdin = new BufferedReader(new InputStreamReader(System.in));
    		
    		clientStream.writeBytes("ID:RJ");
    		
    		while(true){
    			
    			System.out.print("RJCLIENT:> ");
    			
    			String request = stdin.readLine();
    			
    			clientStream.writeBytes(request);
    			
    			response = serverStream.readLine();
    				
    			if(response.equals("SEE YOU SOON")){
    				
    				System.out.println("bye bye\n");
    				System.exit(0);
    				
    			}
    			
    			System.out.println(response);
    			
    		}
    		
    		serverStream.close();
    		clientStream.close();
    		
    		client.close();
    		
    	}
    
    }
    Non riesco a compilarlo, mi restituisce:
    codice:
    RJClient.java:80: warning: [deprecation] readLine() in java.io.DataInputStream has been deprecated
                            response = serverStream.readLine();
                                                   ^
    RJClient.java:94: unreachable statement
                    clientStream.close();
                    ^
    1 error
    1 warning



    Grazie per l'aiuto.


  2. #2
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    cambia quel System.exit(0); con un break; oppure cambia il while:

    while(true) ... lo fai diventare while(flag), definisce a monte boolean flag = true, e al posto di System.exit(0) ci metti flag = false;, insomma devi uscire dal while
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

  3. #3
    Già. Non ci avevo pensato che sciocco.
    Funge grazie

    Mi puoi dire perchè readLine è deprecated e con cosa lo sostituisco ?


  4. #4
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    tutte le cose "deprecated" hanno la sostituzione suggerita nelle API

    http://java.sun.com/javase/6/docs/ap...putStream.html
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

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.