• 大小: 218KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-06-09
  • 语言: C#
  • 标签:

资源简介

1.加深对TCP/IP协议的理解; 2.掌握用Socket编程实现聊天工具。 3. visual studio2005提供了支持网络环境的类库system.net,可适应多种网络协议,包括TCP/IP、UDP等。使用system.net类库,可快速地开发各种网络应用程序,而Socket通信是基于TCP/IP协议。从而应用基于system.net开发一对一聊天程序。

资源截图

代码片段和文件信息

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

namespace client
{
    public partial class client : Form
    {
        private Thread ThreadClientReceive;//客户端接收聊天信息线程
        private Socket ClientSocket;//客户端网络连接
        public client()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;//关闭线程调用主线程控件错误报告,大型程序最好用委托方式。 
        }

        private void Form1_Load(object sender EventArgs e)
        {
            
            
        }

        private void button4_Click(object sender EventArgs e)
        {
            if (button4.Text == “连接“)
            {
                try
                {
                    ClientSocket = new Socket(AddressFamily.InterNetwork SocketType.Stream ProtocolType.Tcp);
                    IPEndPoint remoteEP = new IPEndPoint(IPAddress.Parse(textBox5.Text) int.Parse(textBox4.Text));//建立客户端网络端点
                    ClientSocket.Connect(remoteEP);
                    ThreadClientReceive = new Thread(new ThreadStart(ClientReceive));
                    ThreadClientReceive.Start();//客户端开始接收信息
                    button4.Text = “停止“;
                }
                catch (Exception Mye)
                {
                    MessageBox.Show(this Mye.Message “客户端“ MessageBoxButtons.OK MessageBoxIcon.Error);
                }
            }
            else
            {
                ClientSocket.Close();
                button4.Text = “连接“;
            } 
        }

        private void button3_Click(object sender EventArgs e)
        {
            try
            {
                byte[] Buff = Encoding.Default.GetBytes(textBox3.Text);
                ClientSocket.Send(Buff 0 Buff.Length SocketFlags.None);//客户端发送信息
                richTextBox2.AppendText(“客户端:“ + textBox3.Text + “\r\n“);
                textBox3.Text = ““;
            }
            catch 
            {
                MessageBox.Show(“还没有连接,发送无效!“);
            }
        }
        private void ClientReceive()//客户端开始接收信息
        {
            try
            {
                Byte[] Buff = new byte[1024 * 100];
                int Len = 0;
                while ((Len = ClientSocket.Receive(Buff 0 Buff.Length SocketFlags.None)) > 0)//接收到信息
                {
                    richTextBox2.AppendText(“服务器端:“ + Encoding.Default.GetString(Buff 0 Len) + “\r\n“);
                }
            }
            catch { }
        }
        private void client_FormClosing(object sender FormClosingEventArgs e)
        {
            //结束侦听,并结束接收信息
            Application.Exit();
        }
    }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     文件      154112  2010-06-27 21:09  07软件(2)班-崔永山-070806110077.doc
     目录           0  2010-06-24 19:47  source\
     目录           0  2010-06-24 19:47  source\client\
     目录           0  2010-06-27 22:15  source\client\client\
     文件         907  2010-06-24 19:47  source\client\client.sln
     文件       12288  2010-06-27 22:16  source\client\client.suo
     目录           0  2010-06-24 20:13  source\client\client\bin\
     目录           0  2010-06-27 22:14  source\client\client\bin\Debug\
     文件       32768  2010-06-27 22:15  source\client\client\bin\Debug\client.exe
     文件       26112  2010-06-27 22:15  source\client\client\bin\Debug\client.pdb
     文件        5632  2005-12-08 14:51  source\client\client\bin\Debug\client.vshost.exe
     文件        3122  2010-06-27 22:15  source\client\client\client.cs
     文件        3224  2010-06-27 12:11  source\client\client\client.csproj
     文件        8161  2010-06-27 22:15  source\client\client\client.Designer.cs
     文件       17351  2010-06-27 22:15  source\client\client\client.resx
     目录           0  2010-06-24 19:52  source\client\client\obj\
     文件        1212  2010-06-27 22:16  source\client\client\obj\client.csproj.FileListAbsolute.txt
     目录           0  2010-06-27 22:15  source\client\client\obj\Debug\
     文件        8254  2010-06-27 22:15  source\client\client\obj\Debug\client.client.resources
     文件         901  2010-06-27 22:15  source\client\client\obj\Debug\client.csproj.GenerateResource.Cache
     文件       32768  2010-06-27 22:15  source\client\client\obj\Debug\client.exe
     文件       26112  2010-06-27 22:15  source\client\client\obj\Debug\client.pdb
     文件         180  2010-06-24 19:52  source\client\client\obj\Debug\client.Properties.Resources.resources
     目录           0  2010-06-24 20:13  source\client\client\obj\Debug\Refactor\
     目录           0  2010-06-24 19:47  source\client\client\obj\Debug\TempPE\
     文件         466  2010-06-24 20:13  source\client\client\Program.cs
     目录           0  2010-06-24 19:47  source\client\client\Properties\
     文件        1184  2010-06-24 19:47  source\client\client\Properties\AssemblyInfo.cs
     文件        2868  2010-06-24 19:47  source\client\client\Properties\Resources.Designer.cs
     文件        5612  2010-06-24 19:47  source\client\client\Properties\Resources.resx
     文件        1091  2010-06-24 19:47  source\client\client\Properties\Settings.Designer.cs
............此处省略31个文件信息

评论

共有 条评论

相关资源