Jump to content
  • Advertisement
Sign in to follow this  
Amariah

[.net] Managed C++ Packet Problems

This topic is 4166 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am new to Managed C++ code and I am trying to create a packet class to have a socket and buffer array. If I make the class unmanaged I get this error: error C2365: cannot declare a managed 'thisSocket' in an unmanaged 'CSocketPacket'. If I make the class managed by adding 'public ref class' The program gets like 43 errors instead of just these. My code is below. Please help me. Thanks. #include "stdafx.h" #include "Server.h" using namespace System; using namespace System::Text; using namespace System::IO; using namespace System::Net; using namespace System::Net::Sockets; using namespace System::Collections; const int portNumber = 1024; public class CSocketPacket { System::Net::Sockets::Socket thisSocket; array<Byte>^dataBuffer = gcnew array<Byte>(1024); }; // Constructor Server::Server() { } // Destructor Server::~Server() { } void Server::initServer() { socketListener = gcnew Socket( AddressFamily::InterNetwork, SocketType::Stream, ProtocolType::Tcp ); IPEndPoint ipLocal = gcnew IPEndPoint ( IPAddress::Any, portNumber ); socketListener.Bind( ipLocal ); socketListener.Listen(1); socketListener.BeginAccept( new AsyncCallback(connectCallbackFunction), NULL ); } void Server::connectCallbackFunction(System::IAsyncResult result) { socketWorker = socketListener.EndAccept( result ); WaitForData( socketWorker ); SendData( socketWorker ); } void Server::WaitFordata(System::Net::Sockets::Socket IOsocket) { CSocketPacket packet = new CSocketPacket(); packet.thisSocket = IOsocket; IOsocket.BeginReceive( packet.dataBuffer, 0, packet.dataBuffer->Length, SocketFlags::None, gcnew AsyncCallback(dataReceivedCallbackFunction), packet); } void Server::SendData(System::Net::Sockets::Socket IOsocket) { // convert object to byte array byte[] byteData = System::Text::Encoding::ASCII::get( messageToSend ); // send data IOsocket.Send ( byteData ); } void Server::dataReceivedCallbackFunction(System::IAsyncResult result) { CSocketPacket packet = (CSocketPacket)result->AsyncState; int size = packet.thisSocket->EndReceive( result ); char[] buffer = gcnew char[size + 1]; System::Text::Decoder decoder = System::Text::Encoding::UTF8::get(); int numCharacters = decoder.GetChars( packet.dataBuffer, 0, size, buffer, 0 ); System::String receivedData = gcnew System::String( buffer ); // print to screen }

Share this post


Link to post
Share on other sites
Advertisement
Sound to me like you should ask this in a forum dedicated to managed programming (such as the .NET forum). This is a language problem, not a sockets problem.

It also sounds to me as if you need to include a few of those errors you get (say, the first three) to make it easier for folks to help you.

Share this post


Link to post
Share on other sites
make sure there are no standard windows includes in any of the or it will cause a large number of problems with multiple definitions and missing functions, then try making the class a managed one.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!