C# TcpListener TcpClient 使用,新建从控制台项目,引用System.Net

代码如下:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
//using System.IO.Pipes;
using System.Net;
using System.Net.Mail;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions; namespace LongtengSupremeConsole
{
class Program
{
static void Main(string[] args)
{
new Thread(new ThreadStart(TcpListenerMethod)).Start();
Thread.Sleep(TimeSpan.FromSeconds());
TcpClientSendDataMethod();
Console.ReadKey();
}
public static void TcpClientSendDataMethod()
{
try
{
int i = ;
while (true)
{
TcpClient client = new TcpClient();
client.Connect("127.0.0.1", );
using (NetworkStream n = client.GetStream())
{
BinaryWriter binaryWriter = new BinaryWriter(n);
binaryWriter.Write($"{i}");
binaryWriter.Flush();
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"客户端 发送数据是:{i}");
string binaryReader = new BinaryReader(n).ReadString();
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine($"客户端 收到数据是:{binaryReader}");
//using (StreamWriter streamWriter = new StreamWriter(n))
//{
// Console.ForegroundColor = ConsoleColor.Yellow;
// Console.WriteLine($"客户端 发送数据是:{i}");
// streamWriter.WriteLine("{i}");
//}
//using (StreamReader streamReader = new StreamReader(n))
//{
// Console.ForegroundColor = ConsoleColor.Green;
// Console.WriteLine($"客户端 收到数据是:{streamReader.ReadToEnd()}");
//} i++;
}
Thread.Sleep(TimeSpan.FromSeconds());
}
}
catch (Exception)
{ throw;
}
} public static TcpListener listener = null; public static void TcpListenerMethod()
{
try
{
//listener = new TcpListener(new IPEndPoint(new IPAddress(new byte[] { 127, 0, 0, 1 }), 49153));
listener = new TcpListener(IPAddress.Any, );
//listener.Server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
listener.Start();
while (true)
{
using (TcpClient c = listener.AcceptTcpClient())
{
//c.LingerState.Enabled = false;
using (NetworkStream n = c.GetStream())
{
string binaryReader = new BinaryReader(n).ReadString();
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine($"服务端 收到数据是:{binaryReader}"); BinaryWriter binaryWriter = new BinaryWriter(n);
binaryWriter.Write($"服务端 回复 {binaryReader}");
binaryWriter.Flush();
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"服务端 回复 {binaryReader}"); //using (StreamReader streamReader = new StreamReader(n))
//{
// Console.ForegroundColor = ConsoleColor.Green;
// Console.WriteLine($"服务端:收到数据:{streamReader.ReadToEnd()}");
//}
//using (StreamWriter streamWriter = new StreamWriter(n))
//{
// Console.ForegroundColor = ConsoleColor.Yellow;
// Console.WriteLine($"服务端:发送数据:我已经收到数据了");
// streamWriter.WriteLine("我已经收到数据了。。。。");
//}
}
}
}
}
catch (Exception)
{
listener.Stop();
throw;
}
} public static void TcpListenerStopMethod()
{
try
{
listener.Stop();
}
catch (Exception)
{
throw;
}
}
}
}

测试结果如下:

最新文章

  1. 单链表、循环链表的JS实现
  2. 在Autodesk Vault 2014中使用VDF(Vault Development Framework) API获取所有文件的属性信息
  3. 淘淘商城maven工程的创建和svn的上传实现
  4. 405 Method Not Allowed
  5. Resources
  6. (转).NET代码混淆实践
  7. jQuery练习一好友列表变色
  8. include a image in devexpress datagrid
  9. flex——dictionary跟Object的区别与遍历
  10. Jmeter-基于Ubuntu运行
  11. (NO.00001)iOS游戏SpeedBoy Lite成形记(三)
  12. 使用VS的生成事件命令行指令将生成的exe,dll文件复制到指定文件夹中
  13. C#压缩文件,C#压缩文件夹,C#获取文件
  14. 解决wxParse空格不解析的问题
  15. 详细解读Android中的搜索框(三)—— SearchView
  16. mysql之 redo log
  17. CSS-解决苹果点击高亮、安卓select灰色背景(select下拉框在IOS中背景变黑、出现阴影问题)
  18. 【Beta阶段】第四次Scrum Meeting!
  19. 【Java】PS-查看Java进程-线程数
  20. 转载:Remote Validation

热门文章

  1. python通过装饰器检查函数参数的数据类型的代码
  2. Audio Queue Services Programming Guide(音频队列服务编程指南)
  3. Hybris服务器启动日志分析
  4. 【RAC】 RAC For W2K8R2 安装--共享磁盘的配置(三)
  5. 27.centos7基础学习与积累-013-文件和目录的权限
  6. svn 没有killall命令的解决方法 -bash: killall: command not found
  7. js面向对象--由浅入深
  8. 学.Net Core Web Api开发 ---- 系列文章
  9. python 打印html源码中xpath
  10. 各大公司Java面试题收录含答案(整理版)持续中....