C# TCP服务器和客户端

1. 七层协议

  • 应用层
  • 表示层
  • 会话层
  • 传输层
  • 网络层
  • 数据链路层
  • 物理层
应用层
表示层
会话层
传输层
网络层
数据链路层
物理层

TCP/UDP属于传输层,IP属于网络层

2. TCP协议

建立有连接的,可靠性高的,无数据丢失的连接。

3. 使用C#实现TCP

3.1 服务器

  1. 初始化套接字
  2. 绑定IP、端口号
  3. 监听
  4. 接收客户请求
  5. 收发数据
  6. 关闭套接字
    在这里插入图片描述
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SocketTCPService
{

    public partial class ServerForm : Form
    {
        Socket server;//服务器套接字
        Socket client;//接受客户请求后的套接字
        byte[] buffer = new byte[1024 * 1024 * 6];//申请6Mb内存
        Thread th;//监听线程
        Thread receive;//接收数据线程
        Dictionary<string, Socket> dict = new Dictionary<string, Socket>();
        public ServerForm()
        {
            InitializeComponent();
        }

        #region 启动服务器
        bool isSer = false;
        private void buttonStartServer_Click(object sender, EventArgs e)
        {
            if(!isSer)
            {
                //1. 初始化套接字
                server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
                //2. 绑定套接字和端口号
                IPAddress ip = IPAddress.Parse(textBoxServerIP.Text);
                int port = Convert.ToInt32(textBoxServerPort.Text);
                server.Bind(new IPEndPoint(ip, port));
                //3. 监听
                server.Listen(10);
                //4. 开启监听线程
                th = new Thread(new ThreadStart(ListenC));
                th.IsBackground = true;//设置为后台线程,不需要用户主动关闭
                th.Start();
                isSer = true;
                buttonStartServer.Text = "关闭服务器";
                buttonStartServer.BackColor = Color.GreenYellow;
            }
            else
            {
                if(server != null)
                {
                    server.Close();
                    server = null;
                }
                if (client != null)
                {
                    client.Close();
                    client = null;
                }
                if(th != null)
                {
                    th.Abort();
                }
                if (receive != null)
                {
                    receive.Abort();
                }
                isSer = false;
                buttonStartServer.Text = "启动服务器";
                buttonStartServer.BackColor = Color.LightGray;
            }
        }
        #endregion

        #region 监听线程
        public void ListenC()
        {
            try
            {
                while (true)
                {
                    //等待客户的连接请求
                    client = server.Accept();//阻塞等待

                    dict.Add(client.RemoteEndPoint.ToString(), client);

                    //连接成功
                    Invoke(new MethodInvoker(delegate ()
                    {
                        listBoxReceive.Items.Add("连接成功");
                        comboBoxClient.Items.Add(client.RemoteEndPoint.ToString());
                    }));
                    
                    //开启线程
                    receive = new Thread(new ParameterizedThreadStart(RecData));
                    receive.IsBackground = true;
                    receive.Start(client);
                }
            }
            catch (Exception)
            {
                //throw;
            }
        }
        #endregion

        #region 接收数据线程
        public void RecData(object obj)
        {
            Socket Client = (Socket)obj;
            int counts = 0;
            while (true)
            {
                try
                {
                    //6. 接收数据
                    counts = Client.Receive(buffer);
                    if (counts > 0)
                    {
                        //在控件上显示接收到的数据(字符串)
                        Invoke(new MethodInvoker(delegate ()
                        {
                            listBoxReceive.Items.Add(string.Format("客户端{0}说:{1}",
                                Client.RemoteEndPoint.ToString(), Encoding.Default.GetString(buffer,0,counts)));
                        }));
                    }
                }
                catch(Exception)
                {
                    //throw;
                }
            }
        }
        #endregion

        #region 发送数据
        private void buttonSend_Click(object sender, EventArgs e)
        {
            if(!isSer)
            {
                MessageBox.Show("未启动服务器");
            }
            string ip = comboBoxClient.SelectedItem.ToString();            
            byte[] buffer = Encoding.Default.GetBytes(textBoxSend.Text);
            dict[ip].Send(buffer);
            //client.Send(buffer);
        }
        #endregion

        private void buttonClear_Click(object sender, EventArgs e)
        {
            listBoxReceive.Items.Clear();
        }
    }
}

3.2 客户端

  1. 初始化套接字
  2. 连接服务器
  3. 收发数据
  4. 关闭套接字

