基本信息
源码名称:C# socket实例源码下载(客户端给服务端发消息)
源码大小:0.10M
文件格式:.zip
开发语言:C#
更新时间:2016-04-10
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍

服务端源码:

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;

namespace SocketServer
{
    public partial class SocketServerForm : Form
    {
        public AsySockets asySockets = null;

        private Dictionary<string, AsySockets> dictory = new Dictionary<string, AsySockets>();
        public SocketServerForm()
        {
            InitializeComponent();
        }

        private void SocketServerForm_Load(object sender, EventArgs e)
        {
            
        }

        void asySockets_ReceiveDataCompleteEvent(string mid,string receiveData)
        {
            value = receiveData;
            MethodInvoker mi = new MethodInvoker(SetValue);
            this.Invoke(mi);
            AsySockets ass = dictory[mid];
            if (null != ass)
            {
              ass.SendData("接收成功");
            }
           // asySockets.SendData("接收成功");
        }

        string value = string.Empty;

        private void SetValue()
        {
            //this.txtReceiveData.Text  = value "\r\n";
            this.txtReceiveData.AppendText(value   "\r\n");
        }
        private void ListenEvent_Method(AsySockets asySocket)
        {
            asySocket.ReceiveDataCompleteEvent  = new AsySockets.ReceiveDataCompleteHandler(asySockets_ReceiveDataCompleteEvent);
            asySocket.ReceiveData();
            dictory.Add(asySocket.mid, asySocket);
        }
        //发送数据
        private void btnSendData_Click(object sender, EventArgs e)
        {
            try
            {
               // dictory[mid].SendData("接收成功");
                
               //asySockets.SendData("接收成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void button_listen_Click(object sender, EventArgs e)
        {
            try
            {
                asySockets = new AsySockets(this.txtLocalIP.Text.Trim(), int.Parse(this.txtLocalPort.Text.Trim()));
                asySockets.ListenEvent  = new AsySockets.ListenHandler(ListenEvent_Method);
                asySockets.Listen();
                MessageBox.Show("侦听成功!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }



    }
}

客户端源码:

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;

namespace SocketClient
{
    public partial class SocketClientForm : Form
    {

        public AsySockets asyScokets = null;
        public SocketClientForm()
        {
            InitializeComponent();
        }

        private void btnLinkServer_Click(object sender, EventArgs e)
        {
            try
            {
                asyScokets = new AsySockets();
                asyScokets.ReceiveDataCompleteEvent  = new AsySockets.ReceiveDataCompleteHandler(asyScokets_ReceiveDataCompleteEvent);
                asyScokets.ConnectServer(this.txtServerIP.Text.Trim(), int.Parse(this.txtServerPort.Text.Trim()));
                MessageBox.Show("连接成功");
                this.btnSendData.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        void asyScokets_ReceiveDataCompleteEvent(string strdata)
        {
            value = strdata;
            MethodInvoker mi = new MethodInvoker(SetValue);
            this.Invoke(mi);
        }
        string value = string.Empty;
        private void SetValue()
        {
            this.txtReceiveData.AppendText(value "\r\n");
        }
        private void btnSendData_Click(object sender, EventArgs e)
        {
            asyScokets.SendData(this.txtSendData.Text);
        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }

        private void SocketClientForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            asyScokets.Close();
        }



    }
}