Click here to Skip to main content
15,905,563 members
Home / Discussions / Managed C++/CLI
   

Managed C++/CLI

 
AnswerRe: Between Two forms Pin
rikterveen12-Sep-10 1:39
rikterveen12-Sep-10 1:39 
AnswerRe: Between Two forms Pin
N a v a n e e t h12-Sep-10 19:17
N a v a n e e t h12-Sep-10 19:17 
QuestionResizing the windows media player activex problem Pin
raj157631-Aug-10 2:00
raj157631-Aug-10 2:00 
QuestionHow to instantiate winforms in MFC dynamically, [CWinFormsControl, CWinFormsView etc are template classes which needs compile time declarations for managed controls to be instantiated] Pin
Member 443395430-Aug-10 22:22
Member 443395430-Aug-10 22:22 
AnswerRe: How to instantiate winforms in MFC dynamically, [CWinFormsControl, CWinFormsView etc are template classes which needs compile time declarations for managed controls to be instantiated] Pin
Rolf Kristensen20-Sep-10 10:06
Rolf Kristensen20-Sep-10 10:06 
QuestionRunModalLoop thread Pin
spidolino30-Aug-10 20:59
spidolino30-Aug-10 20:59 
AnswerRe: RunModalLoop thread Pin
Luc Pattyn31-Aug-10 2:28
sitebuilderLuc Pattyn31-Aug-10 2:28 
QuestionAsynchrone Telnet Client does receive but will not send [modified] Pin
rikterveen29-Aug-10 3:33
rikterveen29-Aug-10 3:33 
Hey Folks

I'm writing a little telnet client, after a few days
i had it working for receiving, but it refuses to send.
Someone has a idea what is going wrong or what i'm overlooking?

here are 2 send parts one normal and one asynchrone

Normal:
array<Byte>^ SendMessage = Encoding::ASCII->GetBytes("DATA TO SEND");
Sock->Send(SendMessage, SendMessage->Length, SocketFlags::None);


Asynchrone:
public: void SendMessage(String^ UserMessage)
{
	/* Check if there is a connection */
	if (Sock == null || Sock->Connected == false)
	{
		text_output->Text += "\nERROR: First connect before sending data.\n";
		return;
	}

	try
	{
		/* Encode the user message and send it */
		array<Byte>^ SendMessage = Encoding::ASCII->GetBytes("DATA TO SEND");

		Sock->BeginSend(SendMessage, 0, SendMessage->Length, SocketFlags::None, gcnew AsyncCallback(this, namesp_class::SendData), Sock);
	}

	catch (...)
	{
			text_output->Text += "ERROR: Could not send message.\n";
	}
}

public: void SendData (IAsyncResult^ ar)
		{
				/* Exstract the receiving socket */
				Socket^ s = (Socket^)ar->AsyncState;

				int send = s->EndSend(ar);
		}


If anyone is interested here's the rest


/* Global Variables */
Socket^ Sock;
array<Byte>^ InputBuffer;

public: void Telnet_Load()
{
	Delegate_AddMessage = gcnew AddMessage (this, namesp_class::OutputMessage);
	this->output_textbox->TextChanged += gcnew System::EventHandler(this, namesp_class::output_textbox_TextChanged);
}


public: void Read(String^ Host, int Port)
{
	/* Set the sandglass as cursor */
	Cursor = Cursors::WaitCursor;

	try
	{
		/* When the socket is open */
		if (Sock != null && Sock->Connected == true)
		{
			/* Shutdown the socket for send and receive, wait and close the socket. */
			Sock->Shutdown(SocketShutdown::Both);
			sleep(10);
			Sock->Close();
		}

		/* open a new TCP socket */
		Sock = gcnew Socket(AddressFamily::InterNetwork, SocketType::Stream, ProtocolType::Tcp);

		/* Set an IP point with the user given parameters */
		IPEndPoint^ ip = gcnew IPEndPoint (IPAddress::Parse(Host), Port);

		/* Socket blocking is false for asynchrone data connection. */
		Sock->Blocking = false;

		/* Declare a new asynchone callback */
		AsyncCallback^ onconnect = gcnew AsyncCallback(this, namesp_class::OnConnect);

		/* begin a asynchrone connection to the given IP, when connected the next function will be called. */
		Sock->BeginConnect(ip, onconnect, Sock);

	}
	catch (...)
	{
		Invoke (Delegate_AddMessage, "ERROR: Could not connect to remote machine.\n");
	}

	/* Put the cursor back to arrow */
	Cursor = Cursors::Arrow;
}

public: void OnConnect(IAsyncResult^ ar)
{
	/* Get the state of the socket */
	Socket^ s = (Socket^)ar->AsyncState; 

	try
	{
		/* When in the last function the socket has connected succesfully */
		if (s->Connected == true)
		{
			/* Let the user know he is connected */
			Invoke (Delegate_AddMessage, "Connected.\n");

			/* Pass the socket to the next function */
			SetupReceiveCallback(s);
		}
		else
		{
			Invoke (Delegate_AddMessage, "ERROR: Could not connect to remote machine.\n");
		}
	}
	catch (...)
	{
		Invoke (Delegate_AddMessage, "ERROR: Unknow error during connect.\n");
	}
}

