728x90
Form1.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 | using System; using System.Windows.Forms; using System.Threading; namespace CsharpExample { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Client c = new Client(); MessageBox.Show("수신 완료!", "알림"); // 텍스트 박스에 서버로부터 받은 메시지 출력 textBox1.Text = c.returnMessageFromServer(); } private void button2_Click(object sender, EventArgs e) { // UI freeze(화면 정지)를 막기 위해 쓰레드 생성 new Thread(new ThreadStart(Tread_CreateServer)).Start(); MessageBox.Show("서버 생성됨", "알림"); } public void Tread_CreateServer() { Server s = new Server(); s.Run(); } } } | cs |
Form1.Designer.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 | namespace CsharpExample { partial class Form1 { /// <summary> /// 필수 디자이너 변수입니다. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 사용 중인 모든 리소스를 정리합니다. /// </summary> /// <param name="disposing">관리되는 리소스를 삭제해야 하면 true이고, 그렇지 않으면 false입니다.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form 디자이너에서 생성한 코드 /// <summary> /// 디자이너 지원에 필요한 메서드입니다. /// 이 메서드의 내용을 코드 편집기로 수정하지 마십시오. /// </summary> private void InitializeComponent() { this.textBox1 = new System.Windows.Forms.TextBox(); this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(12, 12); this.textBox1.Name = "textBox1"; this.textBox1.ReadOnly = true; this.textBox1.Size = new System.Drawing.Size(500, 25); this.textBox1.TabIndex = 2; // // button1 // this.button1.Location = new System.Drawing.Point(518, 12); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(50, 25); this.button1.TabIndex = 3; this.button1.Text = "받기"; this.button1.Click += new System.EventHandler(this.button1_Click); // // button2 // this.button2.Location = new System.Drawing.Point(12, 43); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(87, 25); this.button2.TabIndex = 4; this.button2.Text = "서버 생성"; this.button2.Click += new System.EventHandler(this.button2_Click); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(630, 89); this.Controls.Add(this.textBox1); this.Controls.Add(this.button1); this.Controls.Add(this.button2); this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; } } | cs |
Server.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 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.Net.Sockets; namespace CsharpExample { class Server { private IPEndPoint ipep; private Socket server; public Server() { Initialize(); } private void Initialize() { ipep = new IPEndPoint(IPAddress.Any, 9999); server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); } public void Run() { server.Bind(ipep); server.Listen(20); Socket client = server.Accept(); IPEndPoint ip = (IPEndPoint)client.RemoteEndPoint; String buf = "고귀양이 서버입니다. 반갑습니다."; Byte[] data = Encoding.Default.GetBytes(buf); client.Send(data); data = new Byte[1024]; client.Receive(data); buf = Encoding.Default.GetString(data); client.Close(); server.Close(); } } } | cs |
Client.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 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.Net.Sockets; namespace CsharpExample { class Client { private IPEndPoint ipep; private Socket client; public Client() { Initialize(); } private void Initialize() { ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9999); client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); } public String returnMessageFromServer() { client.Connect(ipep); Byte[] _data = new Byte[1024]; client.Receive(_data); String _buf = Encoding.Default.GetString(_data); client.Close(); return _buf; } } } | cs |
실행한 모습
728x90