最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#使用TcpListener及TcpClient開發(fā)一個簡單的Chat工具實(shí)例

 更新時間:2017年12月09日 09:55:17   作者:cnc  
下面小編就為大家分享一篇C#使用TcpListener及TcpClient開發(fā)一個簡單的Chat工具實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

本文使用的開發(fā)環(huán)境是VS2017及dotNet4.0,寫此隨筆的目的是給自己及新開發(fā)人員作為參考,

本例子比較簡單,使用的是控制臺程序開發(fā),若需要使用該軟件作為演示,必須先運(yùn)行服務(wù)端,再運(yùn)行客戶端。

因為是首次接觸該方面的知識,寫得比較簡陋,如有更好的建議,請?zhí)岢?,謝謝!

一、編寫服務(wù)器端代碼,如下:

using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
namespace ChatServer
{
 class Program
 {
  static void Main(string[] args)
  {
   bool cancel = false;
   byte[] buffer = new byte[1024];
   string message;
   byte[] messageBytes;
   int count = 0;
   TcpListener tcpListener = new TcpListener(new IPEndPoint(IPAddress.Any, 13000));
   tcpListener.Start();
   Console.WriteLine("Waiting for a connection... ");
   TcpClient tcpClient = tcpListener.AcceptTcpClient();
   Console.WriteLine("Connected.");
   NetworkStream stream = tcpClient.GetStream();
   
   Task.Factory.StartNew(() => 
   {
    while ((count = stream.Read(buffer, 0, buffer.Length)) != 0)
    {
     Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss fff} Reply from server {tcpClient.Client.LocalEndPoint.ToString()}:{Encoding.UTF8.GetString(buffer, 0, count)}");
    }
   });
     
   Task t = Task.Factory.StartNew(() => 
   {
    while(!cancel)
    {
     message = Console.ReadLine();
     if (message.ToUpper() == "Y")
     {
      cancel = true;
      return;
     }
     messageBytes = Encoding.UTF8.GetBytes(message);
     stream.Write(messageBytes, 0, messageBytes.Length);
    }
   });
      
   if (cancel) tcpClient.Close();
    
   while (true)
   {
    if (t != null && t.IsCompleted) break;
   }
  }
 }
}

二、編寫客戶端代碼,如下:

using System;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
namespace ChatClient
{
 class Program
 {
  static void Main(string[] args)
  {
   bool cancel = false;
   byte[] buffer = new byte[1024];
   string message;
   byte[] messageBytes;
   int count = 0;
   try
   {
    TcpClient tcpClient = new TcpClient(new IPEndPoint(Dns.GetHostEntry(Dns.GetHostName()).AddressList.Where(p => p.AddressFamily == AddressFamily.InterNetwork).First(), 14000));
    tcpClient.Connect(new IPEndPoint(IPAddress.Parse("192.168.94.26"), 13000));
    NetworkStream stream = tcpClient.GetStream();
    
    Task.Factory.StartNew(() =>
    {
     while ((count = stream.Read(buffer, 0, buffer.Length)) != 0)
     {
      Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss fff} Reply from client {tcpClient.Client.LocalEndPoint.ToString()}:{Encoding.UTF8.GetString(buffer, 0, count)}");
     }
    });
    Task t = Task.Factory.StartNew(() =>
    {
     while (!cancel)
     {
      message = Console.ReadLine();
      if (message.ToUpper() == "Y")
      {
       cancel = true;
       return;
      }
      messageBytes = Encoding.UTF8.GetBytes(message);
      stream.Write(messageBytes, 0, messageBytes.Length);
      Thread.Sleep(10);
     }
    });
    if (cancel) tcpClient.Close();
    
    while (true)
    {
     if (t != null && t.IsCompleted) break;
    }
   }
   catch(Exception ex)
   {
    Console.WriteLine(ex.Message);
    Console.ReadKey();
   }
   } 
 }
}

三、先運(yùn)行服務(wù)端代碼,后再另外一臺電腦運(yùn)行客戶端代碼,效果圖如下:

以上這篇C#使用TcpListener及TcpClient開發(fā)一個簡單的Chat工具實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

六枝特区| 彰武县| 邵阳市| 宜昌市| 宁都县| 洛川县| 承德县| 通山县| 德保县| 大竹县| 罗甸县| 康马县| 山丹县| 德令哈市| 长治市| 阳曲县| 普陀区| 宝清县| 明光市| 望城县| 新安县| 丰顺县| 云浮市| 衢州市| 集贤县| 黄大仙区| 米泉市| 临漳县| 南木林县| 理塘县| 蒙阴县| 南和县| 汶川县| 金坛市| 碌曲县| 高青县| 崇文区| 石阡县| 盘锦市| 大英县| 陈巴尔虎旗|