Programming/C/C++/C#
C# 에코 서버 만들기
휘탱
2017. 7. 31. 04:45
에코 서버 만들기
서버 예제 코드
예제
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | using System; using System.Net; using System.Net.Sockets; using System.Text; namespace MainServer { class Program { static void Main(string[] args) { EchoServer(); } static void EchoServer() { var tcpListener = new TcpListener(IPAddress.Any, 8002); tcpListener.Start(); byte[] buffer = new byte[1024]; int totalByteCount = 0; int readByteCount = 0; Console.WriteLine("에코 서버 시작"); var tcpClient = tcpListener.AcceptTcpClient(); var ns = tcpClient.GetStream(); while (true) { readByteCount = ns.Read(buffer, 0, buffer.Length); if (readByteCount == 0) break; totalByteCount += readByteCount; byte[] data = new byte[readByteCount]; Array.Copy(buffer, data, readByteCount); ns.Write(data, 0, readByteCount); Console.WriteLine(Encoding.ASCII.GetString(data)); } Console.WriteLine("\n에코 서버 종료"); ns.Close(); tcpClient.Close(); tcpListener.Stop(); } } } | cs |
클라이언트 예제 코드
예제
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Net.Sockets; using System.Net; namespace ClientTool { /// <summary> /// MainWindow.xaml에 대한 상호 작용 논리 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); // IP입력 IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName()); for (int i = 0; i < host.AddressList.Length; i++) { if (host.AddressList[i].AddressFamily == AddressFamily.InterNetwork) { IPInput.Text = host.AddressList[i].ToString(); break; } } } TcpClient m_tcpClient; NetworkStream m_networkStream; List<string> m_inputHistory = new List<string>(); int m_inputHistoryIndex = 0; private void Button_EchoServerConnection(object sender, RoutedEventArgs e) { if (m_tcpClient == null) { m_tcpClient = new TcpClient(IPInput.Text, 8002); } SendNetworkStream("Hello World! Strnig Echo Start!"); } void SendNetworkStream(string data) { if (!m_tcpClient.Connected) return; if (data == null) { m_networkStream.Close(); m_tcpClient.Close(); return; } m_networkStream = m_tcpClient.GetStream(); byte[] buffer = new byte[data.Length]; byte[] sendMessage = new byte[data.Length]; sendMessage = Encoding.ASCII.GetBytes(data); m_networkStream.Write(sendMessage, 0, sendMessage.Length); int totalCount = 0; int readCount = 0; while (totalCount < sendMessage.Length) { readCount = m_networkStream.Read(buffer, 0, buffer.Length); totalCount += readCount; string readMessage = Encoding.ASCII.GetString(buffer); AddItem_TextListBox(readMessage); } AddItem_TextListBox($"전체 Read byte:{totalCount}"); } private void InputTextBox_PreviewKeyDown(object sender, KeyEventArgs e) { // 아무것도 안치고 엔터를 눌렀을 때의 처리 if (string.IsNullOrEmpty(InputTextBox.Text) && e.Key == Key.Enter) { AddItem_TextListBox("명령어 목록: clear, reset, exit, 리셋, 종료"); return; } if (m_inputHistory.Count == 0) return; if (e.Key == Key.Up) { m_inputHistoryIndex -= 1; if (m_inputHistoryIndex < 0) m_inputHistoryIndex = 0; InputTextBox.Text = m_inputHistory[m_inputHistoryIndex]; } else if (e.Key == Key.Down) { m_inputHistoryIndex += 1; if (m_inputHistoryIndex < m_inputHistory.Count) { InputTextBox.Text = m_inputHistory[m_inputHistoryIndex]; } else { m_inputHistoryIndex = m_inputHistory.Count - 1; InputTextBox.Text = string.Empty; } } } private void InputTextBox_KeyUp(object sender, KeyEventArgs e) { if (e.Key != Key.Enter) return; e.Handled = true; if (string.IsNullOrWhiteSpace(InputTextBox.Text)) { return; } switch (InputTextBox.Text) { case "clear": case "reset": case "리셋": TextListBox.Items.Clear(); return; case "exit": case "종료": SendNetworkStream(null); Close(); break; } if (InputTextBox.Text.Contains("echo")) { // echo 서버에 요청함 SendNetworkStream(InputTextBox.Text); } m_inputHistory.Add(InputTextBox.Text); m_inputHistoryIndex = m_inputHistory.Count; InputTextBox.Text = string.Empty; } #region TextListBox void AddItem_TextListBox(string item) { var index = TextListBox.Items.Add(item); TextListBox.ScrollIntoView(TextListBox.Items[index]); } #endregion } } | cs |
결과
반응형