Example #1
0
        public void SaveMMF(Socket sock)
        {
            // Serialize Type 받음
            SType = (SerializeType)SerializationUtil.Deserialize(SendReceive.Receive(sock), typeof(SerializeType));

            // 데이터 길이 받음
            long byteCnt = (long)SerializationUtil.Deserialize(SendReceive.Receive(sock), typeof(long));

            // 묶음 개수 받음
            int bunchCnt = (int)SerializationUtil.Deserialize(SendReceive.Receive(sock), typeof(int));

            // using 쓰면 안됨, isClosed = true로 바뀜
            Memory = MemoryMappedFile.CreateNew(FileName, byteCnt);

            long position = 0;

            for (int i = 0; i < bunchCnt; i++)
            {
                byte[] received = SendReceive.Receive(sock);

                using (var wt = Memory.CreateViewAccessor(position, received.Length))
                {
                    int pos = 0;
                    wt.WriteArray(pos, received, 0, received.Length);
                    position += received.Length;
                }
            }
        }
Example #2
0
        public static void DisconnectionTest()
        {
            IPHostEntry ipEntry = Dns.GetHostEntry(Dns.GetHostName());

            IPAddress[] addr = ipEntry.AddressList;
            Console.WriteLine("enter your role(s, 1,2,3...)");
            string role = Console.ReadLine();

            int portNo = 100;

            if (role == @"s")
            {
                int clCnt = 2;

                Tools.ServerTCP sv = new ServerTCP();
                sv.SetupServer(portNo);

                Action <object> ac = x =>
                {
                    Socket sock = ((Tuple <Socket, int>)x).Item1;
                    int    clNo = ((Tuple <Socket, int>)x).Item2;

                    Console.WriteLine("waits...");
                    Console.ReadKey();

                    SendReceive.Send <bool>(sock, true);
                };

                Task[] tasks = new Task[clCnt];

                for (int i = 0; i < clCnt; i++)
                {
                    Socket clSock = Tools.ServerTCP.GetClientSocket(sv.ServerSocket);
                    Console.WriteLine($"client no.{i} was accepted");
                    tasks[i] = new Task(ac, Tuple.Create(clSock, i));
                    tasks[i].Start();
                }

                for (int i = 0; i < clCnt; i++)
                {
                    tasks[i].Wait();
                }

                Console.WriteLine("server finished");
                Console.ReadKey();
            }
            else
            {
                Tools.ClientTCP cl = new Tools.ClientTCP(addr[1].ToString(), portNo);
                cl.ConnectToServer();

                bool yn = Tools.SendReceive.Receive <bool>(cl.ClientSocket);

                cl.Close();

                Console.WriteLine("client finished");
                Console.ReadKey();
            }
        }
Example #3
0
        public static MemoryMappedFile SaveMMF(string name, Socket sock)
        {
            long byteCnt  = SendReceive.Receive <long>(sock);
            int  bunchCnt = SendReceive.Receive <int>(sock);

            // using 쓰면 안됨, isClosed = true로 바뀜
            var mmf = MemoryMappedFile.CreateNew(name, byteCnt);

            long position = 0;

            for (int i = 0; i < bunchCnt; i++)
            {
                byte[] received = SendReceive.Receive(sock);

                using (var wt = mmf.CreateViewAccessor(position, received.Length))
                {
                    int pos = 0;
                    wt.WriteArray(pos, received, 0, received.Length);
                    position += received.Length;
                }
            }

            return(mmf);
        }