server.js:
var express = require('express'); // 引入express模块
var app = express();
var bodyParser = require('body-parser');
// 创建 application/x-www-form-urlencoded 编码解析
var urlencodedParser = bodyParser.urlencoded({ extended: false })
app.get('/', function(req, res){ // 指定更目录
res.sendFile( __dirname + "/index.htm" );
});
app.post('/connect', urlencodedParser, function(req, res){
client.connect(req.body, function() {
console.log('CONNECTED SUCCESS');
});
});
process.on('uncaughtException', function (err) {
//打印出错误
console.log(err.code);
});
var server = app.listen(888, function(){
console.log('listen on: 888');
});
index.html
<div class="formConnect">
<input type="text" id="host" placeholder="请填写HOST">
<input type="text" id="port" placeholder="请填写PORT">
<button id="connect_btn" onclick="air_connect();">连接</button>
</div>
<script type="text/javascript" src="js/jquery-3.3.1.min.js"></script>
<script type="text/javascript">
function air_connect(){
host = $.trim($('#host').val());
port = $.trim($('#port').val());
$.post('/connect', {'host':host, 'port':port}, function(backData){
console.log(backData)
});
}
</script>
比如 监听电脑的IP:192.168.1.05 同局域我不同电脑访问192.168.1.5:888 去连接同一TCP地址 就会报错
1
lsp7572 2019-03-18 21:49:57 +08:00
不是 Socket 的 C/S 通信模式吗,8080 作为监听端口要分配别的 socket 和 client 通信吧
|
2
misaka19000 2019-03-18 21:54:29 +08:00
上代码
|
3
zwh2698 2019-03-18 21:57:23 +08:00 via Android
服务器端是不是设置了相应的个数?
|
4
aa514758835 2019-03-19 09:32:37 +08:00
没有端口复用吧,猜的
|
5
lzm1005 OP @misaka19000 代码已经附上
|