net7下的tcpip示例

2023-05-27测试 ,直接用百度文心一言搜索的,结果出来的代码能运行得通,不错不错

服务器端:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System.Net;
using System.Net.Sockets;
using System.Text;
 
namespace tcpipdemo_server;
class Program
{
    static void Main(string[] args)
    {
            // 创建 TCP 服务端实例 
            TcpListener listener = new TcpListener(IPAddress.Parse("127.0.0.1"), 8208);
 
            try
            {
                // 开始监听连接请求 
                listener.Start();
                Console.WriteLine("服务端已启动,等待客户端连接...");
 
                // 循环处理客户端连接请求和数据 
                while (true)
                {
                    // 接受客户端连接请求 
                    TcpClient client = listener.AcceptTcpClient();
                    Console.WriteLine("已连接来自 {0}:{1}", client.Client.RemoteEndPoint, client.Client);
 
                    // 获取网络流并获取数据 
                    NetworkStream stream = client.GetStream();
                    byte[] data = new byte[1024];
                    int length = stream.Read(data, 0, data.Length);
                    string message = Encoding.UTF8.GetString(data, 0, length);
                    Console.WriteLine("收到数据:{0}", message);
 
                    // 处理数据并发送响应数据到客户端 
                    Thread thread = new Thread(() =>
                    {
                        // 创建新的网络流并写入响应数据到客户端 
                        using (NetworkStream stream = client.GetStream())
                        {
                            string response = "Hello, Client!";
                            byte[] data = Encoding.UTF8.GetBytes(response);
                            stream.Write(data, 0, data.Length);
                            Console.WriteLine("已发送响应数据:{0}", response);
                        }
                    });
                    thread.Start();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("发生异常:{0}", ex);
            }
            finally
            {
                // 停止监听连接请求并关闭连接 
                listener.Stop();
            }
    
    }
}

 

 客户端:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using System.Net;
using System.Net.Sockets;
using System.Text;
 
namespace tcpipdemo;
public class Program
{
    static void Main(string[] args)
    {
        // 创建 TCP 客户端实例 
        TcpClient client = new TcpClient();
 
        try
        {
            // 连接到服务端 
            client.Connect("127.0.0.1", 8208);
            Console.WriteLine("连接已建立");
 
            // 获取网络流 
            NetworkStream stream = client.GetStream();
 
            // 发送数据到服务端 
            string message = "Hello, Server!";
            byte[] data = Encoding.UTF8.GetBytes(message);
            stream.Write(data, 0, data.Length);
            Console.WriteLine("已发送数据:{0}", message);
 
            // 从服务端接收数据 
            data = new byte[1024];
            int length = stream.Read(data, 0, data.Length);
            message = Encoding.UTF8.GetString(data, 0, length);
            Console.WriteLine("已接收数据:{0}", message);
        }
        catch (Exception ex)
        {
            Console.WriteLine("发生异常:{0}", ex);
        }
        finally
        {
            // 关闭连接 
            client.Close();
        }
    }
 
}

 

 

 测试结果:客户端发消息到服务器端,服务器端返回消息

 

 

posted @   牛腩  阅读(49)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2017-05-27 log4net使用流程
2017-05-27 C#判断访问网站的设备类型
2017-05-27 webapi中取文件的物理路径(server.mappath)
点击右上角即可分享
微信分享提示