I’m trying to open a TCP socket with a mail server and then send/read commands, but it isn’t working. After running for several minutes with no response I get the IOException catch error “Couldn’t get I/O for the connection to: hostname”.
import java.io.*;
import java.net.*;
void setup() {
size(200,200);
Socket smtpSocket = null;
DataOutputStream os = null;
// DataInputStream is = null;
BufferedReader br = null;
try {
smtpSocket = new Socket("64.233.177.26", 25);
os = new DataOutputStream(smtpSocket.getOutputStream());
// is = new DataInputStream(smtpSocket.getInputStream());
br=new BufferedReader(new InputStreamReader(smtpSocket.getInputStream()));
}
catch (UnknownHostException e) {
println("Don't know about host: hostname");
}
catch (IOException e) {
println("Couldn't get I/O for the connection to: hostname");
}
if (smtpSocket != null && os != null && br != null) {
try {
os.writeBytes("HELO Buddy\n");
String responseLine;
while ((responseLine=br.readLine()) != null) {
println("Server: " + responseLine);
if (responseLine.indexOf("Ok") != -1) {
break;
}
}
os.close();
br.close();
smtpSocket.close();
}
catch (UnknownHostException e) {
System.err.println("Trying to connect to unknown host: " + e);
}
catch (IOException e) {
System.err.println("IOException: " + e);
}
}
}
The expected output should be : 250 mx.google.com at your service