Easy TCP / IP communication using ColdFusion

I did a few searches, and it looks like I could not succeed in establishing a tcp / ip socket connection using Coldfusion. I am trying to act as a simple client and send a string and receive a response. Adobe EventGateway requires a server-side installation, which I cannot touch, but also seems to be only a listener (according to Adobe's document, “It can send outgoing messages to existing clients, but cannot set a link.”).

There is another example on SO / cflib.org, which is the predominant post over a website linking to Java objects, but I fail, and it looks like everyone has a problem with it. In my attempts, I can start it / connect the socket, but nothing else. If I try to send a string, the CF page will load normally, but the server side seems to never see anything (but will register or mark the connection / disconnect). If I try to read the answer, the page will never load. If I close the server during its attempt, it will show a reset connection when trying to readLine (). I tried this with my own application, as well as a simple Java socket listener that will send a message to the connection and should give back all the sent message.

Is this just not a job for CF? If not, any other simple suggestions / examples from the jQuery / Ajax area?

Java listening app:

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(); } } 

CF Simple Send (minus HTML header / footer, IP address for hard coding, port on a simple listener = 4444):

 <cfset sock = createObject( "java", "java.net.Socket" )> <cfset sock.init( "ip.ip.ip.ip", 4444)> <cfset streamOut = sock.getOutputStream()> <cfset output = createObject("java", "java.io.PrintWriter").init(streamOut)> <cfset streamOut.flush()> <cfset output.println("Test Me")> <cfset output.println()> <cfset streamOut.flush()> <cfset sock.shutdownOutput()> <cfset sock.close()> 

Simple CF lookup (again, minus the header / footer template, server IP address for hard coding, port 4444)

 <cfset sock = createObject( "java", "java.net.Socket" )> <cfset sock.init( "ip.ip.ip.ip", 4444)> <cfset streamInput = sock.getInputStream()> <cfset inputStreamReader= createObject( "java", "java.io.InputStreamReader").init(streamInput)> <cfset input = createObject( "java", "java.io.BufferedReader").init(InputStreamReader)> <cfset result = input.readLine()> <cfset sock.shutdownInput()> <cfset sock.close()> 

I tried adding some dreams here and there, and also tried sending without using PrintWriter / using only ObjectOutputStream and writeObject (), but the same behavior. Any help would be greatly appreciated. Thanks!

+4
source share
1 answer

This will be a very difficult process to deploy in ColdFusion, even when you use Java, for a simple reason:

Real-time socket communication, while web requests have start and stop endpoints.

For example, when you make a request to the ColdFusion template, everything (variables, shared memory, object instance, etc.) lives in the context of the page request - and (prohibition of several reservations) dies when the page request ends, So, let's say at that moment, that you had a CFML template that completed the following tasks upon request:

  • The socket is open.
  • A connection has been established with the remote ip: port.
  • I listened to the answer.
  • Printed the answer in the browser.

Suppose further that your code works and is tested and works. You can open the socket, connect to the remote ip and port (you really see the incoming request on the remote server and you can confirm it) and for all purposes and tasks ... your connection is good.

Then, 10 minutes after the execution of your CFML page, the remote server sent a line of text over the connection ...

... at your end CFML there is nothing alive and awaiting a response ready to print it in a browser. The objects you created used to open the socket and connect ... all disappeared when the request for the CFML template ended.

This is why (as stated above), when you tried to “read the answer”, you noticed that the page never loaded. What happens is that ColdFusion says “wait, please, we could possibly get some data on this socket” ... so it blocks the web request from completion and waits ... which appears to the user like That appears to be a "hung up" web page.

The nature of the real-time communication of sockets is that it is connected and listened, waiting for a response ... and, unfortunately, a working web page cannot work (and "wait") forever, it ultimately expires.

The bottom line is that while Java allows you to open / plug / send / receive / close raw sockets, doing this from the context of a CFML page may not be the approach you are ultimately looking for.

+2
source

Source: https://habr.com/ru/post/1390544/


All Articles