激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - C# - C#使用TcpListener及TcpClient開(kāi)發(fā)一個(gè)簡(jiǎn)單的Chat工具實(shí)例

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

2022-02-15 16:47cnc C#

下面小編就為大家分享一篇C#使用TcpListener及TcpClient開(kāi)發(fā)一個(gè)簡(jiǎn)單的Chat工具實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

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

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

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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;
   }
  }
 }
}

二、編寫(xiě)客戶端代碼,如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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開(kāi)發(fā)一個(gè)簡(jiǎn)單的Chat工具實(shí)例

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

以上這篇c#使用tcplistener及tcpclient開(kāi)發(fā)一個(gè)簡(jiǎn)單的chat工具實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。

原文鏈接:http://www.cnblogs.com/cncc/p/7891728.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 97久久精品一区二区三区观看 | 日美av在线| 奶子吧naiziba.cc免费午夜片在线观看 | 亚洲aⅴ免费在线观看 | 免费看国产视频 | 久久国产精品二国产精品中国洋人 | 免费在线观看成人网 | 亚洲成人国产综合 | 日韩黄色片网站 | 日韩美香港a一级毛片免费 欧美一级淫片007 | 羞羞网站 | 色七七久久影院 | 亚洲国产成人一区 | 午夜天堂在线 | 亚洲欧美国产高清 | 国产成人精品午夜 | 国产乱淫av片免费网站 | 黄色成人在线播放 | 中国免费黄色 | 日本成人一区 | 99精品视频免费看 | 欧美三级日本三级少妇99 | 中文字幕网站在线 | 亚洲免费网站 | 欧美日韩高清不卡 | 亚洲日本欧美 | 九草av| 欧美精品网址 | 中国美女一级黄色片 | 日本成人在线免费 | 久久精品国产99国产精品澳门 | 国产精品久久久久久久久久 | 国产一区精品在线观看 | 久久久久久艹 | 欧美在线成人影院 | 亚洲午夜久久久精品一区二区三区 | 成人综合区一区 | av色先锋| 国产精品剧情一区二区在线观看 | 91精品国产91| 97超视频在线观看 |