Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.59 KB | None | 0 0
  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. using System.Windows.Forms;
  10.  
  11. namespace WindowsFormsApplication1
  12. {
  13.  
  14.  
  15. public partial class Form1 : Form
  16. {
  17. public Form1()
  18. {
  19. InitializeComponent();
  20. }
  21.  
  22. private void Form1_Load(object sender, EventArgs e)
  23. {
  24. this.Text = "クライアント"; // フォームのタイトル名
  25. button1.Text = "送信"; // 送信ボタンの表示文字
  26. label1.Text = ""; // 状態表示用ラベルを初期化
  27. textBox1.Text = "aあbいcうd"; // 送信用データ
  28. }
  29.  
  30. private void button1_Click(object sender, EventArgs e)
  31. {
  32.  
  33. // TCPクライアントを生成
  34. System.Net.Sockets.TcpClient client = new System.Net.Sockets.TcpClient(); // TCPクライアント
  35. label2.Text = ""; // 受信データ表示用ラベルの初期化
  36.  
  37. try
  38. {
  39.  
  40. // TCP/IP接続を行う
  41. client.Connect("127.0.0.1", 9000);
  42.  
  43. // 通信ストリームの取得
  44. System.Net.Sockets.NetworkStream stream = client.GetStream();
  45.  
  46. // サーバーへ送信
  47. byte[] SendBuffer = System.Text.Encoding.Unicode.GetBytes(textBox1.Text);
  48. stream.Write(SendBuffer, 0, SendBuffer.Length);
  49. stream.Flush(); // フラッシュ(強制書き出し)
  50. label1.Text = "送信成功"; // 正常に送信できた場合
  51.  
  52.  
  53. // サーバーからの受信
  54. byte[] ReceiveData = new byte[1000];
  55. stream.Read(ReceiveData, 0, ReceiveData.Length);
  56. label2.Text = "受信データ : " + System.Text.Encoding.Unicode.GetString(ReceiveData); // 正常に受信できた場合
  57.  
  58. // TCPクライアントをクローズ
  59. client.Close();
  60.  
  61. }
  62. catch (Exception ex)
  63. {
  64. // 接続できなかった場合
  65. label1.Text = ex.Message;
  66. }
  67.  
  68. }
  69. }
  70.  
  71.  
  72.  
  73. }
  74.  
  75. using System;
  76. using System.Collections.Generic;
  77. using System.ComponentModel;
  78. using System.Data;
  79. using System.Drawing;
  80. using System.Linq;
  81. using System.Text;
  82. using System.Threading.Tasks;
  83. using System.Windows.Forms;
  84. using System.Diagnostics;
  85. using System.Windows;
  86.  
  87.  
  88. namespace WindowsFormsApplication2
  89. {
  90. public partial class Form1 : Form
  91. {
  92. public Form1()
  93. {
  94. InitializeComponent();
  95.  
  96. // スレッドからテキストボックスをアクセスすることを指定
  97. Control.CheckForIllegalCrossThreadCalls = false;
  98. }
  99. // メンバー変数
  100. private System.Net.Sockets.Socket ServerSocket; // ソケット
  101. private System.Threading.Thread StartListeningThread; // 接続待ちスレッド
  102. private volatile bool SLTAlive; // 接続待ちスレッド終了指示フラグ(volatile が指定されていることに注意)
  103.  
  104. // フォーム起動時イベント
  105. private void Form1_Load(object sender, EventArgs e)
  106. {
  107. this.Text = "サーバー"; // フォームのタイトル名
  108. button1.Text = "サーバー開始"; // 開始ボタンの表示文字
  109. button2.Text = "サーバー終了"; // 終了ボタンの表示文字
  110. label1.Text = ""; // 状態表示用ラベルを初期化
  111.  
  112. // スレッド終了指示フラグを未終了に初期化
  113. SLTAlive = false;
  114.  
  115.  
  116. }
  117. // フォーム閉鎖時イベント
  118. private void Form1_FormClosed(object sender, FormClosedEventArgs e)
  119. {
  120. if (SLTAlive)
  121. {
  122.  
  123. if (ServerSocket != null)
  124. {
  125. // 接続要求受け入れの終了
  126. ServerSocket.Close();
  127. }
  128.  
  129. // 念のためスレッドをnull設定
  130. StartListeningThread = null;
  131.  
  132.  
  133. // スレッド終了指示フラグを終了に設定
  134. SLTAlive = false;
  135.  
  136. }
  137.  
  138.  
  139. }
  140. // 接続待ち開始ボタンのクリックイベント
  141. private void button1_Click(object sender, EventArgs e)
  142. {
  143. if (!SLTAlive) // まだ接続待ちスレッドを生成していない場合
  144. {
  145.  
  146. // Socket の生成
  147. ServerSocket = new System.Net.Sockets.Socket(
  148. System.Net.Sockets.AddressFamily.InterNetwork, // IP version 4 のアドレス
  149. System.Net.Sockets.SocketType.Stream, // 通信方式をバイトストリーム
  150. System.Net.Sockets.ProtocolType.Tcp); // プロトコルをTCP
  151.  
  152. // ホストのIPアドレスとポート番号の指定
  153. System.Net.IPEndPoint EndPointHost = new System.Net.IPEndPoint(System.Net.IPAddress.Parse("127.0.0.1"), 9000);
  154. // *** System.Net.IPEndPoint EndPointHost = new System.Net.IPEndPoint(System.Net.IPAddress.Any, 9000);
  155.  
  156. ServerSocket.Bind(EndPointHost); // ローカル エンドポイント(IPアドレス等の情報)と関連付け
  157. ServerSocket.Listen(100); // 電文取り出しの接続がまだ保留中におけるキューの最大長
  158.  
  159.  
  160. // 接続待ち用スレッドを作成
  161. StartListeningThread = new System.Threading.Thread(StartListening);
  162.  
  163. // 接続待ち用スレッドを開始
  164. StartListeningThread.Start();
  165.  
  166. // スレッド終了指示フラグを未終了に設定
  167. SLTAlive = true;
  168.  
  169. }
  170. }
  171.  
  172.  
  173. private void button2_Click(object sender, EventArgs e)
  174. {
  175.  
  176. if (SLTAlive) // 接続待ちスレッドが作成されていて使える場合
  177. {
  178. if (ServerSocket != null)
  179. {
  180. // 接続要求受け入れの終了
  181. ServerSocket.Close();
  182. }
  183.  
  184. // スレッド終了指示フラグを終了に設定
  185. SLTAlive = false;
  186.  
  187. label1.Text = "サーバー終了";
  188. }
  189.  
  190. }
  191.  
  192.  
  193. //============
  194. // 接続待ちスレッド用メソッド
  195. private void StartListening()
  196. {
  197.  
  198. label1.Text = "サーバー開始";
  199.  
  200. try
  201. {
  202.  
  203. // 受信の受付を行なうための無限ループ
  204. while (SLTAlive) // スレッド終了指示フラグでの終了指示がある場合はループ終了
  205. {
  206.  
  207. // クライアントからの接続を受け付ける
  208. System.Net.Sockets.Socket ClientSocket = ServerSocket.Accept(); // Socketクライアント
  209.  
  210. // クライアントからの電文の受信
  211. byte[] ReceiveData = new byte[2000];
  212. int ResSize = ClientSocket.Receive(
  213. ReceiveData, ReceiveData.Length,
  214. System.Net.Sockets.SocketFlags.None); // 受信
  215. string str = System.Text.Encoding.Unicode.GetString(ReceiveData);
  216. textBox1.Text = str; // 受信データ
  217.  
  218. Process p = new Process();
  219. p.StartInfo.FileName = System.Environment.GetEnvironmentVariable("ComSpec");
  220. string batchFile = @"C:yahoo.bat";
  221. //trueにすると、コマンドプロンプトが一瞬現れた
  222. p.StartInfo.UseShellExecute = true;
  223. p.StartInfo.CreateNoWindow = true;
  224. p.StartInfo.Arguments = string.Format(@"/c {0}", batchFile);
  225. p.Start();
  226. p.WaitForExit();
  227. p.Close();
  228.  
  229.  
  230.  
  231.  
  232. // 返信電文をクライアントへ送信
  233. byte[] SendBuffer = Encoding.Unicode.GetBytes("本サーバーの御利用ありがとう御座います。");
  234. int i = ClientSocket.Send(SendBuffer);
  235.  
  236. // Socketクライアントをクローズ
  237. ClientSocket.Shutdown(System.Net.Sockets.SocketShutdown.Both);
  238. ClientSocket.Close();
  239.  
  240. }
  241.  
  242. }
  243. catch (Exception ex)
  244. {
  245. label1.Text = "サーバー終了";
  246. }
  247.  
  248. }
  249.  
  250.  
  251. }
  252. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement