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 ...
随机推荐
- Docker存储卷篇
Docker存储卷篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.写时复制(COW)机制 所谓写时复制的效果如上图所示: Docker镜像由多个只读层叠加而成,启动容器 ...
- Hbase的表结构中rowkey的设计---避免热点问题
热点问题 hbase 中的行是以 rowkey 的字典序排序的,这种设计优化了scan 操作,可以将相关的 行 以及会被一起读取的行 存取在临近位置,便于 scan . 然而,糟糕的 rowkey 设 ...
- mysql 增加列
alter table t_zhaosheng_chengji_is_finish add season_id int(4) default '1' not null COMMENT '招生季ID';
- Educational Codeforces Round 64 (Rated for Div. 2)题解
Educational Codeforces Round 64 (Rated for Div. 2)题解 题目链接 A. Inscribed Figures 水题,但是坑了很多人.需要注意以下就是正方 ...
- 使用gitlab下载代码(附常用命令)
Git是现在很多人常用的代码管理工具,这里有一些常用的命令详解,本人接触也不是很久,若有错误,请在评论指出,谢谢. 若计算机中没有安装GIT,可自行查找安装教程,十分简便. ①首先,我们需要下载项目, ...
- js里调用函数时,函数名带括号与不带括号的区别
function test(){ return 1;}var a=test;console.log(a);//输出[Function: test]var b=test();console.log(b) ...
- LeetCode 1105. Filling Bookcase Shelves
原题链接在这里:https://leetcode.com/problems/filling-bookcase-shelves/ 题目: We have a sequence of books: the ...
- Kali Linux 2019.4中文乱码解决
1.先换源deb http://mirrors.aliyun.com/kali kali-rolling main non-free contribdeb-src http://mirrors.ali ...
- graphql-query-rewriter 无缝处理graphql 变更
graphql-query-rewriter 是一个graphql schema 变动重写的中间件,可以帮助我们解决在版本变动,查询实体变动 是的问题,从目前已知的技术中我们可选的方案有以下处理变动的 ...
- Linux 重启 PHP-FPM 命令
1. 停止命令 pkill php-fpm 2.重启或启动命令 php-fpm -R