|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.ComponentModel; |
| 4 | +using System.Data; |
| 5 | +using System.Drawing; |
| 6 | +using System.Linq; |
| 7 | +using System.Net; |
| 8 | +using System.Net.Sockets; |
| 9 | +using System.Text; |
| 10 | +using System.Threading; |
| 11 | +using System.Threading.Tasks; |
| 12 | +using System.Windows.Forms; |
| 13 | + |
| 14 | +namespace JasperMultiThreadClient |
| 15 | +{ |
| 16 | + public partial class Form1 : Form |
| 17 | + { |
| 18 | + Thread thTimerThread; |
| 19 | + TcpClient clientSocket = new TcpClient(); |
| 20 | + NetworkStream serverStream; |
| 21 | + |
| 22 | + string address; |
| 23 | + int port; |
| 24 | + |
| 25 | + public Form1() |
| 26 | + { |
| 27 | + InitializeComponent(); |
| 28 | + } |
| 29 | + |
| 30 | + private void Form1_Load(object sender, EventArgs e) |
| 31 | + { |
| 32 | + address = "127.0.0.1"; |
| 33 | + port = 8888; |
| 34 | + lblServerStatus.Text = DateTime.Now + " 환영합니다.(Welcome)"; |
| 35 | + |
| 36 | + thTimerThread = new Thread(serverTimerStart); |
| 37 | + thTimerThread.Start(); |
| 38 | + } |
| 39 | + |
| 40 | + public void serverTimerStart() |
| 41 | + { |
| 42 | + |
| 43 | + while (true) |
| 44 | + { |
| 45 | + refreshTimer.Interval = 3000; |
| 46 | + refreshTimer.Enabled = true; |
| 47 | + refreshTimer.Start(); |
| 48 | + } |
| 49 | + |
| 50 | + } |
| 51 | + |
| 52 | + private void refreshTimer_Tick(object sender, EventArgs e) |
| 53 | + { |
| 54 | + try |
| 55 | + { |
| 56 | + clientSocket.Connect(address, port); |
| 57 | + lblServerStatus.Text = "서버 작동중...(Server On...)"; |
| 58 | + recvMessage(); |
| 59 | + |
| 60 | + } |
| 61 | + catch (Exception ex) |
| 62 | + { |
| 63 | + lblServerStatus.Text = "서버 멈췄음(Server Stopped)"; |
| 64 | + txtMessages.Text = txtMessages.Text + Environment.NewLine + |
| 65 | + ex.ToString(); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + public void recvMessage() |
| 70 | + { |
| 71 | + NetworkStream serverStream = clientSocket.GetStream(); |
| 72 | + byte[] inStream = new byte[65536]; |
| 73 | + serverStream.Read(inStream, 0, (int)clientSocket.ReceiveBufferSize); |
| 74 | + |
| 75 | + string returnData = System.Text.Encoding.UTF8.GetString(inStream); |
| 76 | + txtMessages.Text = txtMessages.Text + Environment.NewLine + ">>" + returnData; |
| 77 | + |
| 78 | + } |
| 79 | + |
| 80 | + private void btnSend_Click(object sender, EventArgs e) |
| 81 | + { |
| 82 | + sendMessage(); |
| 83 | + } |
| 84 | + |
| 85 | + public void sendMessage() |
| 86 | + { |
| 87 | + string localIP = GetLocalIP(); |
| 88 | + string usrNickname = txtNickname.Text; |
| 89 | + string usrMessage = txtSend.Text; |
| 90 | + |
| 91 | + // 닉네임 |
| 92 | + if (usrNickname == "") |
| 93 | + { |
| 94 | + usrNickname = "익명(Anonymous)"; |
| 95 | + } |
| 96 | + |
| 97 | + // 메시지(message) |
| 98 | + if ( usrMessage == "") |
| 99 | + { |
| 100 | + usrMessage = localIP + ";" + usrNickname + ";\0"; |
| 101 | + } |
| 102 | + else |
| 103 | + { |
| 104 | + usrMessage = localIP + ";" + usrNickname + ";" + usrMessage; |
| 105 | + } |
| 106 | + |
| 107 | + // 연결 여부(Connection) |
| 108 | + if (clientSocket.Connected == true) |
| 109 | + { |
| 110 | + serverStream = clientSocket.GetStream(); |
| 111 | + byte[] outStream = System.Text.Encoding.UTF8.GetBytes(usrMessage); |
| 112 | + serverStream.Write(outStream, 0, outStream.Length); |
| 113 | + serverStream.Flush(); |
| 114 | + } |
| 115 | + else |
| 116 | + { |
| 117 | + try |
| 118 | + { |
| 119 | + clientSocket.Connect(address, port); |
| 120 | + lblServerStatus.Text = "서버 작동중...(Server On...)"; |
| 121 | + serverStream = clientSocket.GetStream(); |
| 122 | + byte[] outStream = System.Text.Encoding.UTF8.GetBytes(usrMessage); |
| 123 | + serverStream.Write(outStream, 0, outStream.Length); |
| 124 | + serverStream.Flush(); |
| 125 | + recvMessage(); |
| 126 | + } |
| 127 | + catch (Exception ex) |
| 128 | + { |
| 129 | + MessageBox.Show("대상 컴퓨터에서 연결을 거부했으므로 연결하지 못했습니다.\n" + |
| 130 | + "(Failed to connect because the destination computer refused to connect.)", |
| 131 | + "메시지(Message)", MessageBoxButtons.OK, MessageBoxIcon.Information); |
| 132 | + lblServerStatus.Text = "오류(Error)"; |
| 133 | + txtMessages.Text = txtMessages + Environment.NewLine + |
| 134 | + ex.ToString(); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + public string GetLocalIP() |
| 140 | + { |
| 141 | + string localIP = "Not available, please check your network seetings!"; |
| 142 | + IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName()); |
| 143 | + foreach (IPAddress ip in host.AddressList) |
| 144 | + { |
| 145 | + if (ip.AddressFamily == AddressFamily.InterNetwork) |
| 146 | + { |
| 147 | + localIP = ip.ToString(); |
| 148 | + break; |
| 149 | + } |
| 150 | + } |
| 151 | + return localIP; |
| 152 | + } |
| 153 | + |
| 154 | + private void Form1_FormClosed(object sender, FormClosedEventArgs e) |
| 155 | + { |
| 156 | + thTimerThread.Abort(); |
| 157 | + } |
| 158 | + } |
| 159 | +} |
0 commit comments