📄 class1.cs
字号:
using System;
using System.Net.Sockets;
using System.IO;
using System.Net;
namespace ConsoleApplication1
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
// [STAThread]
public static bool islink = false;
public static NetworkStream netStream;
public static StreamReader sr;
public static TcpClient tcpClient;
static void Main(string[] args)
{
int port=2005;//本地待侦听端口
IPAddress localAddr = IPAddress.Parse("192.168.1.139");
TcpListener serverListener=new System.Net.Sockets.TcpListener(localAddr,port);//创建TcpListener对象实例
serverListener.Start(); //启动侦听
islink=true;
while(true)//进入无限循环等待用户端连接
{
try
{
Console.Write("Waiting for a connection... ");
tcpClient=serverListener.AcceptTcpClient();//创建客户端连接对象
Console.WriteLine("\n");
Console.WriteLine("Connected!");
netStream=tcpClient.GetStream();//得到网络流
sr=new StreamReader(netStream);//流读写器
string received="";
received += sr.ReadLine();//读流中一行
Console.WriteLine(received);
sr.Close();
netStream.Close();
tcpClient.Close();
}
catch(Exception re)
{
Console.WriteLine(re.Message);
Console.Read();
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -