C# TcpListener TcpClient
2024-08-31 21:44:01
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;
}
}
}
}
测试结果如下:
最新文章
- 单链表、循环链表的JS实现
- 在Autodesk Vault 2014中使用VDF(Vault Development Framework) API获取所有文件的属性信息
- 淘淘商城maven工程的创建和svn的上传实现
- 405 Method Not Allowed
- Resources
- (转).NET代码混淆实践
- jQuery练习一好友列表变色
- include a image in devexpress datagrid
- flex——dictionary跟Object的区别与遍历
- Jmeter-基于Ubuntu运行
- (NO.00001)iOS游戏SpeedBoy Lite成形记(三)
- 使用VS的生成事件命令行指令将生成的exe,dll文件复制到指定文件夹中
- C#压缩文件,C#压缩文件夹,C#获取文件
- 解决wxParse空格不解析的问题
- 详细解读Android中的搜索框(三)—— SearchView
- mysql之 redo log
- CSS-解决苹果点击高亮、安卓select灰色背景(select下拉框在IOS中背景变黑、出现阴影问题)
- 【Beta阶段】第四次Scrum Meeting!
- 【Java】PS-查看Java进程-线程数
- 转载:Remote Validation
热门文章
- python通过装饰器检查函数参数的数据类型的代码
- Audio Queue Services Programming Guide(音频队列服务编程指南)
- Hybris服务器启动日志分析
- 【RAC】 RAC For W2K8R2 安装--共享磁盘的配置(三)
- 27.centos7基础学习与积累-013-文件和目录的权限
- svn 没有killall命令的解决方法 -bash: killall: command not found
- js面向对象--由浅入深
- 学.Net Core Web Api开发 ---- 系列文章
- python 打印html源码中xpath
- 各大公司Java面试题收录含答案(整理版)持续中....