Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Copyright (c) 2012 [iPs]TeaM
- * Bruno da Silva (email@brunodasilva.com)
- * Enviar sockets em c++
- *
- * www.brunodasilva.com
- * www.ips-team.forumeiros.com
- */
- #pragma once
- #ifndef UNICODE
- #define UNICODE
- #endif
- #define WIN32_LEAN_AND_MEAN
- #define _WIN32_DCOM
- #include <windows.h>
- #include <winsock2.h>
- #include <ws2tcpip.h>
- #include <stdio.h>
- #include <iostream>
- #include <string>
- using namespace System;
- using namespace System::Runtime::InteropServices;
- // link para biblioteca
- #pragma comment(lib, "ws2_32.lib")
- #pragma comment(lib, "User32.lib")
- namespace CPerguntaseRespostas {
- using namespace System;
- using namespace System::ComponentModel;
- using namespace System::Collections;
- using namespace System::Windows::Forms;
- using namespace System::Data;
- using namespace System::Drawing;
- public ref class Form1 : public System::Windows::Forms::Form
- {
- public:
- Form1(void)
- {
- InitializeComponent();
- }
- protected:
- ~Form1()
- {
- if (components)
- {
- delete components;
- }
- }
- private: System::Windows::Forms::TextBox^ textBox1;
- protected:
- private: System::Windows::Forms::Button^ button1;
- private: System::Windows::Forms::Label^ label1;
- private: System::Windows::Forms::ProgressBar^ progressBar1;
- private: System::Windows::Forms::TextBox^ textBox2;
- private: System::Windows::Forms::TextBox^ textBox3;
- private: System::Windows::Forms::Label^ label2;
- private: System::Windows::Forms::Label^ label3;
- private: System::Windows::Forms::Label^ label4;
- private:
- System::ComponentModel::Container ^components;
- #pragma region Windows Form Designer generated code
- void InitializeComponent(void)
- {
- this->textBox1 = (gcnew System::Windows::Forms::TextBox());
- this->button1 = (gcnew System::Windows::Forms::Button());
- this->label1 = (gcnew System::Windows::Forms::Label());
- this->progressBar1 = (gcnew System::Windows::Forms::ProgressBar());
- this->textBox2 = (gcnew System::Windows::Forms::TextBox());
- this->textBox3 = (gcnew System::Windows::Forms::TextBox());
- this->label2 = (gcnew System::Windows::Forms::Label());
- this->label3 = (gcnew System::Windows::Forms::Label());
- this->label4 = (gcnew System::Windows::Forms::Label());
- this->SuspendLayout();
- //
- // textBox1
- //
- this->textBox1->Location = System::Drawing::Point(15, 67);
- this->textBox1->Name = L"textBox1";
- this->textBox1->Size = System::Drawing::Size(219, 20);
- this->textBox1->TabIndex = 0;
- this->textBox1->Text = L"endereco:porta";
- //
- // button1
- //
- this->button1->Font = (gcnew System::Drawing::Font(L"Trebuchet MS", 9.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
- static_cast<System::Byte>(0)));
- this->button1->Location = System::Drawing::Point(12, 177);
- this->button1->Name = L"button1";
- this->button1->Size = System::Drawing::Size(222, 53);
- this->button1->TabIndex = 1;
- this->button1->Text = L"Desafiar";
- this->button1->UseVisualStyleBackColor = true;
- this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
- //
- // label1
- //
- this->label1->AutoSize = true;
- this->label1->Font = (gcnew System::Drawing::Font(L"Trebuchet MS", 9, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
- static_cast<System::Byte>(0)));
- this->label1->Location = System::Drawing::Point(12, 46);
- this->label1->Name = L"label1";
- this->label1->Size = System::Drawing::Size(75, 18);
- this->label1->TabIndex = 2;
- this->label1->Text = L"Endereço IP";
- //
- // progressBar1
- //
- this->progressBar1->Location = System::Drawing::Point(12, 236);
- this->progressBar1->Name = L"progressBar1";
- this->progressBar1->Size = System::Drawing::Size(467, 17);
- this->progressBar1->TabIndex = 3;
- this->progressBar1->Visible = false;
- //
- // textBox2
- //
- this->textBox2->Location = System::Drawing::Point(12, 124);
- this->textBox2->Multiline = true;
- this->textBox2->Name = L"textBox2";
- this->textBox2->Size = System::Drawing::Size(222, 47);
- this->textBox2->TabIndex = 4;
- //
- // textBox3
- //
- this->textBox3->Location = System::Drawing::Point(246, 67);
- this->textBox3->Multiline = true;
- this->textBox3->Name = L"textBox3";
- this->textBox3->ReadOnly = true;
- this->textBox3->Size = System::Drawing::Size(233, 163);
- this->textBox3->TabIndex = 5;
- this->textBox3->Text = L"Waiting for data ..";
- //
- // label2
- //
- this->label2->AutoSize = true;
- this->label2->Font = (gcnew System::Drawing::Font(L"Trebuchet MS", 9, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
- static_cast<System::Byte>(0)));
- this->label2->Location = System::Drawing::Point(243, 46);
- this->label2->Name = L"label2";
- this->label2->Size = System::Drawing::Size(102, 18);
- this->label2->TabIndex = 6;
- this->label2->Text = L"Dados Recebidos";
- //
- // label3
- //
- this->label3->AutoSize = true;
- this->label3->Font = (gcnew System::Drawing::Font(L"Trebuchet MS", 9, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
- static_cast<System::Byte>(0)));
- this->label3->Location = System::Drawing::Point(12, 103);
- this->label3->Name = L"label3";
- this->label3->Size = System::Drawing::Size(103, 18);
- this->label3->TabIndex = 7;
- this->label3->Text = L"Dado para Enviar";
- //
- // label4
- //
- this->label4->AutoSize = true;
- this->label4->Font = (gcnew System::Drawing::Font(L"Trebuchet MS", 14.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
- static_cast<System::Byte>(0)));
- this->label4->ForeColor = System::Drawing::Color::Teal;
- this->label4->Location = System::Drawing::Point(179, 9);
- this->label4->Name = L"label4";
- this->label4->Size = System::Drawing::Size(122, 24);
- this->label4->TabIndex = 8;
- this->label4->Text = L"Sockets Send";
- //
- // Form1
- //
- this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
- this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
- this->ClientSize = System::Drawing::Size(490, 261);
- this->Controls->Add(this->label4);
- this->Controls->Add(this->label3);
- this->Controls->Add(this->label2);
- this->Controls->Add(this->textBox3);
- this->Controls->Add(this->textBox2);
- this->Controls->Add(this->progressBar1);
- this->Controls->Add(this->label1);
- this->Controls->Add(this->button1);
- this->Controls->Add(this->textBox1);
- this->Name = L"Form1";
- this->Text = L"SocketsSend - Ferramenta para Envio de Sockets";
- this->ResumeLayout(false);
- this->PerformLayout();
- }
- #pragma endregion
- private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
- this->progressBar1->Visible = true;
- while(this->progressBar1->Value != 100) {
- this->progressBar1->Value = 1+ (this->progressBar1->Value);
- }
- SOCKET conexao ;
- conexao = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
- sockaddr_in clientService;
- clientService.sin_family = AF_INET;
- char result[2][20];
- int wasSplited = 0;
- char * str = (char *)(void*)Marshal::StringToHGlobalAnsi(this->textBox1->Text);
- for(int i = 0, j = strlen(str); i != j; i ++) {
- if(str[i] == ':') {
- wasSplited = i +1;
- result[0][wasSplited-1] = 0;
- continue;
- }
- if(!wasSplited) {
- result[0][i] = str[i];
- }
- else {
- result[1][i - wasSplited] = str[i];
- }
- }
- clientService.sin_addr.s_addr = inet_addr (result[0]);
- clientService.sin_port = htons (atoi(result[1]));
- connect(conexao, (SOCKADDR *) & clientService, sizeof (clientService));
- int sent_bytes = send(conexao, (char*)(void*)Marshal::StringToHGlobalAnsi(this->textBox2->Text) , 10, 0);
- if (sent_bytes == SOCKET_ERROR) {
- MessageBox::Show(" Houve um erro na conexão com outro computador \n Verifique se o IP está correto !!","[ERRO INTERNO]", MessageBoxButtons::OK);
- closesocket(conexao);
- }
- char bufferStr [050];
- char ppReceive [512];
- sprintf(bufferStr, "Endereco: %s - Porta: %d \r\n", result[0], atoi(result[1]));
- sprintf(bufferStr, "%sBytes Sent: %d\r\n", bufferStr, sent_bytes);
- while(sent_bytes) {
- sent_bytes = recv(conexao, ppReceive, 512, 0);
- if (sent_bytes > 0) {
- sprintf(bufferStr, "%sBytes received: %d\r\n", bufferStr, sent_bytes);
- }
- else if (sent_bytes == -1) {
- break;
- }
- }
- sprintf(bufferStr, "%sConection Closed", bufferStr);
- this->textBox3->Text = gcnew String(bufferStr);
- MessageBox::Show("Conexão efetuado com sucesso !! \n Aguardando resposta do servidor . . .", "[SUCESSO]", MessageBoxButtons::OK);
- closesocket(conexao);
- this->progressBar1->Value = 0;
- }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement