• 大小: 100KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-06-04
  • 语言: C#
  • 标签: Socket通信  

资源简介

一个服务器端和多个客户端的Socket通信。启动服务端后,服务端通过持续监听客户端发来的请求,一旦监听到客户端传来的信息后,两端便可以互发信息了。服务器端需要绑定一个IP和端口号,用于客户端在网络中寻找并建立连接。信息发送原理:将手动输入字符串信息转换成机器可以识别的字节数组,然后调用套接字的Send()方法将字节数组发送出去。信息接收原理:调用套接字的Receive()方法,获取对端传来的字节数组,然后将其转换成人可以读懂的字符串信息。

资源截图

代码片段和文件信息

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;
using System.Net.Sockets;


namespace SocketClient
{
    public partial class FormClient : Form
    {
        Socket client;
        public FormClient()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender EventArgs e)
        {
            AsyncConnect();
        }

        /// 
        /// 连接到服务器
        /// 

        public void AsyncConnect()
        {
            try
            {
                //端口及IP
                IPEndPoint ipe = new IPEndPoint(IPAddress.Parse(textBox_ip.Text) int.Parse(textBox_port.Text));
                //创建套接字
                client = new Socket(AddressFamily.InterNetwork SocketType.Stream ProtocolType.Tcp);
                //开始连接到服务器
                client.BeginConnect(ipe asyncResult =>
                {
                    client.EndConnect(asyncResult);
                    //向服务器发送消息
                    AsyncSend(client “你好我是客户端“);
                    //接受消息
                    AsyncReceive(client);
                } null);
            }
            catch (Exception ex)
            {

            }


        }

        /// 
        /// 发送消息
        /// 

        /// 
        /// 
        public void AsyncSend(Socket socket string message)
        {
            if (socket == null || message == string.Empty) return;
            //编码
            byte[] data = Encoding.UTF8.GetBytes(message);
            try
            {

                socket.BeginSend(data 0 data.Length SocketFlags.None asyncResult =>
                {
                    //完成发送消息
                    int length = socket.EndSend(asyncResult);
                } null);
            }
            catch (Exception ex)
            {
            }
        }

        /// 
        /// 接收消息
        /// 

        /// 
        public void AsyncReceive(Socket socket)
        {
            byte[] data = new byte[1024];
            try
            {

                //开始接收数据
                socket.BeginReceive(data 0 data.Length SocketFlags.None
                asyncResult =>
                {
                    try
                    {
                        int length = socket.EndReceive(asyncResult);
                        setText(Encoding.UTF8.GetString(data));
                    }
                    catch (Exception)
                    {
                        AsyncReceive(socket);
                    }


                    AsyncReceive(socket);
                } null);
            }
            catch (Exception ex)
            {
   

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2017-10-31 16:53  SocketTest\
     目录           0  2017-10-17 09:07  SocketTest\SocketClient\
     目录           0  2017-10-12 17:24  SocketTest\SocketClient\bin\
     目录           0  2017-10-31 16:53  SocketTest\SocketClient\bin\Debug\
     文件       11776  2017-10-19 21:23  SocketTest\SocketClient\bin\Debug\SocketClient.exe
     文件       26112  2017-10-19 21:23  SocketTest\SocketClient\bin\Debug\SocketClient.pdb
     文件       11600  2017-10-16 14:36  SocketTest\SocketClient\bin\Debug\SocketClient.vshost.exe
     文件         490  2016-07-16 19:44  SocketTest\SocketClient\bin\Debug\SocketClient.vshost.exe.manifest
     目录           0  2017-10-12 17:24  SocketTest\SocketClient\bin\Release\
     文件        3596  2017-10-19 21:22  SocketTest\SocketClient\FormClient.cs
     文件        5892  2017-10-19 17:39  SocketTest\SocketClient\FormClient.Designer.cs
     文件        5817  2017-10-19 17:39  SocketTest\SocketClient\FormClient.resx
     目录           0  2017-10-12 17:24  SocketTest\SocketClient\obj\
     目录           0  2017-10-12 17:24  SocketTest\SocketClient\obj\x86\
     目录           0  2017-10-19 21:23  SocketTest\SocketClient\obj\x86\Debug\
     文件        5420  2017-10-17 08:19  SocketTest\SocketClient\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache
     文件        6341  2017-10-19 21:23  SocketTest\SocketClient\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache
     文件         656  2017-10-19 21:23  SocketTest\SocketClient\obj\x86\Debug\SocketClient.csproj.FileListAbsolute.txt
     文件         980  2017-10-19 21:23  SocketTest\SocketClient\obj\x86\Debug\SocketClient.csproj.GenerateResource.Cache
     文件       11776  2017-10-19 21:23  SocketTest\SocketClient\obj\x86\Debug\SocketClient.exe
     文件         180  2017-10-19 21:23  SocketTest\SocketClient\obj\x86\Debug\SocketClient.FormClient.resources
     文件       26112  2017-10-19 21:23  SocketTest\SocketClient\obj\x86\Debug\SocketClient.pdb
     文件         180  2017-10-19 21:23  SocketTest\SocketClient\obj\x86\Debug\SocketClient.Properties.Resources.resources
     目录           0  2017-10-12 17:24  SocketTest\SocketClient\obj\x86\Debug\TempPE\
     文件         498  2017-10-12 17:24  SocketTest\SocketClient\Program.cs
     目录           0  2017-10-12 17:24  SocketTest\SocketClient\Properties\
     文件        1356  2017-10-12 17:24  SocketTest\SocketClient\Properties\AssemblyInfo.cs
     文件        2876  2017-10-12 17:24  SocketTest\SocketClient\Properties\Resources.Designer.cs
     文件        5612  2017-10-12 17:24  SocketTest\SocketClient\Properties\Resources.resx
     文件        1099  2017-10-12 17:24  SocketTest\SocketClient\Properties\Settings.Designer.cs
     文件         249  2017-10-12 17:24  SocketTest\SocketClient\Properties\Settings.settings
............此处省略35个文件信息

评论

共有 条评论