public: void SetupReceiveCallback(Socket^ s)
{
	/* Declare the inputbuffer */
	InputBuffer = gcnew array<Byte>(InputBufferSize);
	try
	{
		/* Declare a new asynchone callback  */
		AsyncCallback^ Receive = gcnew AsyncCallback(this, namesp_class::OnReceivedData);

		/* Store the received data in the inputbuffer, When this operation is completed go the the onreceived function */
		s->BeginReceive(InputBuffer, 0, InputBuffer->Length, SocketFlags::None, Receive, s);
	}
	catch(...)
	{
		Invoke (Delegate_AddMessage, "ERROR: Setup receive callback failed.\n");
	}
}

public: void OnReceivedData(IAsyncResult^ ar)
{
	/* Exstract the receiving socket */
	Socket^ s = (Socket^)ar->AsyncState;

	try
	{
		/* Get the number of received data */
		int bytes = s->EndReceive(ar);

		/* When there is still data availible (will be 0 when the connection is closed) */
		if (bytes > 0)
		{
			/* Encode the received message and put it into a string */
			String^ receivedmessage = Encoding::ASCII->GetString(InputBuffer, 0, bytes);
			receivedmessage = "> " + receivedmessage + "\n";

			/* Put the message as objectarray in the delegate. */
			array<Object^>^ stringarray = {receivedmessage};
			Invoke (Delegate_AddMessage, stringarray);

			/* Go back to the receivecallback and receive data again. */
			SetupReceiveCallback(s);
		}
	}
	catch (...)
	{
		Invoke (Delegate_AddMessage, "ERROR: error during data receive.\n");
	}
}

public: void CloseConnection()
{
	/* When there is a connection */
	if (Sock != null && Sock->Connected == true)
	{
		/* Shutdown both the receive and send buffers and close the socket. */
		Sock->Shutdown(SocketShutdown::Both);
		sleep(10);
		Sock->Close();
	}
}

/* Put the message to the right control */
public: void OutputMessage (String^ Message)
{
	/* Get the first character of the string */
	String^ FirstChar = Message[0].ToString();

	/* When it is a received message */
	if (FirstChar == ">")
	{
		output_textbox->SelectionColor = Received_Message_Color;
		output_textbox->SelectedText += Message;
	}
	/* Else on program messages */
	else
	{
		output_textbox->SelectionColor = Program_Message_Color;
		output_textbox->SelectedText += Message;
	}
}

/* Scroll the textbox with the text */
public: void output_textbox_TextChanged(System::Object^  sender, System::EventArgs^  e) 
{
	output_textbox->Focus();
	output_textbox->SelectionLength = 0;
	output_textbox->SelectionStart = output_textbox->TextLength;
	output_textbox->ScrollToCaret();
}


modified on Sunday, August 29, 2010 10:29 AM

Answersize Pin
Luc Pattyn29-Aug-10 4:01
sitebuilderLuc Pattyn29-Aug-10 4:01 
GeneralRe: size - Source code for download, anyone with an idea? [modified] Pin
rikterveen29-Aug-10 4:30
rikterveen29-Aug-10 4:30 
Questionunmanaged calls starting from Internet Explorer always enters Default App domain [modified] Pin
Venkatesh Laguduva27-Aug-10 1:10
Venkatesh Laguduva27-Aug-10 1:10 
Questiondelete dataGridView row [modified] Pin
jashimu23-Aug-10 4:54
jashimu23-Aug-10 4:54 
AnswerRe: delete dataGridView row Pin
Andreoli Carlo25-Aug-10 20:48
professionalAndreoli Carlo25-Aug-10 20:48 
GeneralRe: delete dataGridView row Pin
jashimu26-Aug-10 3:24
jashimu26-Aug-10 3:24 
AnswerRe: delete dataGridView row Pin
Luc Pattyn26-Aug-10 4:27
sitebuilderLuc Pattyn26-Aug-10 4:27 
QuestionProblem with accessing MS Access Date field with C++/CLI Pin
Dirkus Maximus22-Aug-10 20:37
Dirkus Maximus22-Aug-10 20:37 
AnswerRe: Problem with accessing MS Access Date field with C++/CLI Pin
Luc Pattyn23-Aug-10 1:41
sitebuilderLuc Pattyn23-Aug-10 1:41 
GeneralRe: Problem with accessing MS Access Date field with C++/CLI [modified] Pin
Dirkus Maximus23-Aug-10 16:28
Dirkus Maximus23-Aug-10 16:28 
GeneralRe: Problem with accessing MS Access Date field with C++/CLI Pin
Luc Pattyn23-Aug-10 16:56
sitebuilderLuc Pattyn23-Aug-10 16:56 
GeneralRe: Problem with accessing MS Access Date field with C++/CLI [modified] Pin
Dirkus Maximus23-Aug-10 20:09
Dirkus Maximus23-Aug-10 20:09 
AnswerRe: Problem with accessing MS Access Date field with C++/CLI Pin
Luc Pattyn24-Aug-10 0:45
sitebuilderLuc Pattyn24-Aug-10 0:45 
GeneralRe: Problem with accessing MS Access Date field with C++/CLI Pin
Dirkus Maximus24-Aug-10 15:43
Dirkus Maximus24-Aug-10 15:43 
GeneralRe: Problem with accessing MS Access Date field with C++/CLI Pin
Luc Pattyn24-Aug-10 16:31
sitebuilderLuc Pattyn24-Aug-10 16:31 
AnswerRe: Problem with accessing MS Access Date field with C++/CLI Pin
Luc Pattyn24-Aug-10 17:03
sitebuilderLuc Pattyn24-Aug-10 17:03 
GeneralRe: Problem with accessing MS Access Date field with C++/CLI Pin
Dirkus Maximus25-Aug-10 15:52
Dirkus Maximus25-Aug-10 15:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.