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(){
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!