성태의 닷넷 이야기
홈 주인
모아 놓은 자료
프로그래밍
질문/답변
사용자 관리
사용자
메뉴
아티클
외부 아티클
유용한 코드
온라인 기능
MathJax 입력기
최근 덧글
[정성태] Java - How to use the Foreign Funct...
[정성태] 제가 큰 실수를 했군요. ^^; Delegate를 통한 Bein...
[정성태] Working with Rust Libraries from C#...
[정성태] Detecting blocking calls using asyn...
[정성태] 아쉽게도, 커뮤니티는 아니고 개인 블로그입니다. ^^
[정성태] 질문이 잘 이해가 안 됩니다. 우선, 해당 소스코드에서 ILis...
[양승조
] var대신 dinamic으로 선언해서 해결은 했습니다. 맞는 해...
[양승조
] 또 막혔습니다. ㅠㅠ var list = props[i].Ge...
[양승조
] 아. 감사합니다. 어제는 안됐던것 같은데....정신을 차려야겠네...
[정성태] "props[i].GetValue(props[i])" 코드에서 ...
글쓰기
제목
이름
암호
전자우편
HTML
홈페이지
유형
제니퍼 .NET
닷넷
COM 개체 관련
스크립트
VC++
VS.NET IDE
Windows
Team Foundation Server
디버깅 기술
오류 유형
개발 환경 구성
웹
기타
Linux
Java
DDK
Math
Phone
Graphics
사물인터넷
부모글 보이기/감추기
내용
<div style='display: inline'> <h1 style='font-family: Malgun Gothic, Consolas; font-size: 20pt; color: #006699; text-align: center; font-weight: bold'>C# - 간단하게 만들어 보는 리눅스의 nc(netcat), json_pp 프로그램</h1> <p> 리눅스 도구에 보면 nc가 있는데,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > nc - Unix, Linux Command ; <a target='tab' href='https://www.tutorialspoint.com/unix_commands/nc.htm'>https://www.tutorialspoint.com/unix_commands/nc.htm</a> </pre> <br /> 윈도우에는 없군요. ^^ 검색해 보면 다음의 링크가 있지만,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > netcat ; <a target='tab' href='https://eternallybored.org/misc/netcat/'>https://eternallybored.org/misc/netcat/</a> $ sudo apt install netcat </pre> <br /> 그래도 기본적인 기능이라면 C#으로 다음과 같이 쉽게 만들 수 있습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > using System; using System.IO; using System.Net; using System.Net.Sockets; namespace netcat { class Program { static void Main(string[] args) { if (args.Length != 2) { return; } if (Console.IsInputRedirected == false) { Console.WriteLine("No input redirected"); return; } int port = int.Parse(args[1]); using (TcpClient client = new TcpClient(args[0], port)) { using (NetworkStream ns = client.GetStream()) <span style='color: blue; font-weight: bold'>using (BinaryReader br = new BinaryReader(Console.OpenStandardInput()))</span> { while (true) { byte[] buffer = br.ReadBytes(512); if (buffer == null || buffer.Length == 0) { break; } ns.Write(buffer, 0, buffer.Length); } } } } } } </pre> <br /> 빌드한 후 이런 식으로 테스트할 수 있고,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > C:\temp> <span style='color: blue; font-weight: bold'>echo "Hello!" | netcat localhost 9900</span> C:\temp> <span style='color: blue; font-weight: bold'>type data.txt | netcat localhost 9900</span> </pre> <br /> 그럼 pipeline으로 연결된 "Hello!" 문자열을, 그리고 "data.txt" 파일의 내용을 netcat이 전달받아 "localhost:9900" TCP 서버에 전송합니다.<br /> <br /> <hr style='width: 50%' /><br /> <a name='json_pp'></a> <br /> 유사하게 json_pp도 만들 수 있습니다. 주요 작업은 Newtonsoft.Json 패키지가 다 해주므로 다음과 같이 간단하게 코딩할 수 있습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > using Newtonsoft.Json; using System; using System.IO; namespace json_pp { class Program { static void Main(string[] args) { if (BclExtension.ConsoleHelper.IsInputHandleRedirected() == false) { Console.WriteLine("No input redirected"); Help(); return; } bool verbose = false; if (args.Length == 1 && args[0] == "-v") { verbose = true; } using (MemoryStream ms = new MemoryStream()) using (BinaryReader br = new BinaryReader(Console.OpenStandardInput())) { while (true) { byte[] buffer = br.ReadBytes(512); if (buffer == null || buffer.Length == 0) { break; } ms.Write(buffer, 0, buffer.Length); } ms.Position = 0; using (StreamReader sr = new StreamReader(ms)) using (StringWriter sw = new StringWriter()) { string text = sr.ReadToEnd(); if (verbose == true) { Console.WriteLine(text); } using (StringReader textReader = new StringReader(text)) { Newtonsoft.Json.JsonTextReader jtr = new Newtonsoft.Json.JsonTextReader(textReader); var jsonWriter = new JsonTextWriter(sw) { Formatting = Formatting.Indented }; jsonWriter.WriteToken(jtr); Console.WriteLine(sw.ToString()); } } } } private static void Help() { string appName = Path.GetFileNameWithoutExtension(typeof(Program).Assembly.Location); Console.WriteLine($"(redirect_source) | {appName} [-v]"); Console.WriteLine($"ex:"); Console.WriteLine("\techo \"{ \\\"foo\\\": 500 }\" | " + appName); } } } </pre> <br /> <hr style='width: 50%' /><br /> <br /> Github에도 소스 코드 및 빌드된 바이너리를 올려두었습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > stjeong / Utilities / netcat ; <a target='tab' href='https://github.com/stjeong/Utilities/tree/master/netcat'>https://github.com/stjeong/Utilities/tree/master/netcat</a> stjeong / Utilities / json_pp ; <a target='tab' href='https://github.com/stjeong/Utilities/tree/master/json_pp'>https://github.com/stjeong/Utilities/tree/master/json_pp</a> Utilities.zip ; <a target='tab' href='https://github.com/stjeong/Utilities/releases'>https://github.com/stjeong/Utilities/releases</a> </pre> </p><br /> <br /><hr /><span style='color: Maroon'>[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]</span> </div>
첨부파일
스팸 방지용 인증 번호
3598
(왼쪽의 숫자를 입력해야 합니다.)