基本信息
源码名称:C# 使用异步套接字实现一对多文字聊天程序
源码大小:0.13M
文件格式:.zip
开发语言:C#
更新时间:2018-12-27
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

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

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


使用异步套接字实现一对多文字聊天程序。




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

namespace 异步_client
{
    public partial class Form1 : Form
    {
        private Socket mySocket;
        public Form1()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        private void ConnectServer(IAsyncResult ar)//一般普通函数都是通过事件处理函数调用的
        {
            Socket ss = (Socket)ar.AsyncState;
            try
            {
                ss.EndConnect(ar);//结束挂起的异步连接请求
                StreamReader sr = new StreamReader(new NetworkStream(ss), System.Text.Encoding.Unicode);//接收信号并判断是否连接成功作用

                if (sr.ReadLine().Equals("0"))
                {
                    MessageBox.Show("对方拒绝通话!");
                    ss.Close();
                    this.btconnect.Enabled = true;
                    return;
                }
                this.State.Items.Add("与服务器"   this.mySocket.RemoteEndPoint.ToString()   "连接成功");
                this.Text = "client"   this.mySocket.LocalEndPoint.ToString();//窗口名称,窗口
                TransData td = new TransData();
                td.AReceiveVarData(this.mySocket, this.receive);
            }
            catch
            {
                MessageBox.Show("与服务器连接失败!");
                this.btconnect.Enabled = true;
            }
        }

        private void btstop_Click(object sender, EventArgs e)
        {
            try
            {
                mySocket.Close();
                this.State.Items.Add("与服务器断开连接!");
                this.btconnect.Enabled = true;
            }
            catch
            {
                MessageBox.Show("连接尚未开始,断开无效!");
            }
        }

        private void btconnect_Click_1(object sender, EventArgs e)
        {
            this.btconnect.Enabled = false;

            try
            {
                IPHostEntry remoteHost = Dns.GetHostEntry(this.IP.Text);
                IPAddress remoteIP = remoteHost.AddressList[0];

                remoteIP = IPAddress.Parse("127.0.0.1");
                IPEndPoint iep = new IPEndPoint(remoteIP, Int32.Parse(this.Port.Text));

                this.mySocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//核心建立连接
                this.mySocket.BeginConnect(iep, new AsyncCallback(ConnectServer), this.mySocket);
            }
            catch
            {
                MessageBox.Show("你输入的服务器或端口号格式不正确,请重新输入!");
                this.btconnect.Enabled = true;
                return;
            }
        }

        private void btsend_Click_1(object sender, EventArgs e)
        {
            try
            {
                TransData td = new TransData();
                td.ASendVarData(this.mySocket, this.send);
            }
            catch
            {
                MessageBox.Show("尚未与服务器建立连接,发送失败!");
                this.btconnect.Enabled = true;
            }
        }


    }
}