0

I try create chat server, which will be conncted to Android clients. 1 Client send some data on server, server receive it and send to rest of Clients. So I tried use 'net', but that didn't works. On server i'm listening 1490 port and 192.168.3.XX address. Now my Clients can connect to server, but sending data doesn't work. Code:

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');
Pasha Shkaran
  • 1,433
  • 2
  • 23
  • 41
  • why you're not using socket.emit, if you want it to send to all the clients? That way you can broadcast to all the registered clients at once. – Ravi Sep 19 '14 at 10:53
  • @Ravi I even don't get log in console console.log(data);, so that part of code does not work completely – Pasha Shkaran Sep 19 '14 at 11:04
  • check the `socket` and `port` you are connecting to, and you should emit data on 'data' event, like `socket.emit('data', 'your data')`. – Ravi Sep 19 '14 at 11:12
  • @Ravi changed the code, is that correct? – Pasha Shkaran Sep 19 '14 at 11:19
  • it seems fine, you should also change the client side code, accordingly. PS i don't know much of Android. – Ravi Sep 19 '14 at 11:29
  • @Ravi i try again but obviously socket.on('data', function(data) that block doesn't work – Pasha Shkaran Sep 19 '14 at 12:36
  • try some debugging on both sides, because i cannot tell/guess the problem without any kind or error or something... – Ravi Sep 19 '14 at 12:40

1 Answers1

0

I think your problem is on the android client code.
Check this, hope it helps

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
    }
}