How to send an object to a namespace (a graphic interface type class) and set the object to be used by C ++ button actions

0

I am working on a project that has two administrators connected to a server through sockets and threads. Includes graphic interface. The problem arises when I want to send objects of type socketC to the interface so that each interface sends data to socket of the server but I can not find a way to set them in the interface. The administrator code that creates the sockets objects and sends them to the interface

using namespace System;
using namespace System::Windows::Forms;
using namespace std;
using namespace AdministradorS;

struct arg_struct {
    SOCKET socket;
    int puerto;
    string ip;
};


void* crearSocketCliente(void *datosRec) {
    struct arg_struct *datos = (struct arg_struct *)datosRec;
    socketC sCliente1(datos->puerto, datos->ip, datos->socket);
    Application::EnableVisualStyles();
    Application::SetCompatibleTextRenderingDefault(false);
    AdministradorS::loginAdmi loginAdmi_sec(sCliente1);
    loginAdmi_sec.ShowDialog();

    return NULL;
}


void main()
{
string ipAddress = "192.168.0.6";           // IP Address of the server
int port1 = 5400;                       // Listening port # on the server
int port2 = 5500;
SOCKET SocketAdministrador1 = NULL;
SOCKET SocketAdministrador2 = NULL;
struct arg_struct datosAdministrador1;
struct arg_struct datosAdministrador2;
datosAdministrador1.socket = SocketAdministrador1;
datosAdministrador1.puerto = port1;
datosAdministrador1.ip = ipAddress;
datosAdministrador2.socket = SocketAdministrador2;
datosAdministrador2.puerto = port2;
datosAdministrador2.ip = ipAddress;


pthread_t thread1, thread2;
pthread_create(&thread1, NULL, &crearSocketCliente, (void *)&datosAdministrador1);
pthread_create(&thread2, NULL, &crearSocketCliente, (void *)&datosAdministrador2);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);


// Gracefully close down everything
closesocket(SocketAdministrador1);
closesocket(SocketAdministrador2);
WSACleanup();
}

Code of an interface window (the one of logearse the administrator) that receives the object, tries with a variable static but sets a single object therefore the two interfaces created handle the same socket

namespace AdministradorS {
using namespace std;
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Resumen de loginAdmi
/// </summary>
public ref class loginAdmi : public System::Windows::Forms::Form
{



public:

    loginAdmi(socketC sock)
    {
        //definir el objeto de tipo socket pero en donde?
        InitializeComponent(sock);


        //
        //TODO: agregar código de constructor aquí
        //
    }

protected:
    /// <summary>
    /// Limpiar los recursos que se estén usando.
    /// </summary>
    ~loginAdmi()
    {
        if (components)
        {
            delete components;
        }
    }

private: System::Windows::Forms::Label^  label1;
protected:
private: System::Windows::Forms::Button^  btnAceptar;
private: System::Windows::Forms::TextBox^  txtPassw;
private: System::Windows::Forms::TextBox^  txtUsuario;

private:
    /// <summary>
    /// Variable del diseñador necesaria.
    /// </summary>
    System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
    /// <summary>
    /// Método necesario para admitir el Diseñador. No se puede modificar
    /// el contenido de este método con el editor de código.
    /// </summary>
    void InitializeComponent(socketC sock)
    {
        this->label1 = (gcnew System::Windows::Forms::Label());
        this->btnAceptar = (gcnew System::Windows::Forms::Button());
        this->txtPassw = (gcnew System::Windows::Forms::TextBox());
        this->txtUsuario = (gcnew System::Windows::Forms::TextBox());
        this->SuspendLayout();
        // 
        // label1
        // 
        this->label1->AutoSize = true;
        this->label1->Font = (gcnew System::Drawing::Font(L"Lucida Calligraphy", 27.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
            static_cast<System::Byte>(0)));
        this->label1->Location = System::Drawing::Point(309, 164);
        this->label1->Margin = System::Windows::Forms::Padding(4, 0, 4, 0);
        this->label1->Name = L"label1";
        this->label1->Size = System::Drawing::Size(236, 72);
        this->label1->TabIndex = 7;
        this->label1->Text = L"LOGIN";
        // 
        // btnAceptar
        // 
        this->btnAceptar->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 15.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
            static_cast<System::Byte>(0)));
        this->btnAceptar->Location = System::Drawing::Point(269, 542);
        this->btnAceptar->Margin = System::Windows::Forms::Padding(4, 5, 4, 5);
        this->btnAceptar->Name = L"btnAceptar";
        this->btnAceptar->Size = System::Drawing::Size(276, 55);
        this->btnAceptar->TabIndex = 6;
        this->btnAceptar->Text = L"Aceptar";
        this->btnAceptar->UseVisualStyleBackColor = true;
        this->btnAceptar->Click += gcnew System::EventHandler(this, &loginAdmi::btnAceptar_Click);

