0

I have the following Node.js code:

var http = require('http');

http.createServer(function(req, res)) {
    console.log("URL request"+req.url);
    res.writeHead(200, {'Content-Type':'text/plain'});
    res.end('Hello World\n');
}).listen(9898, '127.0.0.1');

console.log('Server locally running at http://127.0.0.1:9898/');

I am using the Socket class in Java to make a socket that also connects to port 9898. I want whatever the Node.js writes (in this case 'Hello World'), to be processed by a Java class. So far, this is what I have for the Java class:

Socket s = new Socket(serverAddress, 9898);
BufferedReader input =
        new BufferedReader(new InputStreamReader(s.getInputStream()));

My question is how do I get the 'Hello World' to be read by the Java code, so that whenever I call System.out.println(input.readLine()), it prints 'Hello World'?

Krusty the Clown
  • 517
  • 6
  • 24
user5139637
  • 775
  • 3
  • 10
  • 29
  • What is the problem? It fails to connect or it connects but it doesn't read anything? Also, you don't need to write `res.writeHead(200, {'Content-Type':'text/plain'});` because you just opened a raw socket in the TCP layer, so no need for HTTP codes there. – ESala Oct 18 '15 at 21:34
  • Hi. I'm not to familiar with java, but from the node side of things it seems that you may want some kind of webservice that responds with hello world when contacted? In which case you could get the java to hit the URL:port that the node server is running on to get the result. My method doesn't use a websocket connection. Is the websocket connection crucial to your work? Especially since in the example you are using localhost, which means there would be no network latency. – X0r0N Oct 18 '15 at 21:42
  • @Darken Yes, the Java code doesn't read anything. It doesn't print "Hello World" in the console? Why. But when I stop the server (running on the port), 'null' is written on the console. Thank you for the help – user5139637 Oct 18 '15 at 21:44
  • @X0r0N Yes, a web socket us not crucial to my work. As of now, I want this project to work locally (so I can test it). What is your method? Thank you for the help – user5139637 Oct 18 '15 at 21:46
  • Your node.js server is an http server. That means when you connect to it and make a request from Java, you have to send an HTTP request, not just open a socket and read. I don't know Java personally, but I'm quite sure it has classes for sending an HTTP request which can likely do this for you in a few lines of code. – jfriend00 Oct 19 '15 at 01:34
  • @jfriend00 Would a socket.io implementation be better (with a socket.io implementation of Node.js) k\or something like a HTTP request? (This is what I am trying to do: I am making web app. In this app a user types something. This string (that the user typed) needs to be sent to a java class that can process the input and then send it back to the javascript which will post it on the screen. The go between that I have chosen between the JavaScript and Java is node.js) – user5139637 Oct 19 '15 at 01:52
  • Why bother with node.js at all? If you're going to process the input in Java, why not just make the web server in Java and skip the node.js entirely? Or, just skip the Java and do all your processing in node.js. I've seen no reason here to use two different servers in different languages just to process some user input. – jfriend00 Oct 19 '15 at 01:56
  • I absolutely need the Java (because of how it processes the information). And I have to display it on a web browser. So... (can I still skip Node.js?) – user5139637 Oct 19 '15 at 02:03

1 Answers1

0

you had an extra ')' on the third line, but your code seems to otherwise work:

var http = require('http');

http.createServer(function(req, res) {
    console.log("URL request"+req.url);
    res.writeHead(200, {'Content-Type':'text/plain'});
    res.end('Hello World\n');
}).listen(9898, '127.0.0.1');

console.log('Server locally running at http://127.0.0.1:9898/');

i guess you need to create some java functionality to make a http request to the url:port the node server is running on. it probably wont be as simple as 'input.readLine()'.

maybe something like this will help for getting the java code to get the data from node:

How can I get an http response body as a string in Java?

X0r0N
  • 1,816
  • 6
  • 29
  • 50
  • I am making web app. In this app a user types something. This string (that the user typed) needs to be sent to a java class that can process the input and then send it back to the javascript which will post it on the screen. The go between that I have chosen between the JavaScript and Java is node.js. What would be the best approach? What changes do I need to make to the Node.js code for what I want to do. I am a beginner in Node.js (and also java) so any help is appreciated – user5139637 Oct 18 '15 at 22:18