• 大小: 0.09M
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-09
  • 语言: 其他
  • 标签: 其他  

资源简介

Socket多线程实例.rar

资源截图

代码片段和文件信息

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;

namespace ChartClient
{
    public partial class ClientMain : Form
    {
        public ClientMain()
        {
            InitializeComponent();
        }
        //NetworkStream
        System.Net.Sockets.NetworkStream ns;
        //数据量
        System.IO.StreamReader sr;
        //客户端连接
        System.Net.Sockets.TcpClient clientsocket;
        //是否连接
        bool connected;
        //接收信息线程
        System.Threading.Thread receive;
        //服务器网络地址
        string serveraddress = “192.168.104.110“;
        //服务器端口号
        int serverport = 6666;
        //客户端连接名称
        string clientname;
        //操纵其他线程
        System.Threading.Thread t;
        //实现操纵其他线程委托
        public delegate void MyInvoke(string c);

        private void btnConnect_Click(object sender EventArgs e)
        {
            //连接服务器
            EstablishConnection();
            //注册
            RegisterWithServer();
            //连接成功
            if (connected)
            {
                //接受信息线程
                receive = new System.Threading.Thread(new System.Threading.ThreadStart(ReceiveChat));
                //启动线程
                receive.Start();
            }
        }

        private void btnDisconnect_Click(object sender EventArgs e)
        {
            //退出
            QuitChat();
        }

        private void btnSend_Click(object sender EventArgs e)
        {
            //连接成功
            if (connected)
            {
                try
                {
                    //创建信息
                    string command = “CHAT|“ + clientname + “:“ + ChatOut.Text + “\r\n“;
                    //转化为字节流
                    Byte[] outbytes = System.Text.Encoding.ASCII.GetBytes(command.ToCharArray());
                    //发送信息
                    ns.Write(outbytes 0 outbytes.Length);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(“发生错误:“ + ex.Message “错误“ MessageBoxButtons.OK MessageBoxIcon.Exclamation);
                }
            }
        }
        /// 
        /// 连接服务器
        /// 

        private void EstablishConnection()
        {
            //更改提示栏状态
            statusBar1.Text = “正在连接到服务器“;

            try
            {
                //根据网络地址和端口号获取服务器端连接
                clientsocket = new System.Net.Sockets.TcpClient(serveraddress serverport);
                //获取发送和接受数据的NetWorkStream
                ns = clientsocket.GetStream();
                //为NetWorkStream 初始化StreamReader实例
                sr = new System.IO.StreamReader(ns);
                //设置为已连接
                connected = true;
            }
            catch (Exception e)
            {
                //提示信息
    

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

     文件      14336  2011-01-07 09:28  ChartClient\bin\Debug\ChartClient.exe

     文件      40448  2011-01-07 09:28  ChartClient\bin\Debug\ChartClient.pdb

     文件      11608  2011-01-07 09:32  ChartClient\bin\Debug\ChartClient.vshost.exe

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

     文件       5446  2011-01-06 10:02  ChartClient\ChartClient.csproj

     文件        451  2011-01-06 10:02  ChartClient\ChartClient.csproj.user

     文件      13658  2011-01-07 09:28  ChartClient\ClientMain.cs

     文件       6458  2011-01-06 16:31  ChartClient\ClientMain.Designer.cs

     文件       5814  2011-01-06 16:31  ChartClient\ClientMain.resx

     文件        180  2011-01-06 16:31  ChartClient\obj\Debug\ChartClient.ClientMain.resources

     文件       2072  2011-01-07 09:32  ChartClient\obj\Debug\ChartClient.csproj.FileListAbsolute.txt

     文件        852  2011-01-04 11:35  ChartClient\obj\Debug\ChartClient.csproj.GenerateResource.Cache

     文件      14336  2011-01-07 09:28  ChartClient\obj\Debug\ChartClient.exe

     文件      40448  2011-01-07 09:28  ChartClient\obj\Debug\ChartClient.pdb

     文件        180  2011-01-06 10:03  ChartClient\obj\Debug\ChartClient.Properties.Resources.resources

     文件       6170  2011-01-07 09:28  ChartClient\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件        858  2011-01-06 16:31  ChartClient\obj\Debug\GenerateResource-ResGen.read.1.tlog

     文件        958  2011-01-06 16:31  ChartClient\obj\Debug\GenerateResource-ResGen.write.1.tlog

     文件       4608  2011-01-06 10:02  ChartClient\obj\Debug\TempPE\Properties.Resources.Designer.cs.dll

     文件        497  2011-01-04 10:08  ChartClient\Program.cs

     文件       1354  2011-01-04 09:56  ChartClient\Properties\AssemblyInfo.cs

     文件       2858  2011-01-06 10:02  ChartClient\Properties\Resources.Designer.cs

     文件       5612  2011-01-04 09:56  ChartClient\Properties\Resources.resx

     文件       1107  2011-01-06 10:02  ChartClient\Properties\Settings.Designer.cs

     文件        249  2011-01-04 09:56  ChartClient\Properties\Settings.settings

     文件      38400  2011-01-07 09:32  ChartServer\bin\Debug\ChartServer.pdb

     文件      11608  2011-01-06 14:28  ChartServer\bin\Debug\ChartServer.vshost.exe

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

     文件       5483  2011-01-06 10:02  ChartServer\ChartServer.csproj

     文件        451  2011-01-06 10:02  ChartServer\ChartServer.csproj.user

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

评论

共有 条评论