最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Sending data from node.js to Java using sockets - Stack Overflow

programmeradmin0浏览0评论

I'm trying to send data from node.js to Java through sockets. I searched around but nothing was really useful. I'm used to socket.io but in this case it doesn't seem really suitable for this. It seems like all the socket extensions for node.js are not really suited for sending messages, but rather listening to messages and answering something.

My Java app basically should receive some work to do from node.js, do the work and send some result to node.js back. And no, the work cannot be done on node.js, it has to be done by Java (which actually is Scala but whatever).

Does anyone of you know how can I do something like this?

Thanks

I'm trying to send data from node.js to Java through sockets. I searched around but nothing was really useful. I'm used to socket.io but in this case it doesn't seem really suitable for this. It seems like all the socket extensions for node.js are not really suited for sending messages, but rather listening to messages and answering something.

My Java app basically should receive some work to do from node.js, do the work and send some result to node.js back. And no, the work cannot be done on node.js, it has to be done by Java (which actually is Scala but whatever).

Does anyone of you know how can I do something like this?

Thanks

Share Improve this question edited Dec 6, 2011 at 22:24 Gray 117k24 gold badges302 silver badges359 bronze badges asked Dec 6, 2011 at 21:58 MasiarMasiar 21.4k31 gold badges100 silver badges140 bronze badges 2
  • Have you considered the built-in net.Socket? – Felix Loether Commented Dec 6, 2011 at 23:06
  • I upgraded to the last version of node and it seems the net stuff doesn't have the method .connect thus I can't use them I don't know why. – Masiar Commented Dec 7, 2011 at 0:24
Add a comment  | 

1 Answer 1

Reset to default 17

You can use the build in socket in node.js to do something like that (very easy both in java and node.js, but you'll get the point) :

Java :

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class Test {

    public static void main(String[] args) {
        ServerSocket server;
        Socket client;
        InputStream input;

        try {
            server = new ServerSocket(1010);
            client = server.accept();

            input = client.getInputStream();
            String inputString = Test.inputStreamAsString(input);

            System.out.println(inputString);

            client.close();
            server.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String inputStreamAsString(InputStream stream) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(stream));
        StringBuilder sb = new StringBuilder();
        String line = null;

        while ((line = br.readLine()) != null) {
            sb.append(line + "\n");
        }

        br.close();
        return sb.toString();
    }

}

Node.js :

var net = require('net');

var client = net.connect(1010, 'localhost');

client.write('Hello from node.js');

client.end();

And the link to the node.js doc about sockets : http://nodejs.org/docs/latest/api/net.html

发布评论

评论列表(0)

  1. 暂无评论