小弟是一新手,刚接触node不久,现在遇到一个问题搞了半天无法解决,哪位大神能指点一下,小弟将不胜感激。 我用nodejs做服务器,用socket.io接收客户端的连接,客户端是java程序写的: 客户端如下: import java.io.; import java.net.; public class TalkClient { public static void main(String args[]) throws Exception { String host = "127.0.0.1"; int port = 3000; Socket client = new Socket(host, port); Writer writer = new OutputStreamWriter(client.getOutputStream()); writer.write(“Hello Server.”); writer.flush(); Reader reader = new InputStreamReader(client.getInputStream()); char chars[] = new char[64]; int len; StringBuffer sb = new StringBuffer(); while ((len=reader.read(chars)) != -1) { sb.append(new String(chars, 0, len)); } System.out.println("from server: " + sb); writer.close(); reader.close(); client.close(); } } 服务器端app.js如下: //引入程序包 var express = require(‘express’) , path = require(‘path’) , app = express() , server = require(‘http’).createServer(app) , io = require(‘socket.io’).listen(server);
io.sockets.on('connection’, function(socket){ console.log(“有连接来了”); }); //express基本配置 app.configure(function(){ app.set('port’, process.env.PORT || 3000); app.set('views’, __dirname + ‘/views’); app.use(express.favicon()); app.use(express.logger(‘dev’)); app.use(express.bodyParser()); app.use(express.methodOverride()); app.use(app.router); app.use(express.static(path.join(__dirname, ‘public’))); }); app.configure('development’, function(){ app.use(express.errorHandler()); });
server.listen(app.get(‘port’), function(){ console.log("Express server listening on port " + app.get(‘port’)); });
当我运行客户端java程序的时候,服务器端不是应该打印一行“有链接来了”么? 可是怎么实验也没有啊,问题出在哪里啊?求教,急!