Nguyễn Chí Trung
(Yogi)
New Member
Doạn code duoi đây nó tạo thread để làm gì nhỉ, thằng này thích phức tạp hóa vấn đề hay sao ?
public class SMTPClient {
private Socket socket;
private PrintWriter out;
public SMTPClient() throws IOException {
socket = new Socket("smtp.wanadoo.fr", 25);
out = new PrintWriter(socket.getOutputStream());
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
InputStream is = SMTPClient.this.socket.getInputStream();
int data;
while ((data = is.read()) != -1) {
System.out.write(data);
}
} catch (IOException e) {
System.err.println("Erreur de lecture ! ");
e.printStackTrace();
}
}
});
t.start();
}
void writeData(String data) {
System.out.println("envoi de : " + data);
out.write(data);
out.write("\n");
}
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
SMTPClient smtp = new SMTPClient();
smtp.writeData("helo JavaSMTPClient");
smtp.writeData("MAIL FROM: <[email protected]>");
smtp.writeData("RCPT TO: <[email protected]>");
smtp.writeData("DATA");
smtp.writeData("subject : test d'envoi de message");
smtp.writeData("Ceci est un test d'envoi de message.");
smtp.writeData("\n.\n");
smtp.writeData("quit");
}
}
public class SMTPClient {
private Socket socket;
private PrintWriter out;
public SMTPClient() throws IOException {
socket = new Socket("smtp.wanadoo.fr", 25);
out = new PrintWriter(socket.getOutputStream());
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
InputStream is = SMTPClient.this.socket.getInputStream();
int data;
while ((data = is.read()) != -1) {
System.out.write(data);
}
} catch (IOException e) {
System.err.println("Erreur de lecture ! ");
e.printStackTrace();
}
}
});
t.start();
}
void writeData(String data) {
System.out.println("envoi de : " + data);
out.write(data);
out.write("\n");
}
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
SMTPClient smtp = new SMTPClient();
smtp.writeData("helo JavaSMTPClient");
smtp.writeData("MAIL FROM: <[email protected]>");
smtp.writeData("RCPT TO: <[email protected]>");
smtp.writeData("DATA");
smtp.writeData("subject : test d'envoi de message");
smtp.writeData("Ceci est un test d'envoi de message.");
smtp.writeData("\n.\n");
smtp.writeData("quit");
}
}