0001-TcpListener/TcpClient同步編程

在使用TcpListener/TcpClient同步編程之前,要引入兩個命名空間

using System.Net;
using System.Net.Sockets;

服務(wù)器代碼示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace Net
{
    class Program
    {
        static void Main(string[] args)
        {
            //服務(wù)端對端口進行偵聽:
            TcpListener listener = new TcpListener(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 10001));
            listener.Start();
            //檢測來自客戶端的連接請求
            Console.WriteLine("等待連接請求...........");
            TcpClient remoteClient = listener.AcceptTcpClient();
            Console.WriteLine("連接到客戶端!!!");
            //建立和連接的客戶端的數(shù)據(jù)流(傳輸數(shù)據(jù))
            NetworkStream streamFromClient = remoteClient.GetStream();
            //創(chuàng)建緩存區(qū)
            byte[] buffer = new byte[8096];

            //每次讀取緩存中的大小
            int byteRead;
            try
            {
                //為了保證數(shù)據(jù)的完整性以及安全性鎖定數(shù)據(jù)流
                //讀取客戶端發(fā)送來的數(shù)據(jù)
                lock (streamFromClient)
                {
                    byteRead = streamFromClient.Read(buffer, 0, 8096);
                    Console.WriteLine(DateTime.Now +":收到來自客戶端的消息:"+Encoding.Unicode.GetString(buffer, 0, byteRead));
                }

                //向連接的客戶端發(fā)送數(shù)據(jù)
                lock (streamFromClient)

                {
                    //buffer為發(fā)送的字符數(shù)組
                    streamFromClient.Write(buffer,0, buffer.Length);                  

                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            streamFromClient.Dispose();
            remoteClient.Close();

            Console.ReadLine();
        }
    }
}

客戶端代碼示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace Client
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpClient tcpClient = new TcpClient();
            tcpClient.Connect("127.0.0.1", 10001);
            if (tcpClient.Connected)
            {
                NetworkStream streamFromServer = tcpClient.GetStream();
                byte[] buffer = Encoding.Unicode.GetBytes("hello world Unity");
                int byteRead;
                try
                {
                    lock (streamFromServer)
                    {
                        //發(fā)送到服務(wù)器
                        streamFromServer.Write(buffer, 0, buffer.Length);
                        buffer = new byte[8096];
                        
                    }
                    lock (streamFromServer)
                    {
                        byteRead = streamFromServer.Read(buffer, 0, 8096);
                        Console.WriteLine(DateTime.Now + "收到來自服務(wù)器的消息:" + Encoding.Unicode.GetString(buffer, 0, byteRead));
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
            Console.ReadLine();
        }
    }
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 計算機網(wǎng)絡(luò)概述 網(wǎng)絡(luò)編程的實質(zhì)就是兩個(或多個)設(shè)備(例如計算機)之間的數(shù)據(jù)傳輸。 按照計算機網(wǎng)絡(luò)的定義,通過一定...
    蛋炒飯_By閱讀 1,363評論 0 10
  • 1.網(wǎng)絡(luò)編程1.1計算機網(wǎng)絡(luò)概述網(wǎng)絡(luò)編程的實質(zhì)就是兩個(或多個)設(shè)備(例如計算機)之間的數(shù)據(jù)傳輸。按照計算機網(wǎng)絡(luò)的...
    任振銘閱讀 474評論 0 1
  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML標(biāo)準(zhǔn)。 注意:講述HT...
    kismetajun閱讀 28,769評論 1 45
  • Swift1> Swift和OC的區(qū)別1.1> Swift沒有地址/指針的概念1.2> 泛型1.3> 類型嚴(yán)謹(jǐn) 對...
    cosWriter閱讀 11,619評論 1 32
  • 聽著這首借口,覺得想哭,今天和吳總吐露心聲,也是鼓了很大勇氣,同時也為吳總呈現(xiàn)了一個不一樣的馬艷萍,她脆弱,有責(zé)任...
    就流逝的丹青色閱讀 626評論 0 0

友情鏈接更多精彩內(nèi)容