# TCP mail client session in Processing not working

**URL:** https://discourse.processing.org/t/tcp-mail-client-session-in-processing-not-working/11618
**Category:** Libraries
**Created:** [May 26, 2019, 10:10pm UTC](https://discourse.processing.org/t/tcp-mail-client-session-in-processing-not-working/11618 "2019-05-26T22:10:28Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Birb](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/birb/32/3280_2.png) [@Birb](https://discourse.processing.org/u/Birb)
#### Post date: [May 26, 2019, 10:10pm UTC](https://discourse.processing.org/t/tcp-mail-client-session-in-processing-not-working/11618/1 "2019-05-26T22:10:28Z")

</div>

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”.

```auto
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](http://mx.google.com) at your service

---

<div class="post-metadata">

### Author: ![morisil](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/morisil/32/6166_2.png) [@morisil](https://discourse.processing.org/u/morisil)
#### Post date: [May 27, 2019, 8:44pm UTC](https://discourse.processing.org/t/tcp-mail-client-session-in-processing-not-working/11618/2 "2019-05-27T20:44:12Z")

</div>

I run this exact code and I got this output:

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

```

---

<div class="post-metadata">

### Author: ![Birb](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/birb/32/3280_2.png) [@Birb](https://discourse.processing.org/u/Birb)
#### Post date: [May 27, 2019, 10:29pm UTC](https://discourse.processing.org/t/tcp-mail-client-session-in-processing-not-working/11618/3 "2019-05-27T22:29:32Z")

</div>

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

---

<div class="post-metadata">

### Author: ![morisil](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/morisil/32/6166_2.png) [@morisil](https://discourse.processing.org/u/morisil)
#### Post date: [May 27, 2019, 10:43pm UTC](https://discourse.processing.org/t/tcp-mail-client-session-in-processing-not-working/11618/4 "2019-05-27T22:43:56Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![morisil](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/morisil/32/6166_2.png) [@morisil](https://discourse.processing.org/u/morisil)
#### Post date: [May 27, 2019, 10:49pm UTC](https://discourse.processing.org/t/tcp-mail-client-session-in-processing-not-working/11618/5 "2019-05-27T22:49:41Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Birb](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/birb/32/3280_2.png) [@Birb](https://discourse.processing.org/u/Birb)
#### Post date: [May 27, 2019, 10:49pm UTC](https://discourse.processing.org/t/tcp-mail-client-session-in-processing-not-working/11618/6 "2019-05-27T22:49:51Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![morisil](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/morisil/32/6166_2.png) [@morisil](https://discourse.processing.org/u/morisil)
#### Post date: [May 27, 2019, 10:57pm UTC](https://discourse.processing.org/t/tcp-mail-client-session-in-processing-not-working/11618/7 "2019-05-27T22:57:47Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![morisil](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/morisil/32/6166_2.png) [@morisil](https://discourse.processing.org/u/morisil)
#### Post date: [May 27, 2019, 11:13pm UTC](https://discourse.processing.org/t/tcp-mail-client-session-in-processing-not-working/11618/8 "2019-05-27T23:13:48Z")

</div>

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

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

```

Or use `PrintWriter` instance with auto flush.
