欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C#教程

當(dāng)前位置:主頁(yè) > 軟件編程 > C#教程 >

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

來源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C#教程|點(diǎn)擊: 次

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

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

因?yàn)槭鞘状谓佑|該方面的知識(shí),寫得比較簡(jiǎn)陋,如有更好的建議,請(qǐng)?zhí)岢觯x謝!

一、編寫服務(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ù)端代碼,后再另外一臺(tái)電腦運(yùn)行客戶端代碼,效果圖如下:

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

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語(yǔ)言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有