在这里插入图片描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SocketTCPClient
{
    public partial class FormClient : Form
    {
        #region 构造函数
        public FormClient()
        {
            InitializeComponent();
        }
        #endregion

        #region 变量
        Socket ClientSocket;//套接字
        byte[] buffer = new byte[1024 * 1024 * 6];
        bool isConnect = false;
        Thread th;
        #endregion

        #region 连接服务器 初始化套接字
        private void buttonConnect_Click(object sender, EventArgs e)
        {
            try
            {
                if(!isConnect)
                {
                    //1. 初始化套接字
                    ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    //2. 连接服务器
                    IPAddress ip = IPAddress.Parse(textBoxIP.Text);
                    int port = Convert.ToInt32(textBoxPort.Text);
                    ClientSocket.Connect(new IPEndPoint(ip, port));
                    isConnect = true;
                    buttonConnect.Text = "断开服务器";
                    buttonConnect.BackColor = Color.LawnGreen;

                    //3. 开启线程
                    th = new Thread(RecData);
                    th.IsBackground = true;
                    th.Start();
                }
                else
                {
                    if (ClientSocket != null)
                    {
                        ClientSocket.Close();
                        isConnect = false;
                        buttonConnect.Text = "连接服务器";
                        buttonConnect.BackColor = Color.LightGray;
                    }
                    if(th != null)
                    {
                        th.Abort();
                        th = null;
                    }                       
                }
            }
            catch (Exception)
            {
                MessageBox.Show("连接失败,请检查是否启动了服务器", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                //throw;
            }
        }
        #endregion

        #region 接收数据线程
        public void RecData()
        {            
            int counts = 0;
            while (true)
            {
                counts = ClientSocket.Receive(buffer);
                if (counts > 0)
                {
                    Invoke(new MethodInvoker(delegate ()
                    {
                        listBoxRec.Items.Add(string.Format("服务器说:{0}",Encoding.Default.GetString(buffer,0,counts)));
                    }));
                }
            }
        }
        #endregion

        #region 发送
        private void buttonSend_Click(object sender, EventArgs e)
        {
            if(isConnect)
            {
                byte[] buffer = Encoding.Default.GetBytes(textBoxSend.Text);
                ClientSocket.Send(buffer);
            }
            else
            {
                MessageBox.Show("客户端未连接到服务器", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        #endregion

        private void buttonClear_Click(object sender, EventArgs e)
        {
            listBoxRec.Items.Clear();
        }
    }
}

4. 资源下载地址

https://download.csdn.net/download/weixin_38566632/18594194

  • 12
    点赞
  • 71
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
### 回答1: c是计算机编程语言中的一种。它是一种高级编程语言,由丹尼斯·里奇在1972年开发出来。C语言的设计目标是提供高效、灵活和可移植的编程方式。C语言的特点包括: 1. 简洁而直观的语法:C语言的语法结构相对简单,容易理解和使用。它使用了大量的关键字和语法结构来实现各种功能。 2. 功能强大的标准库:C语言提供了丰富的标准库,包含了大量的函数和数据类型,可以方便地进行各种操作和处理。 3. 高效的执行速度:C语言编写的程序执行速度非常快,因为它是一种编译型语言,可以直接转换为机器代码执行。 4. 跨平台性:C语言的代码可以在不同的操作系统和平台上运行,只需稍作修改。这使得C语言成为编写移植性强的程序的理想选择。 5. 应用广泛:C语言广泛应用于系统级编程、嵌入式系统、游戏开发、网络编程等领域。许多操作系统和编译器都是用C语言编写的。 总而言之,C语言是一种强大而灵活的编程语言,具有高效、可移植和广泛应用等特点。它为程序员提供了丰富的功能和工具,使得开发复杂的软件变得更加简单和容易。无论是初学者还是经验丰富的开发者,学习和掌握C语言都是非常有价值的。 ### 回答2: c可以代表很多东西,如: 1. C语言:C语言是一种通用的高级程序设计语言,由贝尔实验室的Dennis M. Ritchie在20世纪70年代开发。它是一门强大且灵活的语言,广泛用于系统软件开发,嵌入式系统以及其他计算机应用程序的编写。 2. 久仰大名:C语言的名字很有名,因为它在计算机编程领域具有重要的地位,并且已成为许多计算机科学课程的基石。学习C语言可以帮助人们了解计算机的底层工作原理并提高编程能力。 3. 碳元素:C是化学元素周期表上的一个符号,代表碳元素。碳是地球上最常见的元素之一,它存在于各种有机化合物中,并且在生命的基础元素中起着重要作用。 4. 高音调:在西方音乐中,C是一个高音调的音符,它位于音阶的第一位。C音符也常用作调音参考,用来调整乐器的音高。 综上所述,C是一个多义词,可以指代C语言、久仰大名、碳元素以及高音调。具体取决于背景和语境。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MechMaster

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值