110,534
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ThreadPool.QueueUserWorkItem(h => receive());
ThreadPool.QueueUserWorkItem(h => send());
Console.WriteLine("..............................请按任意键退出");
Console.ReadLine();
}
private static void send()
{
var s = new TcpListener(IPAddress.Any, port);
s.Start();
var a = s.AcceptTcpClient();
using (var wf = new StreamWriter(a.GetStream()))
{
wf.WriteLine("abc");
wf.WriteLine("你好啊");
wf.WriteLine("じ✿ゞ");
}
}
private static string host = "127.0.0.1";
private static int port = 1234;
/// <summary>
/// 首先作为客户端连接,然后接收数据
/// </summary>
private static void receive()
{
var s = new TcpClient(host, port);
using (var r = new StreamReader(s.GetStream()))
{
while (!r.EndOfStream)
{
var message = r.ReadLine();
Console.WriteLine(message);
}
}
}
}
}
我们谈的是比较专业一点的开发。但是对于初学者,其实学的应该是这个。这就好像是幼儿园学的数学跟一个建筑工程师学的数学肯定是不一样的,幼儿园学的数学也是实用的,就像这里的程序也可以用来改一改就去应付。只不过这里的程序是有其适应场合、针对的人群的!