C# Socket TCP Server & Client & nodejs client

要調試公司某項目裏的一個功能,由於要準備測試環境,趁這個機會重溫了一下Socket(全還給老師了 -_-#),作個備份。javascript

C# Serverjava

static void Main(string[] args)
        {
            int port = 81;
            string host = "192.168.1.151";

            //建立終結點
            IPAddress ip = IPAddress.Parse(host);
            IPEndPoint ipe = new IPEndPoint(ip, port);

            //建立Socket並開始監聽

            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //建立一個Socket對象,若是用UDP協議,則要用SocketTyype.Dgram類型的套接字
            s.Bind(ipe);    //綁定EndPoint對象(2000端口和ip地址)
            s.Listen(0);    //開始監聽

            Console.WriteLine("等待客戶端鏈接");
            
            while (true)
            {
                //接受到Client鏈接,爲此鏈接創建新的Socket,並接受消息
                Socket temp = s.Accept();   //爲新創建的鏈接建立新的Socket
                Console.WriteLine("創建鏈接");
                string recvStr = "";
                byte[] recvBytes = new byte[1024];
                int bytes;
                bytes = temp.Receive(recvBytes, recvBytes.Length, 0); //從客戶端接受消息
                recvStr += Encoding.UTF8.GetString(recvBytes, 0, bytes);

                //給Client端返回信息
                Console.WriteLine("server get message:{0}", recvStr);    //把客戶端傳來的信息顯示出來
                string sendStr = "server receive message successful!";
                byte[] bs = Encoding.UTF8.GetBytes(sendStr);
                temp.Send(bs, bs.Length, 0);  //返回信息給客戶端
                temp.Close();
            }

            s.Close();
            Console.ReadLine();
            Console.Read();
        }

C# Clientnode

static void Main(string[] args)
        {
            int port = 81;
            string host = "192.168.1.151";
            //建立終結點EndPoint
            IPAddress ip = IPAddress.Parse(host);
            IPEndPoint ipe = new IPEndPoint(ip, port);   //把ip和端口轉化爲IPEndPoint的實例

            //向服務器發送信息
            string sendStr = string.Empty;

            while (true)
            {
                Console.WriteLine("請輸入要發送的內容...");
                sendStr = Console.ReadLine();
                if (string.IsNullOrWhiteSpace(sendStr))
                {
                    Environment.Exit(0);
                }
                else
                {
                    Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);   //  建立Socket
                    c.Connect(ipe); //鏈接到服務器
                    byte[] bs = Encoding.UTF8.GetBytes(sendStr);   //把字符串編碼爲字節

                    Console.WriteLine("Send message");
                    c.Send(bs, bs.Length, 0); //發送信息

                    //接受從服務器返回的信息
                    string recvStr = "";
                    byte[] recvBytes = new byte[1024];
                    int bytes;
                    bytes = c.Receive(recvBytes, recvBytes.Length, 0);    //從服務器端接受返回信息
                    recvStr += Encoding.UTF8.GetString(recvBytes, 0, bytes);
                    Console.WriteLine("client get message:{0}", recvStr);    //回顯服務器的返回信息
                    c.Disconnect(true);
                    //必定記着用完Socket後要關閉
                    c.Close();
                }
            }
        }

nodejs client服務器

var net = require('net');
var client = new net.Socket();
//client.setEncoding("utf8");
client.on('data', function(data) {
    console.log('DATA: ' + data);
});

client.on('error', function(err) {
    console.log('ERROR: ' + err);
});

client.on('end', function(data) {
    console.log('END: ' + data);
});

client.connect('81', '192.168.1.151', function () {
    console.log('connection success')
})

var msg = "absdfsf中文";
client.write(msg, "utf8", function(){
    console.log('send success....')
})
相關文章
相關標籤/搜索