资源简介

包括服务器端和客户端源码,采用C#实现服务器与客户端通过TCP协议进行数据通信,对于多个连接的客户端,服务器端采用多线程处理客户端发送的数据。

资源截图

代码片段和文件信息

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
    {
        private IPAddress serverIP = IPAddress.Parse(“127.0.0.1“);//以本机作测试
        public Socket sock;
        private IPEndPoint serverFullAddr;//完整终端地址
        public Form1()
        {
            InitializeComponent();
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false; 
        }

        

        private void Form1_Load(object sender EventArgs e)
        {

        }

        public void ReceiveData()
        {

            while (true)
            {
                string mess = ““;
                try
                {

                    byte[] message = new byte[1024];
                    int bytes = 0;
                    try
                    {
                        bytes = sock.Receive(message);//接收数据
                        mess = mess + Encoding.Default.GetString(message 0 bytes);
                        if (message.Length > 1024)
                        {
                            do
                            {
                                bytes = sock.Receive(message message.Length 0);
                                mess = mess + Encoding.ASCII.GetString(message 0 bytes);
                            }
                            while (bytes > 0);
                        }
                        MessageBox.Show(mess);
                    }
                    catch
                    {
                        // MessageBox.Show(“空数据!“);
                    }
                    

                }
                catch (Exception ex)
                {
                    
                }
            }


        }//调用该函数将socket接收的字符使用RSA私钥解密后以string形式赋值给全局变量dedata,注意多线程调用


        private void button1_Click(object sender EventArgs e)
        {
            try
            {
                serverIP = IPAddress.Parse(textBox1.Text.ToString());
                serverFullAddr = new IPEndPoint(serverIP int.Parse(textBox2.Text.ToString()));//设置IP,端口
                sock = new Socket(AddressFamily.InterNetwork SocketType.Stream ProtocolType.Tcp);
                //指定本地主机地址和端口号
                sock.Connect(serverFullAddr);
                StatuLabel.Text = “连接服务器成功!“;
                Thread thr = new Thread(ReceiveData);
                thr.IsBackground = true;
                thr.Start();
            }
            catch (Exception)
            {
                StatuLabel.Text = “连接服务器失败!“;
            }
        }
        private void button2_Click(object sender EventArgs e)
        {
            sock.Send(Encoding.Default.GetBytes(textBox3.Text.ToString()));
        }

 

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2018-05-12 12:43  coresource\
     目录           0  2018-05-12 12:44  coresource\Client\
     目录           0  2018-05-12 12:44  coresource\Client\Client\
     文件       11776  2018-05-12 12:43  coresource\Client\Client.exe
     文件         987  2018-05-11 14:36  coresource\Client\Client.sln
     文件       45568  2018-05-12 12:43  coresource\Client\Client.v12.suo
     文件         189  2018-05-11 14:36  coresource\Client\Client\App.config
     文件        3865  2018-05-11 14:52  coresource\Client\Client\Client.csproj
     文件        3462  2018-05-12 12:43  coresource\Client\Client\Form1.cs
     文件        7332  2018-05-12 12:43  coresource\Client\Client\Form1.Designer.cs
     文件        5817  2018-05-12 12:43  coresource\Client\Client\Form1.resx
     文件         518  2018-05-11 14:36  coresource\Client\Client\Program.cs
     目录           0  2018-05-12 12:43  coresource\Client\Client\Properties\
     文件        1336  2018-05-11 14:36  coresource\Client\Client\Properties\AssemblyInfo.cs
     文件        2864  2018-05-11 14:36  coresource\Client\Client\Properties\Resources.Designer.cs
     文件        5612  2018-05-11 14:36  coresource\Client\Client\Properties\Resources.resx
     文件        1093  2018-05-11 14:36  coresource\Client\Client\Properties\Settings.Designer.cs
     文件         249  2018-05-11 14:36  coresource\Client\Client\Properties\Settings.settings
     目录           0  2018-05-12 12:43  coresource\Server\
     目录           0  2018-05-12 12:44  coresource\Server\Server\
     文件       14336  2018-05-12 12:43  coresource\Server\Server.exe
     文件         987  2018-05-11 13:38  coresource\Server\Server.sln
     文件       38400  2018-05-12 12:43  coresource\Server\Server.v12.suo
     文件         189  2018-05-11 13:38  coresource\Server\Server\App.config
     文件        9853  2018-05-12 12:43  coresource\Server\Server\Form1.cs
     文件        6528  2018-05-12 12:36  coresource\Server\Server\Form1.Designer.cs
     文件        6011  2018-05-12 12:36  coresource\Server\Server\Form1.resx
     文件         518  2018-05-11 13:38  coresource\Server\Server\Program.cs
     目录           0  2018-05-12 12:43  coresource\Server\Server\Properties\
     文件        1336  2018-05-11 13:38  coresource\Server\Server\Properties\AssemblyInfo.cs
     文件        2864  2018-05-11 13:38  coresource\Server\Server\Properties\Resources.Designer.cs
............此处省略4个文件信息

评论

共有 条评论