Socket編輯基礎tcp通訊

來源:互聯網
上載者:User

 

SocketServer 端應用程式

static void Main(string[] args)
        {

            IPAddress address = IPAddress.Parse("127.0.0.1");
            IPEndPoint point = new IPEndPoint(address, 3000);
          
            Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            server.Bind(point);
            server.Listen(3);
            Socket client;
            byte[] buffer = new byte[1024];
            int i = 0;
            string data;
            Console.WriteLine("等待串連...");
            if ((client = server.Accept()) != null)//進程等待
            {
                Console.WriteLine("使用者已串連...");
                while (true)
                {
                    if ((i = client.Receive(buffer)) != 0)//進程等待
                    {
                        data = System.Text.Encoding.UTF8.GetString(buffer, 0, i);
                        Console.WriteLine("收到的資訊為{0}", data);
                        Console.Write("請輸入回複資訊:");
                        string input = Console.ReadLine();
                        client.Send(System.Text.Encoding.UTF8.GetBytes(input));
                    }
                }
            }

            Console.ReadLine();
        }

//*************************8888

socketClient用戶端程式

  static void Main(string[] args)
        {
            IPAddress address = IPAddress.Parse("127.0.0.1");
          
            IPEndPoint point = new IPEndPoint(address, 3000);
            Socket Client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            Client.Connect(point);//進程等待
            Console.WriteLine("已串連到伺服器...");
            byte[] buffer = new byte[1024];
            string data = null;
            while (true)
            {
                Console.Write("請輸入要發送的資訊:");
                string input;
                input = Console.ReadLine();
                if (input == "exit")
                    break;
                Client.Send(System.Text.Encoding.UTF8.GetBytes(input));
              
                int i = 0;
                i = Client.Receive(buffer);//進程等待
                if (i > 0)
                {
                    Console.Write("伺服器回複資訊:");
                    data = System.Text.Encoding.UTF8.GetString(buffer, 0, i);
                    Console.WriteLine(data);
                }
            }
            Console.WriteLine("中斷連線...");
            Client.Shutdown(SocketShutdown.Both);
            Client.Close();
        }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.