bate's blog

調べたこと実装したことなどを取りとめもなく書きます。

UDPテストコード

フォームで簡単なUDPテストをした。
localhost内なので実際のインターネットで動くかは分からない。


#pragma once


namespace UDP {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
	using namespace System::Runtime::InteropServices;
	using namespace System::Threading;

	/// <summary>
	/// Form1 の概要
	///
	/// 警告: このクラスの名前を変更する場合、このクラスが依存するすべての .resx ファイルに関連付けられた
	///          マネージ リソース コンパイラ ツールに対して 'Resource File Name' プロパティを
	///          変更する必要があります。この変更を行わないと、
	///          デザイナと、このフォームに関連付けられたローカライズ済みリソースとが、
	///          正しく相互に利用できなくなります。
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: ここにコンストラクタ コードを追加します
			//
			m_WSAData = new WSADATA;
			WSAStartup(MAKEWORD(2,0), m_WSAData);

			u_long val = 1;
			m_SendSocket = socket(AF_INET, SOCK_DGRAM, 0);
			ioctlsocket(m_SendSocket, FIONBIO, &val);
			m_SendAddr = new sockaddr_in;
			m_SendAddr->sin_family = AF_INET;
			m_RecvSocket = socket(AF_INET, SOCK_DGRAM, 0);
			ioctlsocket(m_RecvSocket, FIONBIO, &val);
			m_RecvAddr = new sockaddr_in;
			m_RecvAddr->sin_family = AF_INET;
		}

	protected:
		/// <summary>
		/// 使用中のリソースをすべてクリーンアップします。
		/// </summary>
		~Form1()
		{
			closesocket(m_SendSocket);
			delete m_SendAddr;
			closesocket(m_RecvSocket);
			delete m_RecvAddr;

			WSACleanup();

			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::TextBox^  textBox_MyIP;
	private: System::Windows::Forms::TextBox^  textBox_MyPort;
	protected: 


	private: System::Windows::Forms::Label^  label_MyIP;
	private: System::Windows::Forms::Label^  label_MyPort;
	protected: 

	protected: 









	private: System::Windows::Forms::GroupBox^  groupBox_MySettings;
	private: System::Windows::Forms::GroupBox^  groupBox_targetSettings;
	private: System::Windows::Forms::Label^  label_TargetPort;

	private: System::Windows::Forms::Label^  label_TargetIP;
	private: System::Windows::Forms::TextBox^  textBox_TargetPort;


	private: System::Windows::Forms::TextBox^  textBox_TargetIP;
	private: System::Windows::Forms::TextBox^  textBox_Comment;
	private: System::Windows::Forms::Button^  button_Send;



	private: System::Windows::Forms::ListBox^  listBox_Chat;


//-----------------------------------
	private:
		sockaddr_in* m_SendAddr;
		SOCKET m_SendSocket;

		sockaddr_in* m_RecvAddr;
		SOCKET m_RecvSocket;

		WSADATA* m_WSAData;
//-----------------------------------

	private: System::Windows::Forms::Timer^  timer_checkRecv;
	private: System::Windows::Forms::Button^  button_OK;
	private: System::ComponentModel::IContainer^  components;


	private:
		/// <summary>
		/// 必要なデザイナ変数です。
		/// </summary>


#pragma region Windows Form Designer generated code
		/// <summary>
		/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
		/// コード エディタで変更しないでください。
		/// </summary>
		void InitializeComponent(void)
		{
			this->components = (gcnew System::ComponentModel::Container());
			this->textBox_MyIP = (gcnew System::Windows::Forms::TextBox());
			this->textBox_MyPort = (gcnew System::Windows::Forms::TextBox());
			this->label_MyIP = (gcnew System::Windows::Forms::Label());
			this->label_MyPort = (gcnew System::Windows::Forms::Label());
			this->groupBox_MySettings = (gcnew System::Windows::Forms::GroupBox());
			this->groupBox_targetSettings = (gcnew System::Windows::Forms::GroupBox());
			this->label_TargetPort = (gcnew System::Windows::Forms::Label());
			this->label_TargetIP = (gcnew System::Windows::Forms::Label());
			this->textBox_TargetPort = (gcnew System::Windows::Forms::TextBox());
			this->textBox_TargetIP = (gcnew System::Windows::Forms::TextBox());
			this->textBox_Comment = (gcnew System::Windows::Forms::TextBox());
			this->button_Send = (gcnew System::Windows::Forms::Button());
			this->listBox_Chat = (gcnew System::Windows::Forms::ListBox());
			this->timer_checkRecv = (gcnew System::Windows::Forms::Timer(this->components));
			this->button_OK = (gcnew System::Windows::Forms::Button());
			this->groupBox_MySettings->SuspendLayout();
			this->groupBox_targetSettings->SuspendLayout();
			this->SuspendLayout();
			// 
			// textBox_MyIP
			// 
			this->textBox_MyIP->Location = System::Drawing::Point(42, 18);
			this->textBox_MyIP->Name = L"textBox_MyIP";
			this->textBox_MyIP->Size = System::Drawing::Size(100, 19);
			this->textBox_MyIP->TabIndex = 0;
			this->textBox_MyIP->TextChanged += gcnew System::EventHandler(this, &Form1::textBox_MyIP_TextChanged);
			// 
			// textBox_MyPort
			// 
			this->textBox_MyPort->Location = System::Drawing::Point(42, 43);
			this->textBox_MyPort->Name = L"textBox_MyPort";
			this->textBox_MyPort->Size = System::Drawing::Size(100, 19);
			this->textBox_MyPort->TabIndex = 1;
			this->textBox_MyPort->TextChanged += gcnew System::EventHandler(this, &Form1::textBox_MyPort_TextChanged);
			// 
			// label_MyIP
			// 
			this->label_MyIP->AutoSize = true;
			this->label_MyIP->Location = System::Drawing::Point(6, 21);
			this->label_MyIP->Name = L"label_MyIP";
			this->label_MyIP->Size = System::Drawing::Size(15, 12);
			this->label_MyIP->TabIndex = 2;
			this->label_MyIP->Text = L"IP";
			// 
			// label_MyPort
			// 
			this->label_MyPort->AutoSize = true;
			this->label_MyPort->Location = System::Drawing::Point(6, 46);
			this->label_MyPort->Name = L"label_MyPort";
			this->label_MyPort->Size = System::Drawing::Size(26, 12);
			this->label_MyPort->TabIndex = 3;
			this->label_MyPort->Text = L"Port";
			// 
			// groupBox_MySettings
			// 
			this->groupBox_MySettings->Controls->Add(this->textBox_MyIP);
			this->groupBox_MySettings->Controls->Add(this->textBox_MyPort);
			this->groupBox_MySettings->Controls->Add(this->label_MyPort);
			this->groupBox_MySettings->Controls->Add(this->label_MyIP);
			this->groupBox_MySettings->Location = System::Drawing::Point(12, 12);
			this->groupBox_MySettings->Name = L"groupBox_MySettings";
			this->groupBox_MySettings->Size = System::Drawing::Size(154, 71);
			this->groupBox_MySettings->TabIndex = 5;
			this->groupBox_MySettings->TabStop = false;
			this->groupBox_MySettings->Text = L"my settings";
			// 
			// groupBox_targetSettings
			// 
			this->groupBox_targetSettings->Controls->Add(this->label_TargetPort);
			this->groupBox_targetSettings->Controls->Add(this->label_TargetIP);
			this->groupBox_targetSettings->Controls->Add(this->textBox_TargetPort);
			this->groupBox_targetSettings->Controls->Add(this->textBox_TargetIP);
			this->groupBox_targetSettings->Location = System::Drawing::Point(181, 12);
			this->groupBox_targetSettings->Name = L"groupBox_targetSettings";
			this->groupBox_targetSettings->Size = System::Drawing::Size(154, 71);
			this->groupBox_targetSettings->TabIndex = 6;
			this->groupBox_targetSettings->TabStop = false;
			this->groupBox_targetSettings->Text = L"target settings";
			// 
			// label_TargetPort
			// 
			this->label_TargetPort->AutoSize = true;
			this->label_TargetPort->Location = System::Drawing::Point(12, 48);
			this->label_TargetPort->Name = L"label_TargetPort";
			this->label_TargetPort->Size = System::Drawing::Size(26, 12);
			this->label_TargetPort->TabIndex = 3;
			this->label_TargetPort->Text = L"Port";
			// 
			// label_TargetIP
			// 
			this->label_TargetIP->AutoSize = true;
			this->label_TargetIP->Location = System::Drawing::Point(10, 22);
			this->label_TargetIP->Name = L"label_TargetIP";
			this->label_TargetIP->Size = System::Drawing::Size(15, 12);
			this->label_TargetIP->TabIndex = 2;
			this->label_TargetIP->Text = L"IP";
			// 
			// textBox_TargetPort
			// 
			this->textBox_TargetPort->Location = System::Drawing::Point(48, 43);
			this->textBox_TargetPort->Name = L"textBox_TargetPort";
			this->textBox_TargetPort->Size = System::Drawing::Size(100, 19);
			this->textBox_TargetPort->TabIndex = 1;
			this->textBox_TargetPort->TextChanged += gcnew System::EventHandler(this, &Form1::textBox_TargetPort_TextChanged);
			// 
			// textBox_TargetIP
			// 
			this->textBox_TargetIP->Location = System::Drawing::Point(48, 18);
			this->textBox_TargetIP->Name = L"textBox_TargetIP";
			this->textBox_TargetIP->Size = System::Drawing::Size(100, 19);
			this->textBox_TargetIP->TabIndex = 0;
			this->textBox_TargetIP->TextChanged += gcnew System::EventHandler(this, &Form1::textBox_TargetIP_TextChanged);
			// 
			// textBox_Comment
			// 
			this->textBox_Comment->Location = System::Drawing::Point(12, 256);
			this->textBox_Comment->Name = L"textBox_Comment";
			this->textBox_Comment->Size = System::Drawing::Size(236, 19);
			this->textBox_Comment->TabIndex = 8;
			// 
			// button_Send
			// 
			this->button_Send->Location = System::Drawing::Point(260, 256);
			this->button_Send->Name = L"button_Send";
			this->button_Send->Size = System::Drawing::Size(75, 23);
			this->button_Send->TabIndex = 9;
			this->button_Send->Text = L"Send";
			this->button_Send->UseVisualStyleBackColor = true;
			this->button_Send->Click += gcnew System::EventHandler(this, &Form1::button_Send_Click);
			// 
			// listBox_Chat
			// 
			this->listBox_Chat->FormattingEnabled = true;
			this->listBox_Chat->ItemHeight = 12;
			this->listBox_Chat->Location = System::Drawing::Point(12, 126);
			this->listBox_Chat->Name = L"listBox_Chat";
			this->listBox_Chat->Size = System::Drawing::Size(323, 124);
			this->listBox_Chat->TabIndex = 10;
			// 
			// timer_checkRecv
			// 
			this->timer_checkRecv->Enabled = true;
			this->timer_checkRecv->Interval = 1000;
			this->timer_checkRecv->Tick += gcnew System::EventHandler(this, &Form1::timer_checkRecv_Tick);
			// 
			// button_OK
			// 
			this->button_OK->Location = System::Drawing::Point(131, 89);
			this->button_OK->Name = L"button_OK";
			this->button_OK->Size = System::Drawing::Size(75, 23);
			this->button_OK->TabIndex = 11;
			this->button_OK->Text = L"OK";
			this->button_OK->UseVisualStyleBackColor = true;
			this->button_OK->Click += gcnew System::EventHandler(this, &Form1::button_OK_Click);
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(351, 287);
			this->Controls->Add(this->button_OK);
			this->Controls->Add(this->listBox_Chat);
			this->Controls->Add(this->button_Send);
			this->Controls->Add(this->textBox_Comment);
			this->Controls->Add(this->groupBox_targetSettings);
			this->Controls->Add(this->groupBox_MySettings);
			this->Name = L"Form1";
			this->Text = L"Form1";
			this->groupBox_MySettings->ResumeLayout(false);
			this->groupBox_MySettings->PerformLayout();
			this->groupBox_targetSettings->ResumeLayout(false);
			this->groupBox_targetSettings->PerformLayout();
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
	private: System::Void textBox_MyIP_TextChanged(System::Object^  sender, System::EventArgs^  e) {
				 std::string tmp;
				 IntPtr mptr = Marshal::StringToHGlobalAnsi(static_cast<TextBox^>(sender)->Text);
				 tmp = static_cast<const char*>(mptr.ToPointer());
				 m_RecvAddr->sin_addr.S_un.S_addr = inet_addr(tmp.c_str());
				 Marshal::FreeHGlobal(mptr);
			 }
private: System::Void textBox_MyPort_TextChanged(System::Object^  sender, System::EventArgs^  e) {
			 std::string tmp;
			 IntPtr mptr = Marshal::StringToHGlobalAnsi(static_cast<TextBox^>(sender)->Text);
			 tmp = static_cast<const char*>(mptr.ToPointer());
			 m_RecvAddr->sin_port = htons((u_short)atoi(tmp.c_str()));	// やばいかも
			 Marshal::FreeHGlobal(mptr);
		 }
private: System::Void textBox_TargetIP_TextChanged(System::Object^  sender, System::EventArgs^  e) {
			 std::string tmp;
			 IntPtr mptr = Marshal::StringToHGlobalAnsi(static_cast<TextBox^>(sender)->Text);
			 tmp = static_cast<const char*>(mptr.ToPointer());
			 m_SendAddr->sin_addr.S_un.S_addr = inet_addr(tmp.c_str());
			 Marshal::FreeHGlobal(mptr);
		 }
private: System::Void textBox_TargetPort_TextChanged(System::Object^  sender, System::EventArgs^  e) {
			 std::string tmp;
			 IntPtr mptr = Marshal::StringToHGlobalAnsi(static_cast<TextBox^>(sender)->Text);
			 tmp = static_cast<const char*>(mptr.ToPointer());
			 m_SendAddr->sin_port = htons((u_short)atoi(tmp.c_str()));	// やばいかも
			 Marshal::FreeHGlobal(mptr);
		 }
private: System::Void timer_checkRecv_Tick(System::Object^  sender, System::EventArgs^  e) {
			 char buf[512] = { 0 };
			 int n = recv(m_RecvSocket, buf, sizeof(buf), 0);
			 if(n > 1) {
				 String^ text = gcnew String(buf);
				 listBox_Chat->Items->Add(text);
			 }
		 }
private: System::Void button_Send_Click(System::Object^  sender, System::EventArgs^  e) {
			 std::string tmp;
			 IntPtr mptr = Marshal::StringToHGlobalAnsi(textBox_Comment->Text);
			 tmp = static_cast<const char*>(mptr.ToPointer());
			 m_RecvAddr->sin_port = htons((u_short)atoi(tmp.c_str()));	// やばいかも
			 Marshal::FreeHGlobal(mptr);
			 int len = tmp.length();
			 sendto(m_SendSocket, tmp.c_str(), len, 0, (sockaddr*)m_SendAddr, sizeof(sockaddr_in));
		 }
private: System::Void button_OK_Click(System::Object^  sender, System::EventArgs^  e) {
//			 int er1 = bind(m_SendSocket, (sockaddr*)m_SendAddr, sizeof(sockaddr_in));
			 int er2 = bind(m_RecvSocket, (sockaddr*)m_RecvAddr, sizeof(sockaddr_in));
		 }
};
}