基本信息
源码名称:Socket内网实现通信(局域网聊天)
源码大小:0.06M
文件格式:.zip
开发语言:C#
更新时间:2019-01-14
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

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

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

【调试步骤】

打开两个Chatting.exe客户端,两个都输入本地的ip地址,第一个选择等待对方链接,第二个选择主动链接,就可以通信了




using System;
using System.Drawing;
using System.Windows.Forms;
using System.Net.Sockets;

namespace Chatting
{
    public partial class MainForm : Form
    {

        public MainForm()
        {
            InitializeComponent();
        }

        SocketFunc socket;
        System.Action<string> ReceiveAction;
        System.Action AccessAction;

        private void MainForm_Load(object sender, EventArgs e)
        {
            //异步建立连接回调
            AccessAction = () =>
            {
                this.Invoke((MethodInvoker)delegate()
                {
                    lblFriendIP.Visible = false;
                    txtIP.Visible = false;
                    btnConnect.Visible = false;
                    btnWaitAccess.Visible = false;

                    String friendIP = socket.communicateSocket.RemoteEndPoint.ToString();
                    lblState.Text = String.Format("连接成功. 对方IP:{0}", friendIP);

                    try
                    {
                        socket.Receive(ReceiveAction);
                    }
                    catch (Exception exp)
                    {
                        MessageBox.Show(exp.Message, "错误");
                    }
                });

            };
            //异步接收消息回调
            ReceiveAction = msg =>
            {
                txtGetMsg.Invoke((MethodInvoker)delegate()
                {
                    UpdateGetMsgTextBox(false, msg, Color.Red);
                });
            };
        }

        private void btnWaitAccess_Click(object sender, EventArgs e)
        {
            this.socket = new ServerSocket();
            try
            {
                this.socket.Access("", this.AccessAction);
            }
            catch (Exception ecp)
            {
                MessageBox.Show(ecp.Message, "错误");
            }

            lblState.Text = "等待对方连接...";
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            this.socket = new ClientSocket();
            try
            {
                this.socket.Access(txtIP.Text, this.AccessAction);
            }
            catch (Exception ecp)
            {
                MessageBox.Show(ecp.Message, "错误");
            }
            lblState.Text = "正在连接对方...";
        }

        private void btnSendMsg_Click(object sender, EventArgs e)
        {
            string message = txtSendMsg.Text.Trim();
            if (string.IsNullOrEmpty(message))
            {
                MessageBox.Show("消息内容不能为空!", "错误");
                txtSendMsg.Focus();
                return;
            }

            try
            {
                socket.Send(message);
            }
            catch(Exception ecp)
            {
                MessageBox.Show(ecp.Message, "错误");
                return;
            }

            UpdateGetMsgTextBox(true, message, Color.Blue);
            txtSendMsg.Text = "";
        }

        private void UpdateGetMsgTextBox(bool sendMsg, string message, Color color)
        {
            string appendText;
            if (sendMsg == true)
            {
                appendText = "Me:           "   System.DateTime.Now.ToString()
                      Environment.NewLine
                      message   Environment.NewLine;
            }
            else
            {
                appendText = "Friend:           "   System.DateTime.Now.ToString()
                      Environment.NewLine
                      message   Environment.NewLine;
            }
            int txtGetMsgLength = txtGetMsg.Text.Length;
            txtGetMsg.AppendText(appendText);
            txtGetMsg.Select(txtGetMsgLength, appendText.Length - Environment.NewLine.Length*2 -message.Length);
            txtGetMsg.SelectionColor = color;

            txtGetMsg.ScrollToCaret();
        }
    }
}