본문 바로가기

프로그래밍/C#

client 다른 버전

using System;
using System.IO;
using System.Net;
using System.Net.Sockets;

namespace SUB5
{
    class SUB5
    {
        const string strIP = "127.0.0.1";
        const int BUF_SIZE = 4096;
        const int PORT = 9090;
        public static void StartClient(string fpath)
        {
            // Data buffer for incoming data. 
            byte[] bytes = new byte[BUF_SIZE];

            // Connect to a remote device. 
            try
            {
                // Establish the remote endpoint for the socket. 
                // This example uses port 11000 on the local computer. 
                IPAddress ipAddress = IPAddress.Parse(strIP);
                IPEndPoint remoteEP = new IPEndPoint(ipAddress, PORT);

                // Create a TCP/IP  socket. 
                Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                // Connect the socket to the remote endpoint. Catch any errors. 
                try
                {
                    sender.Connect(remoteEP);

                    //Console.WriteLine("Socket connected to {0}", sender.RemoteEndPoint.ToString()); 

                    NetworkStream ns = new NetworkStream(sender);
                    BinaryWriter bw = new BinaryWriter(ns);

                    DirectoryInfo di = new DirectoryInfo(fpath);
                    FileInfo[] fiArr = di.GetFiles();
                    foreach (FileInfo infoFile in fiArr)
                    {
                        bw.Write(infoFile.Name);
                        long lSize = infoFile.Length;
                        bw.Write(lSize);
                        FileStream fs = new FileStream(infoFile.FullName, FileMode.Open);
                        while (lSize > 0)
                        {
                            int nReadLen = fs.Read(bytes, 0, Math.Min(BUF_SIZE, (int)lSize));
                            bw.Write(bytes, 0, nReadLen);
                            lSize -= nReadLen;
                        }
                        fs.Close();
                    }

                    bw.Close();
                    ns.Close();

                    // Release the socket. 
                    sender.Shutdown(SocketShutdown.Both);
                    sender.Close();

                    Console.WriteLine("Finished.");
                }
                catch (ArgumentNullException ane)
                {
                    Console.WriteLine("ArgumentNullException : {0}", ane.ToString());
                }
                catch (SocketException se)
                {
                    Console.WriteLine("SocketException : {0}", se.ToString());
                }
                catch (Exception e)
                {
                    Console.WriteLine("Unexpected exception : {0}", e.ToString());
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }

        static void Main(string[] args)
        {
                StartClient("../../MERGED");
        }
    }
}

반응형

'프로그래밍 > C#' 카테고리의 다른 글

server 다른 버전  (0) 2019.08.29
C# 문자->숫자  (0) 2019.03.24
C# 문자열->시간  (0) 2019.03.24
C# 역문자열  (0) 2019.03.24
C# 문자열, 숫자, 영어 검사  (0) 2019.03.24