axWinsock->GetData method Exception

mkomsa

New member
Joined
Jan 28, 2005
Messages
1
Location
Sunspot, New Mexico
Hi All,

This is my first post. My code is generating an exception when the GetData method executes in my DataArrival event handler. I get the following exception message: Exception from HRESULT:0x800A9C52. My code is listed below. I suspect one of my parameters is incorrect. Help please!

private: System::Void axWinsockCmd_DataArrival(...)
{
Object* objBuffer = new Object;
Object** refBuffer = &objBuffer;
int maxLen;

maxLen = axWinsockCmd->BytesReceived ;
try
{
axWinsockCmd->GetData(refBuffer, DataFormats::Text, __box(maxLen));
}
catch(Exception* pe)
{
MessageBox::Show (pe->Message, S"An Exception Error occurred" );
}
txtIncomingCmdData->Text = dynamic_cast<String*>(*refBuffer) ;
}
 
ideally you shouldnt be using Winsock in .NET ( whether it be vb.net , c# or c++ ) , take a look at the System.Net.Socket Class. heres a quick example of connecting to an address , sending a GET / request and handling the Receiving of the data .
C#:
	// reference these near the top of your forms code window ...
	using namespace System::Net;
	using namespace System::Net::Sockets;

        private: Socket * socket; // this will do the Sending and Receiving
	private: Byte buffer[]; // this will Receive any Data ( hold the data )

	private: void button1_Click(System::Object *  sender, System::EventArgs *  e)
			 {
				 IPHostEntry *  host = Dns::Resolve("msn.com");
				 IPAddress * address = host->AddressList[0];
				 IPEndPoint * point = new IPEndPoint(address , 80);
				 socket = new Socket(AddressFamily::InterNetwork , SocketType::Stream , ProtocolType::Tcp);
				 socket->BeginConnect(point, new AsyncCallback(this,Connected), socket);
			 }

	private: void Connected(IAsyncResult * ar)
			 {
				 if(ar->IsCompleted)
				 {
					 String * str = S"GET /\r\n"; // lets send a GET / message to retrieve the content of msn.coms address.
					 Byte  data[] = System::Text::Encoding::ASCII->GetBytes(str); // first we must convert it to a byte array.
					 socket->Send(data,data->Length,SocketFlags::None);
					 buffer = new Byte[(int)SocketFlags::Partial];
					 socket->BeginReceive(buffer,0,buffer->Length,SocketFlags::Partial, new AsyncCallback(this,Receive), socket);
				 }
			 }

	private: void Receive(IAsyncResult * ar)
			 {
				 int x = socket->EndReceive(ar);
				 if(x > 0)
				 {
					richTextBox1->AppendText(System::Text::Encoding::ASCII->GetString(buffer));
				 	socket->BeginReceive(buffer,0,buffer->Length,SocketFlags::Partial, new AsyncCallback(this,Receive), socket);
				 }
			 }
for more info on the Socket Class check this msdn article ... Link to Socket Class article
hope it helps you on your way :cool:
 
Back
Top