練習用です。いろんなものがごちゃまぜです。
| @@ -47,6 +47,7 @@ | ||
| 47 | 47 | this.btn_start.TabIndex = 0; |
| 48 | 48 | this.btn_start.Text = "接続"; |
| 49 | 49 | this.btn_start.UseVisualStyleBackColor = true; |
| 50 | + this.btn_start.Click += new System.EventHandler(this.btn_start_Click); | |
| 50 | 51 | // |
| 51 | 52 | // btn_stop |
| 52 | 53 | // |
| @@ -56,6 +57,7 @@ | ||
| 56 | 57 | this.btn_stop.TabIndex = 0; |
| 57 | 58 | this.btn_stop.Text = "切断"; |
| 58 | 59 | this.btn_stop.UseVisualStyleBackColor = true; |
| 60 | + this.btn_stop.Click += new System.EventHandler(this.btn_stop_Click); | |
| 59 | 61 | // |
| 60 | 62 | // label1 |
| 61 | 63 | // |
| @@ -113,6 +115,7 @@ | ||
| 113 | 115 | this.btn_msg.TabIndex = 0; |
| 114 | 116 | this.btn_msg.Text = "メッセージ"; |
| 115 | 117 | this.btn_msg.UseVisualStyleBackColor = true; |
| 118 | + this.btn_msg.Click += new System.EventHandler(this.btn_msg_Click); | |
| 116 | 119 | // |
| 117 | 120 | // Form1 |
| 118 | 121 | // |
| @@ -1,20 +1,112 @@ | ||
| 1 | 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.Text; | |
| 8 | -using System.Threading.Tasks; | |
| 9 | 2 | using System.Windows.Forms; |
| 3 | +using System.Net; | |
| 4 | +using System.Net.Sockets; | |
| 5 | +using System.Threading; | |
| 10 | 6 | |
| 11 | 7 | namespace Client |
| 12 | 8 | { |
| 13 | 9 | public partial class Form1 : Form |
| 14 | 10 | { |
| 11 | + private Socket Sock; | |
| 12 | + private ManualResetEvent connectDone = new ManualResetEvent(false); | |
| 13 | + private ManualResetEvent sendDone = new ManualResetEvent(false); | |
| 14 | + | |
| 15 | + delegate void stateTextSetDelegate(string text); | |
| 16 | + private void stateTextSet(string text) | |
| 17 | + { | |
| 18 | + if (this.IsDisposed) return; | |
| 19 | + Invoke(new stateTextSetDelegate(tbox_stateTextSet), text); | |
| 20 | + } | |
| 21 | + private void tbox_stateTextSet(string text) | |
| 22 | + { | |
| 23 | + tbox_stat.Text = text; | |
| 24 | + } | |
| 25 | + | |
| 15 | 26 | public Form1() |
| 16 | 27 | { |
| 17 | 28 | InitializeComponent(); |
| 18 | 29 | } |
| 30 | + | |
| 31 | + private void btn_start_Click(object sender, EventArgs e) | |
| 32 | + { | |
| 33 | + if (tbox_ip.Text.Trim() == String.Empty) | |
| 34 | + { | |
| 35 | + MessageBox.Show("ipを指定してください。"); | |
| 36 | + return; | |
| 37 | + } | |
| 38 | + if (tbox_port.Text == String.Empty) | |
| 39 | + { | |
| 40 | + MessageBox.Show("portを指定してください。"); | |
| 41 | + return; | |
| 42 | + } | |
| 43 | + int port; | |
| 44 | + if (Int32.TryParse(tbox_port.Text, out port) == false) | |
| 45 | + { | |
| 46 | + MessageBox.Show("portは0~65535の数字を入力してください。"); | |
| 47 | + return; | |
| 48 | + } | |
| 49 | + if (port < 0 || 65535 < port) | |
| 50 | + { | |
| 51 | + MessageBox.Show("portは0~65535の範囲で入力してください。"); | |
| 52 | + return; | |
| 53 | + } | |
| 54 | + | |
| 55 | + IPAddress ipAddress = IPAddress.Parse(tbox_ip.Text.Trim()); | |
| 56 | + IPEndPoint serverEP = new IPEndPoint(ipAddress, port); | |
| 57 | + Sock = new Socket(AddressFamily.InterNetwork, | |
| 58 | + SocketType.Stream, | |
| 59 | + ProtocolType.Tcp); | |
| 60 | + try | |
| 61 | + { | |
| 62 | + Sock.BeginConnect(serverEP, | |
| 63 | + new AsyncCallback(ConnectCallback), | |
| 64 | + Sock); | |
| 65 | + connectDone.WaitOne(); | |
| 66 | + byte[] bb = System.Text.Encoding.UTF8.GetBytes("Client >> Hello \r\n"); | |
| 67 | + Sock.BeginSend(bb, 0, bb.Length, 0, new AsyncCallback(SendCallback), Sock); | |
| 68 | + sendDone.WaitOne(); | |
| 69 | + }catch(Exception ee) | |
| 70 | + { | |
| 71 | + MessageBox.Show(ee.Message); | |
| 72 | + } | |
| 73 | + } | |
| 74 | + private void ConnectCallback(IAsyncResult ar) | |
| 75 | + { | |
| 76 | + Socket client = (Socket)ar.AsyncState; | |
| 77 | + client.EndConnect(ar); | |
| 78 | + stateTextSet(String.Format("{0}に接続しています", client.RemoteEndPoint)); | |
| 79 | + connectDone.Set(); | |
| 80 | + } | |
| 81 | + private void SendCallback(IAsyncResult ar) | |
| 82 | + { | |
| 83 | + Socket client = (Socket)ar.AsyncState; | |
| 84 | + int sentSize = client.EndSend(ar); | |
| 85 | + sendDone.Set(); | |
| 86 | + } | |
| 87 | + | |
| 88 | + private void btn_msg_Click(object sender, EventArgs e) | |
| 89 | + { | |
| 90 | + byte[] bb = System.Text.Encoding.UTF8.GetBytes("Client >>> " + rtbox_msg.Text + "\r\n"); | |
| 91 | + try | |
| 92 | + { | |
| 93 | + Sock.BeginSend(bb, 0, bb.Length, 0, new AsyncCallback(SendCallback), Sock); | |
| 94 | + }catch(Exception ee) | |
| 95 | + { | |
| 96 | + MessageBox.Show(ee.Message); | |
| 97 | + } | |
| 98 | + } | |
| 99 | + | |
| 100 | + private void btn_stop_Click(object sender, EventArgs e) | |
| 101 | + { | |
| 102 | + try | |
| 103 | + { | |
| 104 | + Sock.Close(); | |
| 105 | + stateTextSet("接続していません"); | |
| 106 | + }catch(Exception ee) | |
| 107 | + { | |
| 108 | + MessageBox.Show(ee.Message); | |
| 109 | + } | |
| 110 | + } | |
| 19 | 111 | } |
| 20 | 112 | } |