Ecco il codice:
codice:
import java.net.*;
import java.io.*;
import java.util.*;
public class RiceviMsg
{
int port;
String host;
private static BufferedReader in;
private static DataOutputStream out;
public static void main(java.lang.String[] args) throws Exception
{
String username = args[0]; // users name
String password = args[1]; // password
String popServer = args[2]; // POP servers name.
boolean delete = false;
boolean secure = false;
Socket s; // communicazione socket
String inputLine; // string to store the line of text we read in.
String numeroMessaggi; // numero messaggi
try
{
if (args.length > 3)
if (args[3].compareTo("-s") == 0)
secure = true;
else if (args[3].compareTo("-d") == 0)
delete = true;
else
System.out.println("Errore: La sintassi č %java PopClient <username> <password> [-s] [-d]");
if (args.length > 4)
if (args[4].compareTo("-s") == 0)
secure = true;
else if (args[4].compareTo("-d") == 0)
delete = true;
else
System.out.println("Errore: La sintassi č %java PopClient <username> <password> [-s] [-d]");
{
// connessione al server
Socket s = new Socket("nome_host", 110); // host e porta
Out = new DataOutputStream( s.getOutputStream() );
In = new BufferedReader( new InputStreamReader(s.getInputStream()) );
String line;
}
inputLine = readFromInputStream(); // autenticazione
if (!readFromInputStream().startsWith("+OK"))
{
System.out.println("User name errato, disconnessione in corso");
s.close();
System.exit(1);
}
// invio pass
out.println("PASS " + password);
System.out.println("c: PASS ********");
// aspetta la risposta
inputLine = readFromInputStream();
if (!inputLine.startsWith("+OK"))
{
System.out.println("Password errata, disconnesisone in corso");
s.close();
System.exit(1);
}
else
{
String timeStamp = inputLine.substring(4,inputLine.length());
System.out.println(timeStamp);
// concatenate timestamp with secret(password) and encrypt using md5.
md5 mdc = new md5(timeStamp + password);
System.out.println(timeStamp + password);
mdc.calc();
DataOutputStream("APOP " + username + " " + mdc);
readFromInputStream();
s.close();
System.exit(1);
}
DataOutputStream("STAT");
inputLine = readFromInputStream();
if (!inputLine.startsWith("+OK"))
{
System.out.println("Errore:" + inputLine);
s.close();
System.exit(1);
}
int i = inputLine.lastIndexOf(' ');
numeroMessaggi = inputLine.substring(4,i);
System.out.println("Hai " + numeroMessaggi + " messaggio/i nella tua mailbox!");
}
// scarica i messaggi
// per ogni messaggio invochiamo il comando RETR with as parameter the number
// (index) of the message
int n = Integer.parseInt(numberMessages);
int numberBytes;
for (int msg = 1;msg<=n;msg++)
{
System.out.println("Retreaving message " + msg);
DataOutputStream("RETR " + msg);
inputLine = readFromInputStream();
if (!inputLine.startsWith("+OK"))
{
System.out.println("Error: " + inputLine);
s.close();
System.exit(1);
}
i = inputLine.lastIndexOf(' ');
numberBytes = Integer.parseInt(inputLine.substring(4,i));
inputLine = in.readLine();
while (inputLine.compareTo(".") != 0)
{
if (inputLine.compareTo("..") == 0)
System.out.println(".");
else
System.out.println(inputLine);
inputLine = in.readLine();
}
if (delete)
{
DataOutputStream("DELE " + msg);
readFromInputStream();
}
}
// fine della connessione e della classe RiceviMsg
DataOutputStream("QUIT");
readFromInputStream();
s.close();
System.exit(1);
}
catch (IOException e) { System.err.println("IOException : " + e);}
// metodo DataOutputStream
private static void DataOutputStream(String s) throws IOException
{
System.out.println("c: " + s);
out.println(s);
return;
}
// metodo readFromInputStream
private static String readFromInputStream() throws IOException
{
String s = in.readLine();
if (s != null)
System.out.println("s: " + s);
return s;
}
}
vi allego 2 file .gif in cui potete vedere gli errori in fase si compilazione.