   A malformed packet?
  Hey there guys, question for you.  Right now I am working on a launcher for a game using Windows Forms (VC++) and .NET 3.5.  I have it set up to generate a packet and send it to the server as a "knock" to verify that the server is on and working correctly.  The problem is, when the packet gets sent, it is detected as a malformed/mangled packet and spit back.  I'm not quite sure why this is happening, as I've never had this problem with other code.  (And ignore the highly static code, this was simply done as a test before I did the main app.) cli::array<unsigned char, 1>^ dgram = gcnew cli::array<unsigned char, 1>(4); dgram->SetValue('0', 0); dgram->SetValue('A', 1); dgram->SetValue('0', 2); dgram->SetValue('D', 3); String^ ip = gcnew String("66.199.242.186"); String^ ip_home = gcnew String("192.168.1.102"); Net::IPAddress^ ipa_out = Net::IPAddress::Parse(ip); Net::IPAddress^ ipa_home = Net::IPAddress::Parse(ip_home); Net::IPEndPoint^ ipe_out = gcnew Net::IPEndPoint(ipa_out, 34500); Net::IPEndPoint^ ipe_home = gcnew Net::IPEndPoint(ipa_home, 9000); Net::IPEndPointipe_back; System::Net::Sockets::Socket^ gcso = gcnew Net::Sockets::Socket(Net::Sockets::AddressFamily::InterNetwork, Net::Sockets::SocketType::Dgram, Net::Sockets::ProtocolType::Udp); gcso->Bind(ipe_home); gcso->ReceiveTimeout = 800000000; gcso->Blocking = false; try { MessageBox::Show("Sending packet."); gcso->SendTo(dgram, ipe_out); MessageBox::Show("Sent packet."); } catch(...) { MessageBox::Show("Error sending packet."); } try { System::Boolean inc_true = gcso->Poll(5000000, Net::Sockets::SelectMode::SelectRead); if(inc_true == true) {array<unsigned char, 1>^ dgramback = gcnew array<unsigned char, 1>(1024);int a = gcso->ReceiveFrom(dgramback, ^ipe_back);if(dgramback->GetValue(0) == '0' && dgramback->GetValue(1) == 'A'){ MessageBox::Show("YAY");}} else MessageBox::Show("None for you."); } catch(...) { MessageBox::Show("You looose the game."); } }
  I've seen that happening once when I didn't format the address structure correctly.apart from that, unless you start hacking packet headers, that should not happen.EDIT : try Bind() to (IPAddress.Any, 0) on client, and (IPAddress.Any, 34500) on server.   ;Quote:Original post by oliiiI've seen that happening once when I didn't format the address structure correctly.apart from that, unless you start hacking packet headers, that should not happen.EDIT : try Bind() to (IPAddress.Any, 0) on client, and (IPAddress.Any, 34500) on server.Guess what?  It worked.  No idea why, but it worked.  Thanks a lot man, :D(Note, the server didn't use .NET, its unix based and used its own binding code, so I didn't have to change it.)So, did I format the address or port wrong when I was doing it manually?;  could be the local address you supply (192.168.1.102) is actually incorrect. the default local address would be 127.0.0.1 (loopback address). I don't know if the port you need to specify is in network byte order, or system byte order. Check the result of Bind(). You should be able to bind to a specific port though (9000).   ;  Also, if you run the client and server on the same machine, you can't bind both to the same port. In general, you don't explcitcly bind a client-side socket at all, but instead let the implementation do that when you first call sendto() (for UDP) or connect() (for TCP).