TCP mail client session in Processing not working

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

1 Like

I run this exact code and I got this output:

Server: 220 mx.google.com ESMTP z9si2024903ybj.299 - gsmtp
Server: 250 mx.google.com at your service

Weird, what version of processing? Everything is exactly the same? I wonder why I’m unable to connect.

Processing 3.5.3 on ubuntu linux. I remember that there was one annoying bug with TCP connections on mac initiated from JVM due to timeout with DNS resolution. But usually it was just taking up to 5 seconds instead of milliseconds. What’s your output from this code?

BTW what happens when you do telnet 64.233.177.26 25 from terminal on the same machine?

I get no output at all, sometimes after a number of minutes it results in the IOexception catch, so the problem seems to be establishing the socket connection. I was thinking it may be my connection, as I’m in China and google is blocked, but I can ping 64.233.177.26 and telnet 64.233.177.26 25 works fine through command prompt. Just to test that theory though, how could I connect through a proxy with sockets?

I should specify I’m also on 3.5.3, but using Windows.

so if you want to debug what’s happening with TCP connections you can use netstat. As far as I remember it works quite similarly on windows. After starting your code I do:

$ netstat -na | grep :25
tcp6 0 0 mylocalip:41150 64.233.177.26:25 ESTABLISHED

If something is wrong with the connection it wouldn’t be established.

1 Like

You can also try to do flush() on the OutputStream:

os.writeBytes("HELO Buddy\n"); 
os.flush();

Or use PrintWriter instance with auto flush.