        // 
        // txtPassw
        // 
        this->txtPassw->Font = (gcnew System::Drawing::Font(L"Times New Roman", 18, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
            static_cast<System::Byte>(0)));
        this->txtPassw->Location = System::Drawing::Point(212, 398);
        this->txtPassw->Margin = System::Windows::Forms::Padding(4, 5, 4, 5);
        this->txtPassw->Name = L"txtPassw";
        this->txtPassw->PasswordChar = '*';
        this->txtPassw->Size = System::Drawing::Size(424, 49);
        this->txtPassw->TabIndex = 5;
        this->txtPassw->Text = L"PASSWORD";
        this->txtPassw->TextAlign = System::Windows::Forms::HorizontalAlignment::Center;
        // 
        // txtUsuario
        // 
        this->txtUsuario->AccessibleDescription = L"USUARIO";
        this->txtUsuario->Font = (gcnew System::Drawing::Font(L"Times New Roman", 18, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
            static_cast<System::Byte>(0)));
        this->txtUsuario->Location = System::Drawing::Point(212, 298);
        this->txtUsuario->Margin = System::Windows::Forms::Padding(4, 5, 4, 5);
        this->txtUsuario->Name = L"txtUsuario";
        this->txtUsuario->Size = System::Drawing::Size(424, 49);
        this->txtUsuario->TabIndex = 4;
        this->txtUsuario->Text = L"USUARIO";
        this->txtUsuario->TextAlign = System::Windows::Forms::HorizontalAlignment::Center;
        // 
        // loginAdmi
        // 
        this->AutoScaleDimensions = System::Drawing::SizeF(9, 20);
        this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
        this->ClientSize = System::Drawing::Size(803, 708);
        this->Controls->Add(this->label1);
        this->Controls->Add(this->btnAceptar);
        this->Controls->Add(this->txtPassw);
        this->Controls->Add(this->txtUsuario);
        this->Margin = System::Windows::Forms::Padding(4, 5, 4, 5);
        this->Name = L"loginAdmi";
        this->Text = L"loginAdmi";
        this->ResumeLayout(false);
        this->PerformLayout();

    }
#pragma endregion
void MarshalString(String ^ s, string& os) {
    using namespace Runtime::InteropServices;
    const char* chars =
        (const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer();
    os = chars;
    Marshal::FreeHGlobal(IntPtr((void*)chars));
}

private: System::Void btnAceptar_Click(System::Object^  sender, System::EventArgs^  e) {
    String ^ user = txtUsuario->Text; //obtiene el dato del textbox
    String ^passw = txtPassw->Text; //obtiene el dato del textbox txtUsuario->Text;
    string userS;
    string passwS;
    MarshalString(user, userS);
    MarshalString(passw, passwS);
    bool logeado = false;
    if(AdministradorS::socket.puertoS ==5400)    //socket deberia ser la variable con el objeto entrante seteado 
        MessageBox::Show("5400");
    else if(AdministradorS::socket.puertoS == 5500)   //socket deberia ser la variable con el objeto entrante seteado 
        MessageBox::Show("5500");




    /*if (passwS == "1") {
        clienteLOG.enviarporSocketsLOG(logeado, userS, passwS);
        if (logeado == true) {
            this->Hide();
            AdministradorMenu::administrador menuP;
            menuP.ShowDialog();
        }
    }*/
}
private: System::Void loginAdmi_Load(System::Object^  sender, System::EventArgs^  e) {
}
};
}

My question in general is how to create a variable in the code of the interface that can set the incoming object and thus be used in the action of the button? (I can not create variables in a normal way since an error occurs that the variable was already created, I guess, because of the threads) Thank you in advance and I hope I have expressed myself well in my question, sorry if the code has a lot of commented section or I am new.

    
asked by Yo soy 13.11.2018 в 09:30
source

0 answers