C# TcpListener TcpClient
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;
}
}
}
}
测试结果如下:
C# TcpListener TcpClient的更多相关文章
- Socket聊天室-TcpListener,TcpClient
参考自:http://blog.csdn.net/liguo9860/article/details/6148614 服务端:
- 【socket】一分钟理清 socket udpsocket tcpsocket tcplistener TCPClient和 UDPClient
socket 套接字接口是各种语言tcp udp的网络操作的基础. 直接用socket 对象开发 可以选择 udpsocket 或者 tcpsocket ,两者在使用上仅一些方法和参数不同,所有的底 ...
- .net 网络编程
1.首先说下计算机网络中的TCP/IP参考模型 TCP/IP把网络分为5层,每一层负责完成不同的功能 1)应用层:传输报文,提供各种网络应用,有FTP.SMTP.HTTP等协议 2)运输层:传输报文段 ...
- TCP同步传送数据示例以及可能出现问题分析
TCP传送数据可以分为同步传送和异步传送,首先这里使用了TCP的同步传送方式,学习了TCP同步传送数据的原理. 同步工作方式是指利用TCP编写的程序执行到监听或者接受数据语句的时候,在未完成当前工作( ...
- Simple TCP/IP Echo Server & Client Application in C#
1. TCP Server The server’s job is to set up an endpoint for clients to connect to and passively wait ...
- TCP listener
调试时未关闭之前开启的TCP SERVER: 应该关闭: TcpListener TcpClient 1.开启TCP listener (1)Start()方法失败 异常位置:该异常的产生位置为 tc ...
- C# - 网络编程 之 TcpClient与TcpListener
TcpClient类 TcpListener类 TCP通信 UDP通信 参考:
- TcpClient类与TcpListener类
TcpClient类 //构造方法1 TcpClient t = new TcpClient(); t.Connect(); //构造方法2 IPEndPoint iep = ); TcpClient ...
- Socket的三个功能类TCPClient、TCPListener 和 UDPClient (转)
应用程序可以通过 TCPClient.TCPListener 和 UDPClient 类使用传输控制协议 (TCP) 和用户数据文报协议 (UDP) 服务.这些协议类建立在 System.Net.So ...
随机推荐
- S/4HANA Service Management和SAP Field Service Management的集成
经常有朋友提出这样的问题:"SAP Business Suite里的CRM的Service模块已经通过Addon的方式迁移到了S/4HANA上,并且SAP之前又收购了一家专门做Service ...
- Linux 服务控制与运行级别
如何控制服务的运行状态?如何切换不同的运行级别? 服务控制 ntsysv 仿图形交互界面,集中配置各种服务启动状态 --level 35:同时对指定运行级别中的服务进行管理,不加仅管理当前运行级别中的 ...
- 自动化运维-ansible入门篇
1.ansible配置 什么是Ansible IT自动化工具 依赖于现有的操作系统凭证来访问控制远程机器 简单易用.安全可靠 Ansible可以完成哪些任务 配置系统 开发软件 编排高级的IT任务 A ...
- 获取ul li的value的值
<script> $(function(){ $(".month-list").find("li").click(function(){ var t ...
- easyui 自己写的一些小东西
1设置combobox,当我们只需要显示一个commbobox的时候,并且默认选择第一项 function Getcombobox(comboId, value, groupNo) { $('#' + ...
- AST11103 Problem Solving
AST11103 Problem Solving with Programming SkillsAdditional Individual Assignment: Min-Game Programmi ...
- Scanner的常用用法
通过new Scanner(System.in)创建一个Scanner,控制台会一直等待输入,直到敲回车键结束,把所输入的内容传给Scanner. s.useDelimiter(" |,|\ ...
- Windows10 Faster R-CNN(GPU版) 配置训练自己的模型
参考链接 1. 找到合适自己的版本,下载安装Anaconda 点击跳转下载安装 Anaconda,双击下载好的 .exe 文件安装,只勾选第一个把 conda 添加到 PATH 路径.
- php Web 项目的文件/文件夹上传下载
PHP用超级全局变量数组$_FILES来记录文件上传相关信息的. 1.file_uploads=on/off 是否允许通过http方式上传文件 2.max_execution_time=30 允许脚本 ...
- learning scala akka actorySystem create and close
package com.example import akka.actor.ActorSystem import scala.util.{Failure, Success} import scala. ...