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 ...
随机推荐
- java自定义注释及其信息提取
转自:https://xuwenjin666.iteye.com/blog/1637247 1. 自定义注解 import java.lang.annotation.Retention; import ...
- RTSP协议介绍 (转)
1. 实 时流协议RTSP RTSP[3]协 议以客户服务器方式工作,它是一个多媒体播放控制协议,用来使用户在播放从因特网下载的实时数据时能够进行控制,如:暂停/继 续.后退.前进等.因此 RTSP ...
- synchronized底层实现原理
基于进入和退出管程(Monitor)对象实现,无论显式(Monitorenter Monitorexit)还是隐式都是如此.同步方法并不是由monitorenter和monitorexit ...
- 【转】SetWindowText 的用法
SetWindowTextW表示设置的字符串是WCHAR (双字节字符 )SetWindowTextA表示设置的字符串是CHAR (单字节字符 )SetWindowText表示设置的字符串是自动匹配当 ...
- springboot+mybatis +yml文件配置多数据源
记录一下java开发中多数据源的配置过程, 参考博客:https://blog.csdn.net/weinichendian/article/details/72903757,我在这里进行了整理,使用 ...
- 摘:J2EE开发环境搭建(1)——安装JDK、Tomcat、Eclipse
J2EE开发环境搭建(1)——安装JDK.Tomcat.Eclipse 1:背景 进公司用SSH(Struts,spring和hibernate)开发已经有两个月了,但由于一 直要么只负责表示层的开发 ...
- jpa之No property buyerOpenId found for type OrderMaster! Did you mean 'buyerOpenid'?
java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.conte ...
- js数组操作 求最大值,最小值,正序、倒叙大小值排序,去重复
var arr = [1,5,2,56,12,34,21,3,5] Math.min.apply({},arr) Math.max.apply({},arr) arr.sort((m,n)=>m ...
- 学习Spring-Data-Jpa(十二)---投影Projections-对查询结果的扩展
Spring-Data数据查询方法的返回通常的是Repository管理的聚合根的一个或多个实例.但是,有时候我们只需要返回某些特定的属性,不需要全部返回,或者只返回一些复合型的字段.Spring-D ...
- LeetCode 1027. Longest Arithmetic Sequence
原题链接在这里:https://leetcode.com/problems/longest-arithmetic-sequence/ 题目: Given an array A of integers, ...