• 大小: 117KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-05-31
  • 语言: C#
  • 标签: WinForm  C#完整  

资源简介

史上最完整的的基于Winform下的Socket异步通信实例源代码,绝对物超所值,你值得拥有!

资源截图

代码片段和文件信息

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

namespace Socket_Client_asynchronous
{
    public partial class Form1 : Form
    {
        public TcpClient tcpc; //对服务器端建立TCP连接 
        public Socket tcpsend; //发送创建套接字 
        public bool connect_flag = false; 
        public byte[] receive_buff = new byte[4096]; 
        public ManualResetEvent connectDone = new ManualResetEvent(false); //连接的信号 
        public ManualResetEvent readDone = new ManualResetEvent(false); //读信号 
        public ManualResetEvent sendDone = new ManualResetEvent(false); //发送结束
        public bool connect(string address)
        { 
            try 
            {
                tcpsend = new Socket(AddressFamily.InterNetwork SocketType.Stream ProtocolType.Tcp);//初始化套接字
                IPEndPoint remotepoint = new IPEndPoint(IPAddress.Parse(address) Convert.ToInt32(textBox3.Text));//根据ip地址和端口号创建远程终结点
                EndPoint end = (EndPoint)remotepoint; 
                tcpsend.BeginConnect(end new AsyncCallback(ConnectedCallback) tcpsend); //调用回调函数
                connectDone.WaitOne(); 
                return true;
            } 
            catch 
            { 
                return false;
            }
        }
        private void ConnectedCallback(IAsyncResult ar)
        {
            Socket client = (Socket)ar.AsyncState;
            client.EndConnect(ar);
            connect_flag = true;
            connectDone.Set();
        }
        public void send(string data)
        {
            int length = data.Length;
            Byte[] Bysend = new byte[length];
            Bysend = System.Text.Encoding.GetEncoding(“GB2312“).GetBytes(data); //将字符串指定到指定Byte数组
            tcpsend.BeginSend(Bysend 0 Bysend.Length 0 new AsyncCallback(SendCallback) tcpsend); //异步发送数据
            sendDone.WaitOne();
        }
        private void SendCallback(IAsyncResult ar) //发送的回调函数
        {
            Socket client = (Socket)ar.AsyncState;
            int bytesSend = client.EndSend(ar); //完成发送
            sendDone.Set();
        }
        public void receive() //接收数据
        {
            //byte[] receive=new byte[1024];
            tcpsend.BeginReceive(receive_buff 0 receive_buff.Length 0 new AsyncCallback(ReceiveCallback) tcpsend);
            //sendDone.WaitOne();
        }
        
        private void ReceiveCallback(IAsyncResult ar)
        {
            Socket client = (Socket)ar.AsyncState; //获取句柄
            int bytesread = client.EndReceive(ar);
            if (bytesread > 0)
            {
                tcpsend.BeginReceive(receive_buff 0 receive_buff.Length 0 new AsyncCallback(ReceiveCallback) client);
            }
            else
            {
                readDone.Se

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2013-03-01 23:57  Socket_asynchronous\
     目录           0  2013-03-01 23:57  Socket_asynchronous\Socket_Client_asynchronous\
     目录           0  2013-03-01 23:57  Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\
     文件        6411  2013-03-22 21:49  Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\Form1.Designer.cs
     文件        5302  2013-03-23 19:21  Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\Form1.cs
     文件        5817  2013-03-22 21:49  Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\Form1.resx
     文件         507  2013-03-01 16:15  Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\Program.cs
     目录           0  2013-03-01 23:57  Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\Properties\
     文件        1384  2013-03-01 16:15  Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\Properties\AssemblyInfo.cs
     文件        2902  2013-03-01 16:15  Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\Properties\Resources.Designer.cs
     文件        5612  2013-03-01 16:15  Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\Properties\Resources.resx
     文件        1111  2013-03-01 16:15  Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\Properties\Settings.Designer.cs
     文件         249  2013-03-01 16:15  Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\Properties\Settings.settings
     文件        3711  2013-03-01 16:43  Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous.csproj
     目录           0  2013-03-01 23:57  Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\bin\
     目录           0  2013-03-01 23:57  Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\bin\Debug\
     文件       11776  2013-03-23 19:21  Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\bin\Debug\Socket_Client_asynchronous.exe
     文件       38400  2013-03-23 19:21  Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\bin\Debug\Socket_Client_asynchronous.pdb
     文件       11600  2013-03-23 19:28  Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\bin\Debug\Socket_Client_asynchronous.vshost.exe
     文件         490  2009-08-31 00:40  Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\bin\Debug\Socket_Client_asynchronous.vshost.exe.manifest
     目录           0  2013-03-01 23:57  Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\obj\
     目录           0  2013-03-01 23:57  Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\obj\x86\
     目录           0  2013-03-23 19:21  Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\obj\x86\Debug\
     文件        4440  2013-03-01 19:46  Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache
     文件        6356  2013-03-23 19:21  Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache
     文件        1296  2013-03-22 21:49  Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\obj\x86\Debug\GenerateResource.read.1.tlog
     文件        3118  2013-03-22 21:49  Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\obj\x86\Debug\GenerateResource.write.1.tlog
     文件        7897  2013-03-01 19:56  Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\obj\x86\Debug\ResolveAssemblyReference.cache
     文件         180  2013-03-22 21:49  Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\obj\x86\Debug\Socket_Client_asynchronous.Form1.resources
     文件         180  2013-03-12 17:24  Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\obj\x86\Debug\Socket_Client_asynchronous.Properties.Resources.resources
     文件        3720  2013-03-23 19:28  Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\obj\x86\Debug\Socket_Client_asynchronous.csproj.FileListAbsolute.txt
............此处省略40个文件信息

评论

共有 条评论