프로그래밍/C#

서버 클라이언트 예제 1

mi-nos 2024. 5. 14. 00:04

 

콘솔 실행 후 

콘솔 입력 : CMD_001#DEVICE_069#fe303904 엔터키
콘솔 출력 : DEVICE_069:CMD_001_A#fe303904

class Program
    {
        // Command Info 관리 
        static Dictionary<string, string> CmdInfo = new Dictionary<string, string>();

        static void Main(string[] args)
        {
            // 파일로부터 Command Info 읽어오기 
            LoadCmdInfo();

            String line = Console.ReadLine();

            // command parsing  ex) CMD_002#DEVICE_083,DEVICE_015#ce3c39e1
            string[] words = line.Split('#');
            string command = words[0];
            string[] devices = words[1].Split(',');
            string param = words[2];

            // target 개수만큼 처리 
            string cmdDevice = CmdInfo[command]; // device로 전달할 command 
            foreach (var deviceId in devices)
            {
                Console.WriteLine(string.Format("{0}:{1}#{2}", deviceId, cmdDevice, param));
            }
        }

        
        // 파일로부터 Command Info 읽어오기 
        static void LoadCmdInfo()
        {
            string[] lines = File.ReadAllLines(".\\INFO\\SERVER_COMMAND.TXT");
            foreach(string line in lines)
            {
                string[] words = line.Split('#');
                CmdInfo[words[0]] = words[1];
            }
        }
    }