[C#]使用TcpListener及TcpClient开发一个简单的Chat工具
2017-11-24 17:53
555 查看
本文为原创文章、源代码为原创代码,如转载/复制,请在网页/代码处明显位置标明原文名称、作者及网址,谢谢!
本文使用的开发环境是VS2017及dotNet4.0,写此随笔的目的是给自己及新开发人员作为参考,
本例子比较简单,使用的是控制台程序开发,若需要使用该软件作为演示,必须先运行服务端,再运行客户端。
因为是首次接触该方面的知识,写得比较简陋,如有更好的建议,请提出,谢谢!
一、编写服务器端代码,如下:
二、编写客户端代码,如下:
三、先运行服务端代码,后再另外一台电脑运行客户端代码,效果图如下:
本文使用的开发环境是VS2017及dotNet4.0,写此随笔的目的是给自己及新开发人员作为参考,
本例子比较简单,使用的是控制台程序开发,若需要使用该软件作为演示,必须先运行服务端,再运行客户端。
因为是首次接触该方面的知识,写得比较简陋,如有更好的建议,请提出,谢谢!
一、编写服务器端代码,如下:
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(); } } } }
三、先运行服务端代码,后再另外一台电脑运行客户端代码,效果图如下:
相关文章推荐
- C#使用TcpListener及TcpClient开发一个简单的Chat工具实例
- 【C#】使用TcpListener和TcpClient实现简单通信
- 简介 WatiN是一个开源的用于Web测试自动化的类库,从watir中获得的灵感,使用C#开发。WatiN通过与浏览器的交互来实现自动化,使用起来具有轻便,简单的特点。目前最新版本为2.0,加入了对
- 使用C#开发一个简单的P2P应用
- 使用C#开发一个简单的P2P应用
- 使用C#开发一个简单的P2P应用
- 使用C#开发一个简单的P2P应用
- 转载一个很经典的--C# Socket TCP和UDP报文及端口测试工具的开发(提供源码)
- 使用C#开发一个简单的P2P应用
- 使用C#开发一个简单的P2P应用
- 使用C#开发一个简单的P2P应用
- 使用C#开发一个简单的P2P应用
- [转]使用C#开发一个简单的P2P应用
- 使用C#开发一个简单的P2P应用
- 使用C#开发一个简单的P2P应用
- 使用C#开发一个简单的P2P应用
- 使用C#开发一个简单的P2P应用
- 使用C#开发一个简单的P2P应用
- 一个简单的动态编译器,支持C#和VB.NET。当你想测试一些简单的代码的时候可以使用。
- C#开发一个简单的P2P应用