Alex'87 innanzitutto grazie per la risposta, piú gente risponde piú facile trovare una soluzione. In secondo luogo, scusa se ho urtato la tua sensibilitá di programmatore, come detto in un precedente post ieri, sono nuovo alla programmazione ed a Java e cerco di dare piú informazioni possibili nei miei post, COMUNQUE non mi sembra il caso di inca22arsi tanto per un post (
Cosa c'entra il fatto che i metodi siano private?
é solo un post! per diammine ci sono cose ben piú importanti in questo mondo!
Comunque a parte la polemica, ritorno con il mio questito originario, perché ho chiesto al mio tutor e lui sostiene che un modo c'è.
Quindi per evitare ulteriori fraintendimenti ecco il codice, praticamente è un client FTP che apre una connessione con il server e quindi deve (nel metodo doClose) chiudere la connessione, il mio problema é che io sono riuscito a chiuderla solo modificando la dichiarazione di Socket, Input ed Output stream.
Nel codice potete cercare per Problema che ho per trovare i due metodi con cui ho problemi.
Come sempre grazie a tutti per l'aiuto e le risposte (anche a te Alex anche se ti incazzi ;-) .
Ciao
Strider
:
codice:
/*
* FTPClient.java - skeleton class
*
*/
package client;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
public class FTPClient
{
private static final int PORT = 8198;
private static final String EOF = "EOF";
private final Scanner keyboard;
private BufferedReader fromServer;
private PrintWriter toServer;
private String host;
private boolean connected; //are we currently connected to server?
private String clientText; //text client entered from keyboard
private String command; //the last client command entered (e.g., DIR)
private String args; //anything after the command (its arguments)
private boolean cont; //keep the client running if true
/**************************** utilizzando queste dichiarazioni funziona *****************************/
// Streams used for connecting to server
//private InputStream is;
//private OutputStream os;
// Socket to server
//private Socket socket;
/**********************************************fine*******************************/
// Sring for messages coming from the server
private String messageFromServer;
//provided constructor
public FTPClient()
{
host = "localhost";
connected = false;
keyboard = new Scanner(System.in);
cont = true;
}
/* **************** Methods to process the individual commands *****************/
private void doOpen()
{
String hS;
if (noArgs())
{
hS = args;
}
else
{
hS = host;
}
//check for connection status
if (!connected)
{
System.out.println("Trying to open connection to: " + hS);
//try to open a connection to the host
try
{
openConnection(hS);
connected = true;
// Send Command to server
sendToServer("OPEN");
sendToServer("Hello from the FTPClient");
//display the server response
displayServerResponse();
}
catch (IOException e)
{
System.out.println("Trouble contacting the server " + e);
}
}
else
{
System.out.println("Connection to " + hS + " already open!");
}
}
// Prolema che ho parte 1 Metodo che non riesco a usare se altero OpenConnection(String
//toHost)
private void doClose()
{
try
{
sendToServer("CLOSE");
// Close Streams to server
closeStreams();
// Close socket connection to server
socket.close();
System.out.println("Connection to Server closed");
}
catch(IOException e)
{
System.out.println("Probelms by closing of the connection: " + e);
}
}
private void closeStreams() throws IOException
{
fromServer.close();
is.close();
connected = false;
}
//BYE implies that we no longer want to continue this session.
//If connected, we also close the connection to the server
//This is a provided method
private void doBye()
{
cont = false; //the only case where we don't continue
if (connected)
{
//Server does not understand BYE
clientText = "CLOSE";
doClose();
}
else
{
System.out.println("Bye bye...");
}
}
/* ************************ Other helper methods *****************************/
//This method sets the clientText variable, which contains
//everything the client typed at the command line.
//It also sets the value of command - the first thing on the line of client text
//and args - anything else on the line after the command.
//Note that we send the clientText to the server when required,
//rather than the command and args, which are for local use.
private void setClientText()
{
clientText = keyboard.nextLine().trim();
Scanner sc = new Scanner(clientText);
if (sc.hasNext())
{
command = sc.next().toUpperCase(); //allow lowercase
}
else
{
command = "";
}
if (sc.hasNext())
{
args = sc.nextLine().trim(); //trim required using this approach
}
else
{
args = ""; //empty
}
}
//this method is provided
private void openConnection(String host) throws IOException
{
/**************************** utilizzando queste dichiarazioni funziona *****************************/
//socket = new Socket(host, PORT);
//is = socket.getInputStream();
//os = socket.getOutputStream();
/**************************** Problema che ho *****************************/
Socket socket = new Socket(host, PORT);
InputStream is = socket.getInputStream();
OutputStream os = socket.getOutputStream();
fromServer = new BufferedReader(new InputStreamReader(is));
toServer = new PrintWriter(os, true);
}
//Send a string to the server.
private void sendToServer(String text)
{
toServer.println(text);
}
//Display all feedback from the server up to EOF
private void displayServerResponse()
{
try
{
messageFromServer = fromServer.readLine();
while(messageFromServer != null && !messageFromServer.equals("EOF"))
{
System.out.println(messageFromServer);
messageFromServer = fromServer.readLine();
}
}
catch (IOException e)
{
System.out.println("Trouble while getting reply from server: " + e);
}
}
//Returns true if there is nothing that looks like an argument
//after the client's command, based on the current args value
private boolean noArgs()
{
return (args == null || "".equals(args));
}
//this is not very extensible, but is simple to follow
private void processCommand()
{
if ("OPEN".equals(command))
{
doOpen();
}
else if ("LIST".equals(command))
{
doList();
}
else if ("DIR".equals(command))
{
doDir();
}
else if ("PUT".equals(command))
{
doPut();
}
else if ("CLOSE".equals(command))
{
doClose();
}
else if ("BYE".equals(command))
{
doBye();
}
else
{
System.out.println("Command " + command + " is not supported.");
}
}
/************* The main method used by the client application ********/
//execute a command read from the keyboard
public void run()
{
System.out.println("ready for input - use OPEN to connect to server ");
System.out.println("To end the session use BYE");
System.out.println("-------------------------------------------------\n");
System.out.println(">");
do
{
setClientText();
System.out.println(">" + clientText); //echo
processCommand();
//show the command prompt again
System.out.println(">");
}
while (cont);
System.out.println("FTP client has stopped running");
}
}