送信側(サーバ)
int groupPort = 11000; IPEndPoint ^groupEP = gcnew IPEndPoint(IPAddress::Parse("255.255.255.255"), groupPort); //ブロードキャストの場合 //groupEP = gcnew IPEndPoint(IPAddress::Broadcast, groupPort); groupEP = gcnew IPEndPoint(IPAddress::Parse("192.168.1.28"), groupPort); Socket ^socket = gcnew Socket(AddressFamily::InterNetwork, SocketType::Dgram, ProtocolType::Udp); socket->SetSocketOption(SocketOptionLevel::Socket, SocketOptionName::Broadcast, 1); socket->SendTo(System::Text::ASCIIEncoding::ASCII->GetBytes(textBox1->Text), groupEP);受信側(クライアント)
Socket ^listener = gcnew Socket(AddressFamily::InterNetwork, SocketType::Dgram,ProtocolType::Udp); IPEndPoint ^localEndPoint = gcnew IPEndPoint(IPAddress::Any, 11000); listener->Bind(localEndPoint); EndPoint ^ep = gcnew IPEndPoint(0,0); System::Diagnostics::Debug::WriteLine("Ready to receive…"); array<Byte> ^data = gcnew array<Byte>(1024); int recv = listener->ReceiveFrom(data, ep); String ^stringData = Encoding::ASCII->GetString(data, 0, recv); System::Diagnostics::Debug::WriteLine(stringData); listener->Close();
0 件のコメント:
コメントを投稿