socket code in Windows Forms c++

  • Thread starter Thread starter Biscuiz
  • Start date Start date
B

Biscuiz

Guest
I am trying to implement a socket in my program to connect to a third party program. I am using Visual Studio C++ and my program has a simple UI (WIndows Forms). The idea is that when the user clicks on the button the connection between the 2 programs starts (I need to mention here that when I implemented this connection in native C++ uwing winsock, without the UI, it all worked fine):

connection class:


#pragma once

using namespace System::Net::Sockets;
using namespace System;

ref class PanguConnect {
private:
TcpClient ^mTcpClient;
Socket ^s;


public:
PanguConnect(void){};
void Connection(System::String^ server, Int32 port)
{
mTcpClient = gcnew TcpClient(server, port);
s = mTcpClient->Client;
}
void CloseConnection ()
{
mTcpClient->Close();
}
};

User interface section:


#pragma once
#include "PanguConnect.h"

namespace client {

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::IO;

using namespace System::Net::Sockets;

/// <summary>
/// Summary for UserInterface1
/// </summary>
public ref class UserInterface1 : public System::Windows::Forms::Form
{
public:
UserInterface1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}

protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~UserInterface1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::PictureBox^ pictureBox1;
private: System::Windows::Forms::Button^ button1;
protected:

private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->pictureBox1 = (gcnew System::Windows::Forms::PictureBox());
this->button1 = (gcnew System::Windows::Forms::Button());
(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->pictureBox1))->BeginInit();
this->SuspendLayout();
//
// pictureBox1
//
this->pictureBox1->BackColor = System::Drawing::SystemColors::ButtonFace;
this->pictureBox1->BorderStyle = System::Windows::Forms::BorderStyle::Fixed3D;
this->pictureBox1->Location = System::Drawing::Point(52, 55);
this->pictureBox1->Name = L"pictureBox1";
this->pictureBox1->Size = System::Drawing::Size(555, 476);
this->pictureBox1->TabIndex = 0;
this->pictureBox1->TabStop = false;
this->pictureBox1->Click += gcnew System::EventHandler(this, &UserInterface1::pictureBox1_Click);
//
// button1
//
this->button1->Location = System::Drawing::Point(657, 55);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(104, 61);
this->button1->TabIndex = 1;
this->button1->Text = L"button1";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &UserInterface1::button1_Click);
//
// UserInterface1
//
this->AutoScaleDimensions = System::Drawing::SizeF(8, 16);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->BackColor = System::Drawing::SystemColors::ActiveCaptionText;
this->ClientSize = System::Drawing::Size(802, 596);
this->Controls->Add(this->button1);
this->Controls->Add(this->pictureBox1);
this->Name = L"UserInterface1";
this->Text = L"UserInterface1";
(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->pictureBox1))->EndInit();
this->ResumeLayout(false);

}
#pragma endregion

private: System::Void pictureBox1_Click(System::Object^ sender, System::EventArgs^ e) {

}

private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
PanguConnect ^PC;

PC = gcnew PanguConnect();


try{
PC->Connection("localhost",10363);
}

catch(Exception ^e)
{
MessageBox::Show(e->Message);
}


try
{
PC->CloseConnection ();
}

catch(Exception ^e)
{
MessageBox::Show(e->Message);
}
}
};
}




When i run the program the third party application (which runs in server mode) crashes and I get the following error message:

No connection made because the target machine actively refused it: 127.0.0.1:10363.


Any idea how I can fix this? THanks in advance for any help/suggestion/comment

Continue reading...
 
Back
Top