• R/O
  • SSH
  • HTTPS

Commit

Tags
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

練習用です。いろんなものがごちゃまぜです。


Commit MetaInfo

Revision74 (tree)
Time2015-11-03 14:35:33
Authorbellyoshi

Log Message

Change Summary

Incremental Difference

--- Lianix/Lianxi3/Client/Form1.Designer.cs (revision 73)
+++ Lianix/Lianxi3/Client/Form1.Designer.cs (revision 74)
@@ -47,6 +47,7 @@
4747 this.btn_start.TabIndex = 0;
4848 this.btn_start.Text = "接続";
4949 this.btn_start.UseVisualStyleBackColor = true;
50+ this.btn_start.Click += new System.EventHandler(this.btn_start_Click);
5051 //
5152 // btn_stop
5253 //
@@ -56,6 +57,7 @@
5657 this.btn_stop.TabIndex = 0;
5758 this.btn_stop.Text = "切断";
5859 this.btn_stop.UseVisualStyleBackColor = true;
60+ this.btn_stop.Click += new System.EventHandler(this.btn_stop_Click);
5961 //
6062 // label1
6163 //
@@ -113,6 +115,7 @@
113115 this.btn_msg.TabIndex = 0;
114116 this.btn_msg.Text = "メッセージ";
115117 this.btn_msg.UseVisualStyleBackColor = true;
118+ this.btn_msg.Click += new System.EventHandler(this.btn_msg_Click);
116119 //
117120 // Form1
118121 //
--- Lianix/Lianxi3/Client/Form1.cs (revision 73)
+++ Lianix/Lianxi3/Client/Form1.cs (revision 74)
@@ -1,20 +1,112 @@
11 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;
92 using System.Windows.Forms;
3+using System.Net;
4+using System.Net.Sockets;
5+using System.Threading;
106
117 namespace Client
128 {
139 public partial class Form1 : Form
1410 {
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+
1526 public Form1()
1627 {
1728 InitializeComponent();
1829 }
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+ }
19111 }
20112 }