English 中文(简体)
TCP/IP使用“冷漠”进行简单的通信
原标题:Simple TCP/IP socket communication using ColdFusion

我做了一些搜索,似乎在通过冷聚成功建立笔/笔记连接方面有很大的成绩。 我试图作为简单的客户行事,并发出插手和反应。 登峰造光机需要服务器的安装,我可以触动,但似乎只是听众(根据Adobe s doc,“它可以向现有客户发出离去的信息,但不能建立链接)。

关于SO/cflib.org,还有一个例子,就是在网上使用 Java物体的流行职位,但Im没有成功,而且似乎还有许多人对此有一定程度的麻烦。 在我的尝试中,我可以把它搁置/连接起来,但别无选择。 如果我试图发出插座,CF页面负荷就会被罚款,但服务器方面似乎从未见过任何东西(但会记录或显示连接/脱节)。 如果我试图阅读答复,那页就永远不会装上。 如果我在尝试时关闭服务器,它将在尝试阅读Line时显示连接重新开始。 我已用内部的传闻和简便的贾瓦袖珍听众来尝试这样做,他们将发出连接的信息,并应当重复所发的任何信息。

难道这不单单单是CF的工作吗? 如果没有,jQuery/Ajax 广场的任何其他简单建议/实例?

Java听众:

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 简明灯(敏锐的超声波头/脚手、硬编码的IP、简单听器的港口=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 Read (again, minus header/footer template, IP of server to be hard-coded, 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()>

我试图在这里和那里增加一些睡觉,并试图用一个寄来,不是使用印刷机/使用单纯的标语,而是书写标语。 任何帮助都将受到高度赞赏。 感谢!

问题回答

即便在利用贾瓦时,出于以下简单原因,这仍将是一个非常具有挑战性的进程:

www.un.org/Depts/DGACM/index_spanish.htm 袖珍通信是实时的,而网络要求则有明确的起点和停留点。

例如,当你提出“冷静”模板要求时,一切(可变、共享记忆、物体即刻等)都在要求页的范围之内,而且(禁止几个洞穴)在页面要求结束时死亡。 因此,请在您具备履行以下任务所需的CFML模板时假设:

  1. Opened a socket.
  2. Established a connection to a remote ip:port.
  3. Listened for a response.
  4. Printed that response to the browser.

让我们进一步假设,你的法典正在更新、测试和运行。 你们能够开车,与遥远的管道和港口连接(他们实际上看到即将到来的对遥远服务器的要求,并且能够证实这一点),而且为了所有目的和目的,连接是好的。

然后,在你执行CFML网页10分钟后,遥远的服务器在接线上发送了一系列案文。

......您的调频最终不会活着,等待反应,准备向浏览器印刷。 当CFML模板要求终止时,你即时提出的用于开张和连接......的所有物体均已消失。

正因为如此(如上所述),当你试图“阅读答复”时,你发现该页从未装满。 正在发生的情况是,有人告诉“愿意接受,我们实际上可能获得一些数据,以填补这一空白”。

实时电话通信的性质是,它连接起来,听起来,等待答复......不幸的是,一个运行良好的网页不能运行(等待),最终会停下来。

底线是,尽管 Java将允许你打开/连接/发送/接收/接收近似的原始材料,但从CFML页的角度来看,这样做可能不是你最终寻求的办法。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签