资源简介

这个socket通信demo 用vs2010,框架4写的,主要用tcpClient.Connect进行连接、监听断用SocketAsyncEventArgs 异步来接收socket。主要内容是发送端每隔一定时间发送自动生成的图片给接收端,接收端通过协议"begin"+"图片长度"+"图片"的方式进行拆包,最后保存图片。 里面写有日志。 代码里面有详细说明,供大家交流.

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net.Sockets;
using System.Net;

namespace Client_Receive
{
    class Listener
    {
        //开个线程用来监听socket
        static Thread trMain;
        static Socket objServer;
        public static void Start()
        {
            trMain = new Thread(ThreadMains);
            trMain.Start();
        }

        static void ThreadMains()
        {
            objServer = new Socket(AddressFamily.InterNetwork SocketType.Stream ProtocolType.Tcp);
            objServer.Bind(new System.Net.IPEndPoint(IPAddress.Any 12345));
            objServer.Listen(1000);
            while (true)
            {
                Socket sock = objServer.Accept();
                ProcessEvent(sock);
            }
        }
        static void ProcessEvent(Socket sock)
        {
            //采用异步接受socket
            SocketAsyncEventArgs e = new SocketAsyncEventArgs();
            e.AcceptSocket = sock;
            SocketEvent client = new SocketEvent();
            //绑定事件,接受完一个包,进行判断
            e.Completed += client.IOEventArgs_Completed;
            client.Start(e);
            //Console.WriteLine(“客户端请求成功-“ + sock.RemoteEndPoint.ToString());
        }

    }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2012-05-31 17:58  Client_Receive\
     文件         144  2012-05-31 17:42  Client_Receive\app.config
     目录           0  2012-05-31 17:58  Client_Receive\bin\
     目录           0  2012-05-31 18:52  Client_Receive\bin\Debug\
     目录           0  2012-05-31 18:50  Client_Receive\bin\Debug\20120531\
     文件       10747  2012-05-31 18:05  Client_Receive\bin\Debug\20120531\20120531060547.jpg
     文件       10764  2012-05-31 18:06  Client_Receive\bin\Debug\20120531\20120631060547.jpg
     文件       10679  2012-05-31 18:07  Client_Receive\bin\Debug\20120531\20120731060547.jpg
     文件       10787  2012-05-31 18:08  Client_Receive\bin\Debug\20120531\20120831060547.jpg
     文件       10712  2012-05-31 18:09  Client_Receive\bin\Debug\20120531\20120931060547.jpg
     文件       10532  2012-05-31 18:10  Client_Receive\bin\Debug\20120531\20121031060547.jpg
     文件       10681  2012-05-31 18:11  Client_Receive\bin\Debug\20120531\20121131060547.jpg
     文件       10724  2012-05-31 18:12  Client_Receive\bin\Debug\20120531\20121231060547.jpg
     文件       10655  2012-05-31 18:13  Client_Receive\bin\Debug\20120531\20121331060548.jpg
     文件       10697  2012-05-31 18:14  Client_Receive\bin\Debug\20120531\20121431060548.jpg
     文件       10901  2012-05-31 18:15  Client_Receive\bin\Debug\20120531\20121531060548.jpg
     文件       10742  2012-05-31 18:16  Client_Receive\bin\Debug\20120531\20121631060548.jpg
     文件       10571  2012-05-31 18:17  Client_Receive\bin\Debug\20120531\20121731060548.jpg
     文件       10699  2012-05-31 18:18  Client_Receive\bin\Debug\20120531\20121831060548.jpg
     文件       10832  2012-05-31 18:19  Client_Receive\bin\Debug\20120531\20121931060548.jpg
     文件       10930  2012-05-31 18:20  Client_Receive\bin\Debug\20120531\20122031060548.jpg
     文件       10772  2012-05-31 18:21  Client_Receive\bin\Debug\20120531\20122131060548.jpg
     文件       10932  2012-05-31 18:22  Client_Receive\bin\Debug\20120531\20122231060548.jpg
     文件       10897  2012-05-31 18:23  Client_Receive\bin\Debug\20120531\20122331060548.jpg
     文件       10809  2012-05-31 18:24  Client_Receive\bin\Debug\20120531\20122431060548.jpg
     文件       10889  2012-05-31 18:25  Client_Receive\bin\Debug\20120531\20122531060548.jpg
     文件       11000  2012-05-31 18:26  Client_Receive\bin\Debug\20120531\20122631060548.jpg
     文件       10774  2012-05-31 18:27  Client_Receive\bin\Debug\20120531\20122731060548.jpg
     文件       10496  2012-05-31 17:46  Client_Receive\bin\Debug\20120531\20124631050546.jpg
     文件       10716  2012-05-31 17:47  Client_Receive\bin\Debug\20120531\20124731050519.jpg
     文件       10705  2012-05-31 17:47  Client_Receive\bin\Debug\20120531\20124731050546.jpg
............此处省略70个文件信息

评论

共有 条评论