Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Java将Meteor中的消息服务器套接字发送到客户端_Java_Sockets_Meteor - Fatal编程技术网

用Java将Meteor中的消息服务器套接字发送到客户端

用Java将Meteor中的消息服务器套接字发送到客户端,java,sockets,meteor,Java,Sockets,Meteor,我想从Meteor应用程序向Java应用程序发送消息,Meteor应用程序是服务器套接字,Java应用程序是客户端 我的目标是在Meteor应用程序的文本框中编写一条消息,并使用套接字发送到Java 有人能指出我代码中的错误吗 我的代码: 流星 my.html myServer.js 我的Java代码: public static void main(String[] args) { Socket smtpSocket = null; DataOutputStream os =

我想从Meteor应用程序向Java应用程序发送消息,Meteor应用程序是服务器套接字,Java应用程序是客户端

我的目标是在Meteor应用程序的文本框中编写一条消息,并使用套接字发送到Java

有人能指出我代码中的错误吗

我的代码: 流星

my.html

myServer.js

我的Java代码:

public static void main(String[] args) {
    Socket smtpSocket = null;
    DataOutputStream os = null;
    DataInputStream is = null;
    PrintStream output;
    try {
        smtpSocket = new Socket("localhost", 666);
        os = new DataOutputStream(smtpSocket.getOutputStream());
        is = new DataInputStream(smtpSocket.getInputStream());
        output = new PrintStream(smtpSocket.getOutputStream());
        output.print("Rastalovely");  //  Send to Meteor this message
    } catch (UnknownHostException e) {
        System.err.println("Don't know about host: hostname");
    } catch (IOException e) {
        System.err.println("Couldn't get I/O for the connection to: hostname");
    }
    if (smtpSocket != null && os != null && is != null) {
        try {
            String responseLine;
            while ((responseLine = is.readLine()) != null) {
                //    wait the response from Meteor
                System.out.println("Server: " + responseLine);
                if (responseLine.indexOf("Ok") != -1) {
                    break;
                }
            }
            os.close();
            is.close();
            smtpSocket.close();
        } catch (UnknownHostException e) {
            System.err.println("Trying to connect to unknown host: " + e);
        } catch (IOException e) {
            System.err.println("IOException:  " + e);
        }
    }
}

感谢

为了在方法中使用连接,您需要使其可用,即通过在Meteor外部声明它。startup我也赞成在顶级代码上声明Meteor方法,以便在调用startup之前初始化它们:

var net = require('net');
var server;

Meteor.startup(() => {
  // Here is necessary start the server socket

  server = net.createServer(
    function (connection) {
      //...
    }   
  );

  server.listen(666, function () {
    console.log('server is listening');
  });

});

Meteor.methods({
    method_socket: function (message) {
        // I can´t send this message to Java app I had tried:
        server.connection.write(message+'\r\n'); 
        //connection.write(message+'\r\n');
    }
});

为了在方法中使用连接,您需要使其可用,即在Meteor.startup之外声明连接。我还建议在顶级代码上声明Meteor方法,以便在调用startup之前初始化它们:

var net = require('net');
var server;

Meteor.startup(() => {
  // Here is necessary start the server socket

  server = net.createServer(
    function (connection) {
      //...
    }   
  );

  server.listen(666, function () {
    console.log('server is listening');
  });

});

Meteor.methods({
    method_socket: function (message) {
        // I can´t send this message to Java app I had tried:
        server.connection.write(message+'\r\n'); 
        //connection.write(message+'\r\n');
    }
});

是否有任何控制台输出/错误?在使用:server.connection.write(message+'\r\n')时,仅“无法读取未定义的属性write”;我明白了,因为连接不在共享上下文中,所以您的方法不知道连接,因为服务器是在启动时创建的变量。将其设置为Meteor.startup.com之外的变量。是否存在任何控制台输出/错误?使用时,仅“无法读取未定义的属性write”:server.connection.write(message+'\r\n');我明白了,因为连接不在共享上下文中,所以您的方法不知道连接,因为服务器是在启动时创建的变量。将其设置为Meteor.startup之外的变量。您的想法是编写以下代码:server=net.createServer(函数(连接){…});您也可以在方法中调用net.createServer,但每次调用该方法时都会创建服务器和连接,这就是为什么将服务器作为变量提供给方法b,但在启动时对其进行初始化;进入方法_套接字?您也可以在方法中调用net.createServer,但这将在每次调用方法时创建服务器和连接,这就是为什么您将服务器作为变量提供给方法b,但在启动时初始化它。
var net = require('net');
var server;

Meteor.startup(() => {
  // Here is necessary start the server socket

  server = net.createServer(
    function (connection) {
      //...
    }   
  );

  server.listen(666, function () {
    console.log('server is listening');
  });

});

Meteor.methods({
    method_socket: function (message) {
        // I can´t send this message to Java app I had tried:
        server.connection.write(message+'\r\n'); 
        //connection.write(message+'\r\n');
    }
});