本文使用的開發(fā)環(huán)境是VS2017及dotNet4.0,寫此隨筆的目的是給自己及新開發(fā)人員作為參考,
成都創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),沙縣企業(yè)網(wǎng)站建設(shè),沙縣品牌網(wǎng)站建設(shè),網(wǎng)站定制,沙縣網(wǎng)站建設(shè)報價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,沙縣網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。
本例子比較簡單,使用的是控制臺程序開發(fā),若需要使用該軟件作為演示,必須先運行服務(wù)端,再運行客戶端。
因為是首次接觸該方面的知識,寫得比較簡陋,如有更好的建議,請?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();
}
}
}
}三、先運行服務(wù)端代碼,后再另外一臺電腦運行客戶端代碼,效果圖如下:


以上這篇C#使用TcpListener及TcpClient開發(fā)一個簡單的Chat工具實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持創(chuàng)新互聯(lián)。
本文題目:C#使用TcpListener及TcpClient開發(fā)一個簡單的Chat工具實例
本文網(wǎng)址:http://chinadenli.net/article48/jpsgep.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、搜索引擎優(yōu)化、外貿(mào)網(wǎng)站建設(shè)、動態(tài)網(wǎng)站、手機網(wǎng)站建設(shè)、網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)