Advertisement
ipsBruno

(C++) Send Sockets

Jul 26th, 2012
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.86 KB | None | 0 0
  1. /*
  2.  *  Copyright (c) 2012 [iPs]TeaM
  3.  *  Bruno da Silva (email@brunodasilva.com)
  4.  *  Enviar sockets em c++
  5.  *
  6.  * www.brunodasilva.com
  7.  * www.ips-team.forumeiros.com
  8. */
  9.  
  10. #pragma once
  11.  
  12. #ifndef UNICODE
  13. #define UNICODE
  14. #endif
  15.  
  16. #define WIN32_LEAN_AND_MEAN
  17.  
  18. #define _WIN32_DCOM
  19.  
  20.  
  21. #include <windows.h>
  22. #include <winsock2.h>
  23. #include <ws2tcpip.h>
  24. #include <stdio.h>
  25. #include <iostream>
  26. #include <string>
  27.  
  28. using namespace System;
  29. using namespace System::Runtime::InteropServices;
  30.  
  31. // link para biblioteca
  32. #pragma comment(lib, "ws2_32.lib")
  33. #pragma comment(lib, "User32.lib")
  34.  
  35.  
  36. namespace CPerguntaseRespostas {
  37.  
  38.     using namespace System;
  39.     using namespace System::ComponentModel;
  40.     using namespace System::Collections;
  41.     using namespace System::Windows::Forms;
  42.     using namespace System::Data;
  43.     using namespace System::Drawing;
  44.  
  45.     public ref class Form1 : public System::Windows::Forms::Form
  46.     {
  47.     public:
  48.         Form1(void)
  49.         {
  50.             InitializeComponent();
  51.         }
  52.  
  53.     protected:
  54.         ~Form1()
  55.         {
  56.             if (components)
  57.             {
  58.                 delete components;
  59.             }
  60.         }
  61.     private: System::Windows::Forms::TextBox^  textBox1;
  62.     protected:
  63.     private: System::Windows::Forms::Button^  button1;
  64.     private: System::Windows::Forms::Label^  label1;
  65.     private: System::Windows::Forms::ProgressBar^  progressBar1;
  66.     private: System::Windows::Forms::TextBox^  textBox2;
  67.     private: System::Windows::Forms::TextBox^  textBox3;
  68.     private: System::Windows::Forms::Label^  label2;
  69.     private: System::Windows::Forms::Label^  label3;
  70.     private: System::Windows::Forms::Label^  label4;
  71.  
  72.     private:
  73.         System::ComponentModel::Container ^components;
  74.  
  75. #pragma region Windows Form Designer generated code
  76.         void InitializeComponent(void)
  77.         {
  78.             this->textBox1 = (gcnew System::Windows::Forms::TextBox());
  79.             this->button1 = (gcnew System::Windows::Forms::Button());
  80.             this->label1 = (gcnew System::Windows::Forms::Label());
  81.             this->progressBar1 = (gcnew System::Windows::Forms::ProgressBar());
  82.             this->textBox2 = (gcnew System::Windows::Forms::TextBox());
  83.             this->textBox3 = (gcnew System::Windows::Forms::TextBox());
  84.             this->label2 = (gcnew System::Windows::Forms::Label());
  85.             this->label3 = (gcnew System::Windows::Forms::Label());
  86.             this->label4 = (gcnew System::Windows::Forms::Label());
  87.             this->SuspendLayout();
  88.             //
  89.             // textBox1
  90.             //
  91.             this->textBox1->Location = System::Drawing::Point(15, 67);
  92.             this->textBox1->Name = L"textBox1";
  93.             this->textBox1->Size = System::Drawing::Size(219, 20);
  94.             this->textBox1->TabIndex = 0;
  95.             this->textBox1->Text = L"endereco:porta";
  96.             //
  97.             // button1
  98.             //
  99.             this->button1->Font = (gcnew System::Drawing::Font(L"Trebuchet MS", 9.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
  100.                 static_cast<System::Byte>(0)));
  101.             this->button1->Location = System::Drawing::Point(12, 177);
  102.             this->button1->Name = L"button1";
  103.             this->button1->Size = System::Drawing::Size(222, 53);
  104.             this->button1->TabIndex = 1;
  105.             this->button1->Text = L"Desafiar";
  106.             this->button1->UseVisualStyleBackColor = true;
  107.             this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
  108.             //
  109.             // label1
  110.             //
  111.             this->label1->AutoSize = true;
  112.             this->label1->Font = (gcnew System::Drawing::Font(L"Trebuchet MS", 9, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
  113.                 static_cast<System::Byte>(0)));
  114.             this->label1->Location = System::Drawing::Point(12, 46);
  115.             this->label1->Name = L"label1";
  116.             this->label1->Size = System::Drawing::Size(75, 18);
  117.             this->label1->TabIndex = 2;
  118.             this->label1->Text = L"Endereço IP";
  119.             //
  120.             // progressBar1
  121.             //
  122.             this->progressBar1->Location = System::Drawing::Point(12, 236);
  123.             this->progressBar1->Name = L"progressBar1";
  124.             this->progressBar1->Size = System::Drawing::Size(467, 17);
  125.             this->progressBar1->TabIndex = 3;
  126.             this->progressBar1->Visible = false;
  127.             //
  128.             // textBox2
  129.             //
  130.             this->textBox2->Location = System::Drawing::Point(12, 124);
  131.             this->textBox2->Multiline = true;
  132.             this->textBox2->Name = L"textBox2";
  133.             this->textBox2->Size = System::Drawing::Size(222, 47);
  134.             this->textBox2->TabIndex = 4;
  135.             //
  136.             // textBox3
  137.             //
  138.             this->textBox3->Location = System::Drawing::Point(246, 67);
  139.             this->textBox3->Multiline = true;
  140.             this->textBox3->Name = L"textBox3";
  141.             this->textBox3->ReadOnly = true;
  142.             this->textBox3->Size = System::Drawing::Size(233, 163);
  143.             this->textBox3->TabIndex = 5;
  144.             this->textBox3->Text = L"Waiting for data ..";
  145.             //
  146.             // label2
  147.             //
  148.             this->label2->AutoSize = true;
  149.             this->label2->Font = (gcnew System::Drawing::Font(L"Trebuchet MS", 9, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
  150.                 static_cast<System::Byte>(0)));
  151.             this->label2->Location = System::Drawing::Point(243, 46);
  152.             this->label2->Name = L"label2";
  153.             this->label2->Size = System::Drawing::Size(102, 18);
  154.             this->label2->TabIndex = 6;
  155.             this->label2->Text = L"Dados Recebidos";
  156.             //
  157.             // label3
  158.             //
  159.             this->label3->AutoSize = true;
  160.             this->label3->Font = (gcnew System::Drawing::Font(L"Trebuchet MS", 9, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
  161.                 static_cast<System::Byte>(0)));
  162.             this->label3->Location = System::Drawing::Point(12, 103);
  163.             this->label3->Name = L"label3";
  164.             this->label3->Size = System::Drawing::Size(103, 18);
  165.             this->label3->TabIndex = 7;
  166.             this->label3->Text = L"Dado para Enviar";
  167.             //
  168.             // label4
  169.             //
  170.             this->label4->AutoSize = true;
  171.             this->label4->Font = (gcnew System::Drawing::Font(L"Trebuchet MS", 14.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
  172.                 static_cast<System::Byte>(0)));
  173.             this->label4->ForeColor = System::Drawing::Color::Teal;
  174.             this->label4->Location = System::Drawing::Point(179, 9);
  175.             this->label4->Name = L"label4";
  176.             this->label4->Size = System::Drawing::Size(122, 24);
  177.             this->label4->TabIndex = 8;
  178.             this->label4->Text = L"Sockets Send";
  179.             //
  180.             // Form1
  181.             //
  182.             this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
  183.             this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
  184.             this->ClientSize = System::Drawing::Size(490, 261);
  185.             this->Controls->Add(this->label4);
  186.             this->Controls->Add(this->label3);
  187.             this->Controls->Add(this->label2);
  188.             this->Controls->Add(this->textBox3);
  189.             this->Controls->Add(this->textBox2);
  190.             this->Controls->Add(this->progressBar1);
  191.             this->Controls->Add(this->label1);
  192.             this->Controls->Add(this->button1);
  193.             this->Controls->Add(this->textBox1);
  194.             this->Name = L"Form1";
  195.             this->Text = L"SocketsSend - Ferramenta para Envio de Sockets";
  196.             this->ResumeLayout(false);
  197.             this->PerformLayout();
  198.  
  199.         }
  200. #pragma endregion
  201.     private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
  202.  
  203.                 this->progressBar1->Visible = true;
  204.  
  205.                 while(this->progressBar1->Value != 100) {
  206.                     this->progressBar1->Value = 1+ (this->progressBar1->Value);
  207.                 }
  208.  
  209.                 SOCKET conexao ;
  210.  
  211.                 conexao = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  212.  
  213.                 sockaddr_in clientService;
  214.  
  215.                 clientService.sin_family        = AF_INET;
  216.  
  217.  
  218.                 char result[2][20];
  219.                 int wasSplited = 0;
  220.                 char * str = (char *)(void*)Marshal::StringToHGlobalAnsi(this->textBox1->Text);
  221.  
  222.                 for(int i = 0, j = strlen(str); i != j; i ++) {
  223.  
  224.                     if(str[i] == ':') {
  225.                         wasSplited = i +1;
  226.                         result[0][wasSplited-1] = 0;
  227.                         continue;
  228.                     }
  229.  
  230.                     if(!wasSplited) {
  231.                         result[0][i] = str[i];
  232.                     }
  233.                     else {
  234.                         result[1][i - wasSplited] = str[i];
  235.                     }
  236.                 }
  237.  
  238.  
  239.                 clientService.sin_addr.s_addr   = inet_addr (result[0]);
  240.                 clientService.sin_port          = htons     (atoi(result[1]));
  241.  
  242.                 connect(conexao, (SOCKADDR *) & clientService, sizeof (clientService));
  243.  
  244.                 int sent_bytes = send(conexao, (char*)(void*)Marshal::StringToHGlobalAnsi(this->textBox2->Text) , 10, 0);
  245.  
  246.                 if (sent_bytes == SOCKET_ERROR) {
  247.    
  248.                     MessageBox::Show(" Houve um erro na conexão com outro computador \n Verifique se o IP está correto !!","[ERRO INTERNO]", MessageBoxButtons::OK);
  249.                     closesocket(conexao);
  250.                 }
  251.  
  252.                 char bufferStr  [050];
  253.                 char ppReceive  [512];
  254.  
  255.                 sprintf(bufferStr, "Endereco: %s - Porta: %d \r\n", result[0], atoi(result[1]));
  256.                 sprintf(bufferStr, "%sBytes Sent: %d\r\n", bufferStr, sent_bytes);                             
  257.  
  258.                 while(sent_bytes) {
  259.  
  260.                     sent_bytes = recv(conexao, ppReceive, 512, 0);
  261.  
  262.                     if (sent_bytes > 0) {
  263.                         sprintf(bufferStr, "%sBytes received: %d\r\n", bufferStr, sent_bytes);
  264.                     }
  265.                     else if (sent_bytes == -1) {
  266.                         break;
  267.                     }
  268.                 }
  269.  
  270.                 sprintf(bufferStr, "%sConection Closed", bufferStr);
  271.  
  272.                 this->textBox3->Text = gcnew String(bufferStr);
  273.  
  274.                 MessageBox::Show("Conexão efetuado com sucesso !! \n Aguardando resposta do servidor . . .", "[SUCESSO]", MessageBoxButtons::OK);
  275.                 closesocket(conexao);
  276.  
  277.                 this->progressBar1->Value = 0;
  278.             }
  279.     };
  280. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement