C#網路編程執行個體

來源:互聯網
上載者:User

用戶端代碼

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace SocketClient
{
    class Program
    {
        static void Main(string[] args)
        {
            //儲存輸入要發送的字串
            string input;
            //要串連的遠程IP
            IPAddress remoteHost = IPAddress.Parse("172.16.86.90");
            //IP地址跟連接埠的組合
            IPEndPoint iep = new IPEndPoint(remoteHost, 8080);
            //把地址綁定到Socket
            Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //串連遠程伺服器
            try
            {
                clientSocket.Connect(iep);
                Console.WriteLine("請輸入您要發送的字串");
                //儲存輸入的字串
                input = Console.ReadLine();
                //用位元組數組儲存要發送的字串
                byte[] message = System.Text.Encoding.Unicode.GetBytes(input);
                //建立一個NetworkStream對象發送資料
                NetworkStream netstream = new NetworkStream(clientSocket);
                //向伺服器端發送message內容
                netstream.Write(message, 0, message.Length);
                clientSocket.Shutdown(SocketShutdown.Both);
                clientSocket.Close();
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("伺服器端串連失敗");
            }
            

           
            
        }
    }
}

 

 

伺服器端代碼

 

代碼

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace SocketServer
{
    class Program
    {
        static void Main(string[] args)
        {
            //本機IP
            IPAddress ip = IPAddress.Parse("172.16.86.90");
            //IP地址跟連接埠的組合
            IPEndPoint iep = new IPEndPoint(ip, 8080);
            //建立Socket
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //綁定Socket
            socket.Bind(iep);
            //伺服器已經做好接收任何串連的準備
            socket.Listen(10);
            while (true)
            {
                //執行accept方法
                Socket Client = socket.Accept();
                byte[] message = new byte[1024];
                NetworkStream networkStream = new NetworkStream(Client);
                int len = networkStream.Read(message, 0, message.Length);
                //byte數群組轉換成string
                string output = System.Text.Encoding.Unicode.GetString(message);
                Console.WriteLine("一共從用戶端接收了" + len.ToString() + "位元組。接收字串為:"+output);     
            }
            

            

        }
    }
}

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.