TcpListener & TcpClient
2016-06-09 16:15
375 查看
public class Program { public static void Main(string[] args) { StartListener(); Console.ReadLine(); } public static async void StartListener() { IPAddress localAddr = IPAddress.Parse("127.0.0.1"); int port = 2112; TcpListener tcpListener = new TcpListener(localAddr, port); tcpListener.Start(); while (true) { using (TcpClient tcpClient = await tcpListener.AcceptTcpClientAsync()) { using (NetworkStream ns = tcpClient.GetStream()) 8c5c { var recvBuffer = new byte[1256]; int received = await ns.ReadAsync(recvBuffer, 0, recvBuffer.Length); string result = string.Empty; System.Text.UTF8Encoding utf8 = new System.Text.UTF8Encoding(); result = utf8.GetString(recvBuffer); Console.WriteLine(result); } } } } }
public class Program { public static void Main(string[] args) { TestClient(); Console.ReadLine(); } public static async void TestClient() { TcpClient tcpClient = new TcpClient(AddressFamily.InterNetwork); await tcpClient.ConnectAsync("127.0.0.1", 2112); NetworkStream ns = tcpClient.GetStream(); FileStream fs = File.Open("Program.cs", FileMode.Open); int data = fs.ReadByte(); while (data != -1) { ns.WriteByte((byte)data); data = fs.ReadByte(); } fs.Dispose(); ns.Dispose(); tcpClient.Dispose(); } }
相关文章推荐
- 三体争霸网络游戏开发之一:redis下载连接使用
- 三体争霸网络游戏开发之前言
- 那些不能遗忘的知识点回顾——计算机网络系列(笔试面试高频题)
- 计算机网络面试常考知识点整理
- TCP拥塞控制机制
- TCP/IP协议三次握手与四次握手流程解析
- QT TCP/IP
- android中网络连接状态下跳转网页
- 网络留言板1
- Ubuntu网络配置
- voip 网络电话 sipdroid 的移植上出现的问题
- 第二十七、二十八天:网络数据封装及各层数据的发送
- socket通信,tcp,udp通信
- TCP/IP详解阅读笔记(一):TCP协议
- HTTP协议响应消息的常用状态码【转】
- HTTPS理论基础及其在Android中的最佳实践
- etrace 跟踪 nginx之HTTP请求流程
- http://yxonic.blog.ustc.edu.cn/2015/07/python-asyn
- http://my.oschina.net/u/2352897/blog
- asp.net基础-http运行期执行概述