• 大小: 133KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-06-07
  • 语言: C#
  • 标签: C#  socket聊天  

资源简介

C# socket聊天 服务器转发 由client 和server client直接能相互发送消息由服务器转发

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;

namespace AsyncTcpClient
{
    public partial class FormClient : Form
    {
        //声明公共变量
        private bool isExit = false;
        private delegate void SetListBoxCallback(string str);
        private SetListBoxCallback setListBoxCallback;
        private delegate void SetRichTextBoxReceiveCallback(string str);
        private SetRichTextBoxReceiveCallback setRichTextBoxReceiveCallback;

        private delegate void SetcomboBoxCallback(string str);
        private SetcomboBoxCallback setComboBoxCallback;

        private TcpClient client;
        private NetworkStream networkStream;
        private EventWaitHandle allDone = new EventWaitHandle(false EventResetMode.ManualReset);

        public FormClient()
        {
            InitializeComponent();
        }
        //窗体加载
        private void FormClient_Load(object sender EventArgs e)
        {
            listBoxStatus.HorizontalScrollbar = true;
            setListBoxCallback = new SetListBoxCallback(SetListBox);
            setRichTextBoxReceiveCallback = new SetRichTextBoxReceiveCallback(SetRichTextBoxReceive);
            setComboBoxCallback = new SetcomboBoxCallback(SetComboBox);
        }
        //连接事件
        private void buttonConnect_Click(object sender EventArgs e)
        {
            //使用IPv4,实现应用时要将Dns.GetHostName()改为服务器名
            client = new TcpClient(AddressFamily.InterNetwork);
            IPAddress serverIP = IPAddress.Parse(tbxIP.Text);
            AsyncCallback requestCallback = new AsyncCallback(RequestCallback);
            allDone.Reset();
            //开始对远程主机的异步请求
            client.BeginConnect(serverIP 51888 requestCallback client);
            listBoxStatus.Invoke(setListBoxCallback string.Format(“本机EndPoint:{0}“ client.Client.LocalEndPoint));
            listBoxStatus.Invoke(setListBoxCallback “开始与服务器建立连接“);
            //阻塞线程直到BeginConnect完成,连接完成自动调用RequestCallback中的Set方法解除阻塞
            allDone.WaitOne();
        }
        //发送事件
        private void buttonSend_Click(object sender EventArgs e)
        {
            if (comboBox1.SelectedItem != null)
            {
                SendString(richTextBoxSend.Text + “;“ + comboBox1.SelectedItem.ToString() + “;“);
                richTextBoxSend.Clear();
            }
            else
            {
                MessageBox.Show(“请选择一个接收者“);
            }
        }
        private void RequestCallback(IAsyncResult ar)
        {
            //异步执行到此,说明BeginConnect已完成,但未结束,需要解除阻塞,并调用EndConnect
            allDone.Set();
            try
            {
                client = (TcpClient)ar.AsyncState;
                client.EndConnect(ar);
                listBoxStatus.Invo

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       3608  2012-11-21 14:07  client\AsyncTcpClient\AsyncTcpClient.csproj

     文件        452  2012-11-21 14:07  client\AsyncTcpClient\AsyncTcpClient.csproj.user

     文件      15360  2012-11-21 16:44  client\AsyncTcpClient\bin\Debug\AsyncTcpClient.exe

     文件      40448  2012-11-21 16:44  client\AsyncTcpClient\bin\Debug\AsyncTcpClient.pdb

     文件      21464  2012-11-21 16:46  client\AsyncTcpClient\bin\Debug\AsyncTcpClient.vshost.exe

     文件        490  2010-03-17 22:39  client\AsyncTcpClient\bin\Debug\AsyncTcpClient.vshost.exe.manifest

     文件       7329  2012-11-21 16:43  client\AsyncTcpClient\FormClient.cs

     文件       9500  2012-11-21 15:44  client\AsyncTcpClient\FormClient.Designer.cs

     文件       5814  2012-11-21 15:44  client\AsyncTcpClient\FormClient.resx

     文件        874  2011-02-15 10:43  client\AsyncTcpClient\obj\AsyncTcpClient.csproj.FileListAbsolute.txt

     文件       1137  2012-11-21 16:46  client\AsyncTcpClient\obj\Debug\AsyncTcpClient.csproj.FileListAbsolute.txt

     文件        852  2012-11-21 15:45  client\AsyncTcpClient\obj\Debug\AsyncTcpClient.csproj.GenerateResource.Cache

     文件       1339  2012-11-21 14:37  client\AsyncTcpClient\obj\Debug\AsyncTcpClient.csprojResolveAssemblyReference.cache

     文件      15360  2012-11-21 16:44  client\AsyncTcpClient\obj\Debug\AsyncTcpClient.exe

     文件        180  2012-11-21 15:45  client\AsyncTcpClient\obj\Debug\AsyncTcpClient.FormClient.resources

     文件      40448  2012-11-21 16:44  client\AsyncTcpClient\obj\Debug\AsyncTcpClient.pdb

     文件        180  2012-11-21 14:07  client\AsyncTcpClient\obj\Debug\AsyncTcpClient.Properties.Resources.resources

     文件        789  2012-11-21 14:15  client\AsyncTcpClient\obj\Debug\DesignTimeResolveAssemblyReferences.cache

     文件       6091  2012-11-21 16:45  client\AsyncTcpClient\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件       4608  2012-11-21 14:15  client\AsyncTcpClient\obj\Debug\TempPE\Properties.Resources.Designer.cs.dll

     文件        478  2011-02-10 15:55  client\AsyncTcpClient\Program.cs

     文件       1200  2011-02-10 15:55  client\AsyncTcpClient\Properties\AssemblyInfo.cs

     文件       2856  2012-11-21 14:07  client\AsyncTcpClient\Properties\Resources.Designer.cs

     文件       5612  2011-02-10 15:55  client\AsyncTcpClient\Properties\Resources.resx

     文件       1097  2012-11-21 14:07  client\AsyncTcpClient\Properties\Settings.Designer.cs

     文件        249  2011-02-10 15:55  client\AsyncTcpClient\Properties\Settings.settings

     文件        512  2011-02-10 16:00  client\AsyncTcpClient\Readobject.cs

     文件        932  2012-11-21 14:07  client\AsyncTcpClient.sln

    ..A..H.     14336  2011-02-15 10:43  client\AsyncTcpClient.suo

    ..A..H.     29184  2012-11-21 16:48  client\AsyncTcpClient.v11.suo

............此处省略53个文件信息

评论

共有 条评论