1

I am trying to connect a system.net tcp client to a node js server. The server is running and can be connected to from elsewhere, but when i try and connect from the system.net socket the connection gets refused.

here is the client code

 string server = "127.0.0.1";
            string message = "a simple message";
            try
            {
                // Create a TcpClient. 
                // Note, for this client to work you need to have a TcpServer  
                // connected to the same address as specified by the server, port 
                // combination.

                Int32 port = 6969;
                TcpClient client = new TcpClient(server, port);

                // Translate the passed message into ASCII and store it as a Byte array.
                Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);

                // Get a client stream for reading and writing. 
                //  Stream stream = client.GetStream();

                NetworkStream stream = client.GetStream();

                // Send the message to the connected TcpServer. 
                stream.Write(data, 0, data.Length);

                Console.WriteLine("Sent: {0}", message);

                if(client.Connected == true)
                {
                    System.Diagnostics.Debug.WriteLine("here, sucessful connect");
                }
                else
                {
                    System.Diagnostics.Debug.WriteLine("here, not connected");
                }

                // Receive the TcpServer.response. 

                // Buffer to store the response bytes.
                data = new Byte[256];

                // String to store the response ASCII representation.
                String responseData = String.Empty;

                // Read the first batch of the TcpServer response bytes.
                Int32 bytes = stream.Read(data, 0, data.Length);
                responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
                Console.WriteLine("Received: {0}", responseData);

                // Close everything.
                stream.Close();
                client.Close();
            }
            catch (ArgumentNullException ea)
            {
                Console.WriteLine("ArgumentNullException: {0}", ea);
                System.Diagnostics.Debug.WriteLine("here, failed connection");
            }
            catch (SocketException eaa)
            {
                Console.WriteLine("SocketException: {0}", eaa);
                System.Diagnostics.Debug.WriteLine("here, failed connection");
            }
        };

here is the server code

var net = require('net');

var HOST = '127.0.0.1';
var PORT = 6969;

// Create a server instance, and chain the listen function to it
// The function passed to net.createServer() becomes the event handler            for the 'connection' event
// The sock object the callback function receives UNIQUE for each connection
net.createServer(function(sock) {

// We have a connection - a socket object is assigned to the connection automatically
console.log('CONNECTED: ' + sock.remoteAddress +':'+ sock.remotePort);

// Add a 'data' event handler to this instance of socket
sock.on('data', function(data) {

    console.log('DATA ' + sock.remoteAddress + ': ' + data);
    // Write the data back to the socket, the client will receive it as data from the server
    sock.write('You said "' + data + '"');

});

// Add a 'close' event handler to this instance of socket
sock.on('close', function(data) {
    console.log('CLOSED: ' + sock.remoteAddress +' '+ sock.remotePort);
});

 }).listen(PORT, HOST);

console.log('Server listening on ' + HOST +':'+ PORT);

Any help would be appreciated.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
shinAcku
  • 44
  • 4
  • 1
    I ran the exact code that you have given and it worked for me. Maybe there is something wrong with your setup? Please would you post the error that you get? – Shane Haw Jan 22 '15 at 10:33
  • I just ran it from c# console and it worked. I was trying to get it working of an android emulator, the fault must be there. Thank you very much – shinAcku Jan 23 '15 at 09:25

0 Answers0