0

我尝试创建聊天服务器,它将连接到 Android 客户端。1 客户端在服务器上发送一些数据,服务器接收它并发送给其余的客户端。所以我尝试使用'net',但这不起作用。在服务器上,我正在监听 1490 端口和 192.168.3.XX 地址。现在我的客户可以连接到服务器,但发送数据不起作用。代码:

var net = require('http');
var sockets = [];
var tcpServer = net.createServer();

tcpServer.on('connection', function(socket){
console.log(socket.remoteAddress+' connected');
socket.setEncoding('utf8');
sockets.push(socket);
socket.on('data', function(data){
    console.log(data);
    socket.emit('data', data);
});
});
tcpServer.listen(1490, '192.168.3.XX');
4

1 回答 1

0

我认为您的问题出在android客户端代码上。
看看这个,希望对你有帮助

public void postData() {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("id", "12345"));
        nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);
        
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }
} 

于 2014-09-19T10:54:38.390 回答