title: Simple TCP/IP socket communication using ColdFusion
body: <p>I've done some searching and it doesn't seem like there is much in the way of success in establishing a tcp/ip socket connection successfully via Coldfusion.  I'm trying to act as a simple client and send a string over and get a response.  Adobe's EventGateway requires server-side setup, which I can't touch, but also appears to be a listener only (according to Adobe's doc, "It can send outgoing messages to existing clients, but cannot establish a link itself.").</p>
  
  <p>There is another example on SO/cflib.org that is the prevailing post over the web invoking Java objects, but I'm not succeeding with it and it seems pretty much everyone else has some degree of trouble with it.  In my attempts, I can get it to init/connect a socket, but nothing else.  If I try to send a string, the CF page loads fine but the server side seemingly never sees anything (but will log or note a connection/disconnection).  If I try to read a response, the page will never load.  If I close the server while it's trying, it will show a connection reset while trying readLine().  I have tried this with an in-house app as well as a simple Java socket listener that will send a message on connect and should echo anything sent.</p>
  
  <p>Is this just not a job for CF?  If not, any other simple suggestions/examples from the jQuery/Ajax realm?</p>
  
  <p>Java listener app:</p>
  
  <pre><code>package blah;
  
  import java.awt.Color;
  import java.awt.BorderLayout;
  import java.awt.event.*;
  import javax.swing.*;
  
  import java.io.*;
  import java.net.*;
  
  class SocketServer extends JFrame
      implements ActionListener {
  
  /**
   * 
   */
  private static final long serialVersionUID = 1L;
  JButton button;
     JLabel label = new JLabel("Text received over socket:");
     JPanel panel;
     JTextArea textArea = new JTextArea();
     ServerSocket server = null;
     Socket client = null;
     BufferedReader in = null;
     PrintWriter out = null;
     String line;
  
     SocketServer(){ //Begin Constructor
       button = new JButton("Click Me");
       button.addActionListener(this);
  
       panel = new JPanel();
       panel.setLayout(new BorderLayout());
       panel.setBackground(Color.white);
       getContentPane().add(panel);
       panel.add("North", label);
       panel.add("Center", textArea);
       panel.add("South", button);
  
     } //End Constructor
  
    public void actionPerformed(ActionEvent event) {
       Object source = event.getSource();
  
       if(source == button){
           textArea.setText(line);
       }
    }
  
    public void listenSocket(){
  
      try{
        server = new ServerSocket(4444); 
      } catch (IOException e) {
       System.out.println("Could not listen on port 4444");
        System.exit(-1);
      }
  
      try{
        client = server.accept();
   //Show connection status in text box, and send back to client
        line = " Connected ";
        out.println(line);
        textArea.setText(line);
      } catch (IOException e) {
        System.out.println("Accept failed: 4444");
        System.exit(-1);
      }
  
      try{
        in = new BufferedReader(new InputStreamReader(client.getInputStream()));
        out = new PrintWriter(client.getOutputStream(), true);
      } catch (IOException e) {
        System.out.println("Accept failed: 4444");
        System.exit(-1);
      }
  
      while(true){
        try{
  //Try to concatenate to see if line is being changed and we're just not seeing it, show in textbox
          line = line + " " + in.readLine();
          textArea.setText(line);
  //Send data back to client
          out.println(line);
        } catch (IOException e) {
          System.out.println("Read failed");
          System.exit(-1);
        }
      }
    }
  
    protected void finalize(){
  //Clean up 
       try{
          in.close();
          out.close();
          server.close();
      } catch (IOException e) {
          System.out.println("Could not close.");
          System.exit(-1);
      }
    }
  
    public static void main(String[] args){
          SocketServer frame = new SocketServer();
      frame.setTitle("Server Program");
          WindowListener l = new WindowAdapter() {
                  public void windowClosing(WindowEvent e) {
                          System.exit(0);
                  }
          };
          frame.addWindowListener(l);
          frame.pack();
          frame.setVisible(true);
      frame.listenSocket();
    }
  }
  </code></pre>
  
  <p>CF Simple Send (minus HTML header/footer, IP to be hard-coded, port on simple listener = 4444):</p>
  
  <pre><code>&lt;cfset sock = createObject( "java", "java.net.Socket" )&gt;
  &lt;cfset sock.init( "ip.ip.ip.ip", 4444)&gt;
  
  &lt;cfset streamOut = sock.getOutputStream()&gt;
  
  &lt;cfset output = createObject("java", "java.io.PrintWriter").init(streamOut)&gt;
  &lt;cfset streamOut.flush()&gt;
  
  &lt;cfset output.println("Test Me")&gt;
  &lt;cfset output.println()&gt;
  
  &lt;cfset streamOut.flush()&gt;
  
  &lt;cfset sock.shutdownOutput()&gt;
  &lt;cfset sock.close()&gt;
  </code></pre>
  
  <p>Simple CF Read (again, minus header/footer template, IP of server to be hard-coded, port 4444)</p>
  
  <pre><code>&lt;cfset sock = createObject( "java", "java.net.Socket" )&gt;
  &lt;cfset sock.init( "ip.ip.ip.ip", 4444)&gt;
  
  &lt;cfset streamInput = sock.getInputStream()&gt;
  &lt;cfset inputStreamReader= createObject( "java", "java.io.InputStreamReader").init(streamInput)&gt;
  
  &lt;cfset input = createObject( "java", "java.io.BufferedReader").init(InputStreamReader)&gt;
  
  &lt;cfset result = input.readLine()&gt;
  
  &lt;cfset sock.shutdownInput()&gt;
  &lt;cfset sock.close()&gt;
  </code></pre>
  
  <p>I've tried adding some sleeps here and there and also have tried a send not using PrintWriter/using just ObjectOutputStream and writeObject() , but same behavior.  Any help would be greatly appreciated.  Thanks!</p>