资源简介

1、SocketSever服务器端代码

2、SocketClient客户端代码

3、VS2019上测试通过,修改IP地址和端口号就可以直接编译使用

资源截图

代码片段和文件信息

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

namespace SocketDemo.Client
{
    public partial class FrmSocketClientMain : Form
    {
        public FrmSocketClientMain()
        {
            InitializeComponent();
        }
        //创建1个客户端套接字和1个负责监听服务端请求的线程 
        static Thread ThreadClient = null;
        static Socket SocketClient = null;

        private void btnConnect_Click(object sender EventArgs e)
        {
            IPAddress ip = IPAddress.Parse(this.txtServerIP.Text);
            IPEndPoint ipe = new IPEndPoint(ip Convert.ToInt32( this.txtServerPort.Text));
            //定义一个套接字监听 
            SocketClient = new Socket(AddressFamily.InterNetwork SocketType.Stream ProtocolType.Tcp);
            try
            {
                //客户端套接字连接到网络节点上,用的是Connect 
                SocketClient.Connect(ipe);
            }
            catch (Exception)
            {
                listBox1.Items.Add(DateTime.Now.ToString(“yyyy-MM-dd HH:mm:ss:ffff“) + “  “ + “连接失败“);
                return;
            }
            ThreadClient = new Thread(Recv);
            ThreadClient.IsBackground = true;
            ThreadClient.Start();
        }

        //发送字符信息到服务端的方法 
        public static void ClientSendMsg(string sendMsg)
        {
            //将输入的内容字符串转换为机器可以识别的字节数组   
            byte[] arrClientSendMsg = Encoding.UTF8.GetBytes(sendMsg);
            //调用客户端套接字发送字节数组   
            SocketClient.Send(arrClientSendMsg);
        }
        //接收服务端发来信息的方法  
        public  void Recv()
        {
            //持续监听服务端发来的消息 
            while (true)
            {
                try
                {
                    //定义一个1M的内存缓冲区,用于临时性存储接收到的消息 
                    byte[] arrRecvmsg = new byte[1024 * 1024];
                    //将客户端套接字接收到的数据存入内存缓冲区,并获取长度 
                    int length = SocketClient.Receive(arrRecvmsg);
                    //将套接字获取到的字符数组转换为人可以看懂的字符串 
                    string strRevMsg = Encoding.UTF8.GetString(arrRecvmsg 0 length);
                    listBox1.Items.Add(DateTime.Now.ToString(“yyyy-MM-dd HH:mm:ss:ffff“) + “  “ +“服务器:“ + strRevMsg );
                }
                catch (Exception ex)
                {
                    listBox1.Items.Add(DateTime.Now.ToString(“yyyy-MM-dd HH:mm:ss:ffff“) + “  “ + “远程服务器已经中断连接!“ + ex.Message );
                    break;
                }
            }
        }

        private void btnDisConnect_Click(object sender EventArgs e)
        {
            if (SocketClient != null)
                SocketClient.Dispose();
        }

        private void btnSend_Click(object sender EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txtMsg.

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       1632  2021-05-13 14:45  SocketDemo.sln

     文件        187  2021-05-13 11:03  SocketClient\App.config

     文件     730112  2021-05-13 14:59  SocketClient\bin\Debug\SocketClient.exe

     文件        187  2021-05-13 11:03  SocketClient\bin\Debug\SocketClient.exe.config

     文件      34304  2021-05-13 14:59  SocketClient\bin\Debug\SocketClient.pdb

     文件       3518  2021-05-13 14:56  SocketClient\FrmSocketClientMain.cs

     文件      10159  2021-05-13 14:57  SocketClient\FrmSocketClientMain.Designer.cs

     文件     543623  2021-05-13 14:44  SocketClient\FrmSocketClientMain.resx

     文件     358334  2021-05-13 14:44  SocketClient\Network.ico

     文件        210  2021-05-13 11:03  SocketClient\obj\Debug\.NETframeworkVersion=v4.5.AssemblyAttributes.cs

     文件       7248  2021-05-13 14:56  SocketClient\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件         42  2021-05-13 14:59  SocketClient\obj\Debug\SocketClient.csproj.CoreCompileInputs.cache

     文件        870  2021-05-13 14:59  SocketClient\obj\Debug\SocketClient.csproj.FileListAbsolute.txt

     文件       1026  2021-05-13 14:59  SocketClient\obj\Debug\SocketClient.csproj.GenerateResource.cache

     文件     730112  2021-05-13 14:59  SocketClient\obj\Debug\SocketClient.exe

     文件      34304  2021-05-13 14:59  SocketClient\obj\Debug\SocketClient.pdb

     文件     358900  2021-05-13 14:59  SocketClient\obj\Debug\SocketDemo.Client.FrmSocketClientMain.resources

     文件        180  2021-05-13 14:59  SocketClient\obj\Debug\SocketDemo.Client.Properties.Resources.resources

     文件       3584  2021-05-13 14:56  SocketClient\obj\Debug\TempPE\Properties.Resources.Designer.cs.dll

     文件        543  2021-05-13 14:58  SocketClient\Program.cs

     文件       1316  2021-05-13 11:03  SocketClient\Properties\AssemblyInfo.cs

     文件       2890  2021-05-13 14:56  SocketClient\Properties\Resources.Designer.cs

     文件       5612  2021-05-13 11:03  SocketClient\Properties\Resources.resx

     文件       1118  2021-05-13 14:56  SocketClient\Properties\Settings.Designer.cs

     文件        249  2021-05-13 11:03  SocketClient\Properties\Settings.settings

     文件       3812  2021-05-13 14:56  SocketClient\SocketClient.csproj

     文件        187  2021-05-13 11:01  SocketServer\App.config

     文件    1091072  2021-05-14 16:18  SocketServer\bin\Debug\SocketServer.exe

     文件        187  2021-05-13 11:01  SocketServer\bin\Debug\SocketServer.exe.config

     文件      38400  2021-05-14 16:18  SocketServer\bin\Debug\SocketServer.pdb

............此处省略43个文件信息

评论

共有 条评论