nodejs和c#combinationquery

时间:2014-10-11 14:55:54

标签: c# node.js sockets tcp

我正在构建一个应用程序,我需要有一个c#server和nodejs客户端。我已经构建了组件,但我总是遇到ECONNREFUSED错误。除了这个错误,我可以获得任何线索吗?

FYI,  我可以从c#客户端连接到c#服务器。与此类似。我能够从nodejs tcp客户端连接到nodejs tcp服务器。然而,我正面临这种混合物的错误。

嘿抱歉没有提前添加代码。 以下是c#服务器代码:

using System;
using System.Text;
using AsyncClientServerLib.Server;
using System.Net;
using AsyncClientServerLib.Message;
using SocketServerLib.SocketHandler;
using SocketServerLib.Server;

namespace TestApp
{
    delegate void SetTextCallback(string text);

    public partial class FormServer : Form
    {
        private BasicSocketServer server = null;
        private Guid serverGuid = Guid.Empty;

        public FormServer()
        {
            InitializeComponent();
        }

        protected override void OnClosed(EventArgs e)
        {
            if (this.server != null)
            {
                this.server.Dispose();
            }
            base.OnClosed(e);
        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            this.serverGuid = Guid.NewGuid();
            this.server = new BasicSocketServer();
            this.server.ReceiveMessageEvent += new SocketServerLib.SocketHandler.ReceiveMessageDelegate(server_ReceiveMessageEvent);
            this.server.ConnectionEvent += new SocketConnectionDelegate(server_ConnectionEvent);
            this.server.CloseConnectionEvent += new SocketConnectionDelegate(server_CloseConnectionEvent);
            this.server.Init(new IPEndPoint(IPAddress.Loopback, 2147));
            this.server.StartUp();
            this.buttonStart.Enabled = false;
            this.buttonStop.Enabled = true;
            this.buttonSend.Enabled = true;
            MessageBox.Show("Server Started");
        }

        void server_CloseConnectionEvent(AbstractTcpSocketClientHandler handler)
        {
            MessageBox.Show(string.Format("A client is disconnected from the server"), "Socket Server", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        void server_ConnectionEvent(AbstractTcpSocketClientHandler handler)
        {
            MessageBox.Show(string.Format("A client is connected to the server"), "Socket Server", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        void server_ReceiveMessageEvent(SocketServerLib.SocketHandler.AbstractTcpSocketClientHandler handler, SocketServerLib.Message.AbstractMessage message)
        {
            BasicMessage receivedMessage = (BasicMessage)message;
            byte[] buffer = receivedMessage.GetBuffer();
            if (buffer.Length > 1000)
            {
                MessageBox.Show(string.Format("Received a long message of {0} bytes", receivedMessage.MessageLength), "Socket Server", 
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            string s = System.Text.ASCIIEncoding.Unicode.GetString(buffer);
            this.SetReceivedText(s);
        }

        private void buttonStop_Click(object sender, EventArgs e)
        {
            this.server.Shutdown();
            this.server.Dispose();
            this.server = null;
            this.buttonStart.Enabled = true;
            this.buttonStop.Enabled = false;
            this.buttonStop.Enabled = false;
            MessageBox.Show("Server Stopped");
        }

        private void SetReceivedText(string text)
        {
            if (this.textBoxReceived.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetReceivedText);
                this.Invoke(d, new object[] { text });
            }
            else
            {
                this.textBoxReceived.Text = text;
            }
        }

        private void buttonSend_Click(object sender, EventArgs e)
        {
            ClientInfo[] clientList = this.server.GetClientList();
            if (clientList.Length == 0)
            {
                MessageBox.Show("The client is not connected", "Socket Server", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            AbstractTcpSocketClientHandler clientHandler = clientList[0].TcpSocketClientHandler;
            string s = this.textBoxSend.Text;
            byte[] buffer = System.Text.ASCIIEncoding.Unicode.GetBytes(s);
            BasicMessage message = new BasicMessage(this.serverGuid, buffer);
            clientHandler.SendAsync(message);
        }
    }
}

以下是nodejs客户端代码。

var sys = require("sys"),
    net = require("net");

var client = net.createConnection(2147);
client.setEncoding("UTF8");

client.addListener("connect", function() {
    sys.puts("Client connected.");
    // close connection after 2sec
    setTimeout(function() {
        sys.puts("Sent to server: close");
        client.write("close", "UTF8");
    }, 2000);
});

client.addListener("data", function(data) {
    sys.puts("Response from server: " + data);
    if (data == "close") client.end();
});

client.addListener("close", function(data) {
    sys.puts("Disconnected from server");
});

1 个答案:

答案 0 :(得分:0)

我能够解决这个问题。这只是一个被忽视的问题。在服务器代码中,我使用分配给我的机器的局域网地址,但在客户端,我使用的是127.0.0.1。当我将两者都改为相同的值时,我没有得到econnrefused错误。 现在我能够发送和接收数据。但是我经常遇到ECONNRESET错误。任何线索?