资源简介

这是一个用C#写的Socket服务器和客户端,当然Socket服务器和客户端不是主要的(有兴趣的朋友可以参考下),主要的内容是介绍如何通过这种Socket方式发送Object对象。 作者博客 http://luyugao.com

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

using User;
//object对象是通过user类派生的,这里引用的是User.dll 
//  作者博客: http://www.luyugao.com
namespace Client
{
    public partial class Client : Form
    {
        public Client()
        {
            InitializeComponent();
        }
        Socket mySocket;
        /// 
        /// 接收信息缓冲区大小
        /// 

        int packetSize = 1024 * 50;
        private void bt_lj_Click(object sender EventArgs e)
        {
            try
            {
                if (tb_ip.Text.Trim() != ““ && tb_port.Text.Trim() != ““)
                {
                    IPEndPoint iep;
                    try
                    {
                        iep = new IPEndPoint(
                            IPAddress.Parse(tb_ip.Text) Convert.ToInt32(tb_port.Text)
                            );
                    }
                    catch (Exception ex) { MessageBox.Show(ex.Message); return; }
                    mySocket = new Socket(AddressFamily.InterNetwork SocketType.Stream ProtocolType.Tcp);
                    mySocket.Connect(iep);

                    Thread t = new Thread(resMsg);
                    t.IsBackground = true;
                    t.Start();

                    bt_lj.Enabled = false;
                    bt_sendobject.Enabled = bt_sendStr.Enabled = true;
                }
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }

        }
        //object对象是通过user类派生的,这里引用的是User.dll 
        //  作者博客: http://www.luyugao.com
        /// 
        /// 循环接收信息
        /// 

        void resMsg()
        {
            while (true)
            {
                byte[] by = new byte[packetSize];
                int length = -1;
                try
                {
                    length = mySocket.Receive(by);
                }
                catch (SocketException) { break; }
                catch (Exception) { break; }
                if (length < 2)//消息异常
                    continue;
                string _cmd = getMsgHead(by);
                switch (_cmd)
                {
                    case “01“://文本消息
                        string Msg = Encoding.UTF8.GetString(by 2 length - 2);
                        showMsg(Msg);
                        break;
                    case“02“://对象消息
                         showMsg(“收到对象、信息如下:“);
                        byte[] by1=new byte[length-2];
                        Buffer.BlockCopy(by 2 by1 0 by1.Length);
                        object _obj = Deserialize(by1);
                        user _u = _obj as user;
                        string s = st

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

     文件      16384  2013-07-18 17:28  Sendobject\Client\bin\Debug\Client.exe

     文件      34304  2013-07-18 17:28  Sendobject\Client\bin\Debug\Client.pdb

     文件      11608  2013-07-18 17:30  Sendobject\Client\bin\Debug\Client.vshost.exe

     文件       4096  2013-07-18 17:00  Sendobject\Client\bin\Debug\User.dll

     文件       8252  2013-07-18 17:28  Sendobject\Client\Client.cs

     文件       3631  2013-07-18 17:01  Sendobject\Client\Client.csproj

     文件      14119  2013-07-18 17:22  Sendobject\Client\Client.Designer.cs

     文件       5817  2013-07-18 17:22  Sendobject\Client\Client.resx

     文件        180  2013-07-18 17:22  Sendobject\Client\obj\x86\Debug\Client.Client.resources

     文件        691  2013-07-18 17:30  Sendobject\Client\obj\x86\Debug\Client.csproj.FileListAbsolute.txt

     文件        976  2013-07-18 17:22  Sendobject\Client\obj\x86\Debug\Client.csproj.GenerateResource.Cache

     文件       4257  2013-07-18 17:01  Sendobject\Client\obj\x86\Debug\Client.csprojResolveAssemblyReference.cache

     文件      16384  2013-07-18 17:28  Sendobject\Client\obj\x86\Debug\Client.exe

     文件      34304  2013-07-18 17:28  Sendobject\Client\obj\x86\Debug\Client.pdb

     文件        180  2013-07-18 17:01  Sendobject\Client\obj\x86\Debug\Client.Properties.Resources.resources

     文件       5419  2013-07-18 17:29  Sendobject\Client\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件        468  2013-07-18 16:06  Sendobject\Client\Program.cs

     文件       1344  2013-07-18 16:06  Sendobject\Client\Properties\AssemblyInfo.cs

     文件       2864  2013-07-18 16:06  Sendobject\Client\Properties\Resources.Designer.cs

     文件       5612  2013-07-18 16:06  Sendobject\Client\Properties\Resources.resx

     文件       1093  2013-07-18 16:06  Sendobject\Client\Properties\Settings.Designer.cs

     文件        249  2013-07-18 16:06  Sendobject\Client\Properties\Settings.settings

     文件      14848  2013-07-18 17:21  Sendobject\Sendobject\bin\Debug\Sendobject.exe

     文件      34304  2013-07-18 17:21  Sendobject\Sendobject\bin\Debug\Sendobject.pdb

     文件      11608  2013-07-18 17:31  Sendobject\Sendobject\bin\Debug\Sendobject.vshost.exe

     文件       4096  2013-07-18 17:00  Sendobject\Sendobject\bin\Debug\User.dll

     文件      11718  2013-07-18 17:21  Sendobject\Sendobject\FServer.cs

     文件       5261  2013-07-18 17:18  Sendobject\Sendobject\FServer.Designer.cs

     文件       5817  2013-07-18 17:18  Sendobject\Sendobject\FServer.resx

     文件        789  2013-07-18 15:20  Sendobject\Sendobject\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache

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

评论

共有 条评论