c# TCPclient

服务端

 

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
60
61
62
63
64
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Threading;
 
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private TcpListener tcp;
        private void button1_Click(object sender, EventArgs e)
        {
            tcp = new TcpListener(IPAddress.Any, 5000);
            tcp.Start();
            Thread t = new Thread(server);
            t.IsBackground = true;
            t.Start(tcp);
        }
 
        private void server(object o)
        {
             
            TcpListener list = o as TcpListener;
            this.textBox1.AppendText("连接成功"+"\r\n");
            while (true)
            {
                const int buffer = 256;
                TcpClient client = list.AcceptTcpClient();
                NetworkStream strem = client.GetStream();
                byte[] b = new byte[buffer];
                int r = strem.Read(b, 0, buffer);
                string str = Encoding.UTF8.GetString(b, 0, r);
                 
                this.textBox2.AppendText(client.Client.RemoteEndPoint.ToString() + ":"+str+"\r\n");
                strem.Close();
 
            }
         
 
        }
 
 
        public void showmsg(string msg)
        {
            this.textBox2.AppendText(msg + "\r\n");
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            Control.CheckForIllegalCrossThreadCalls = false;
        }
    }
}

  客户端

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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Threading;
 
 
namespace client
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private TcpClient client;
        private void button1_Click(object sender, EventArgs e)
        {
            const int size = 406;
            client = new TcpClient();
            client.Connect(IPAddress.Parse("172.16.5.24"),5000);
            NetworkStream strem = client.GetStream();
            string str = this.textBox1.Text.Trim();
            byte[] b = Encoding.UTF8.GetBytes(str);
            strem.Write(b, 0, b.Length);
            strem.Close();
            client.Close();
 
             
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            Control.CheckForIllegalCrossThreadCalls = false;
        }
    }
}

  

posted @   尘梦  阅读(10323)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示
主题色彩