const http = require("http");
const net = require("net");
const host = '127.0.0.1';
const port = 8082;
let count = 0;
http.createServer(function (req, res) {
debugger;
count++;
req.on('data', function(){
debugger;
});
req.on('end', async function () {
debugger;
console.log('count(%d)', count);
let data;
data = await getJavaData(count);
res.writeHead(200, { 'Content-Type': 'text/html' });
let content = `<p>${data}</p>`;
res.end(content);
});
}).listen(3000);
//======================================
function getJavaData(count) {
return new Promise(function (res, rej) {
let data = '';
let time = 0;
const client = net.connect(port, host, function () {
console.log('java connect...');
});
client.on('data', function (_data) {
if (time++ == 0) {
data = _data.toString();
console.log('java count(%s) data(%s) data.length', count, data, data.length);
}
});
client.on('end', function () {
console.log('java end...');
res(data);
});
});
}
//////////////////////////////////////////////////////////////////////////////////////
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package test_5;
import java.io.IOException;
import java.io.PrintStream;
import java.net.*;
import static java.lang.System.out;
/**
*
* @author xman
*/
public class Socket_1 {
ServerSocket ss;
Socket socket;
static int port = 8082;
public Socket_1() throws Exception {
ss = new ServerSocket(port);
while(true){
socket = ss.accept();
out.println("connect");
PrintStream ps = new PrintStream(socket.getOutputStream());
ps.println("i am java");
ps.close();
socket.close();
out.println("disconnect");
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
try{
new Socket_1();
}catch(Exception e){
out.println(e.toString());
}
}
}