antonio

antonio


  • Name: [not set]
  • Favorite Languages: [not set]
  • Website: [not set]
  • Location: [not set]
  • About Me: [not set]

Recent Comments

  • C# Tutorial - Simple Threaded TCP Server
    10/18/2011 - 15:08

    Hello

    I'm new in C++ and I'm working on the client code but I always get the same
    error C3364: 'System::Threading::ParameterizedThreadStart' : invalid argument for delegate constructor; delegate target needs to be a pointer to a member function

    I also tried with ThreadPool::QueueUserWorkItem(delegate Read_Multipl_Reg(direccion, unidad, referencia,cantidad,registros));

    but it doesn't work either

    can someone help me to see how to solve it

    thanks

    #pragma once
    #include "stdafx.h"
    #include <winsock2.h>
    #include <stdio.h>
    #include <conio.h>
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <ios>
    using std::cout;
    using std::cin;
    using namespace System;
    using namespace System::Text;
    using namespace System::Net;
    using namespace System::Net::Sockets;
    using namespace System::Threading;


    public ref class cliente
    {
            TcpClient^ ClienteMBTCP;
            Thread^ hilocliente;
            int cantidad;
            int unidad;
            int referencia;
            String^ direccion;
            array<byte>^ registros;
    public:
            cliente(){
                            }
    public:
           
            void Connect(String^ address, int port)
       {
          ClienteMBTCP = gcnew TcpClient();
          ClienteMBTCP->Connect(address, port);
          // Create a thread to read data sent from the server.
          //ThreadPool::QueueUserWorkItem(delegate Read_Multipl_Reg(direccion, unidad, referencia,cantidad,registros));
              cliente^ mb=gcnew cliente();
              hilocliente = gcnew Thread(gcnew ParameterizedThreadStart(mb,cliente::Read_Multipl_Reg(direccion, unidad, referencia,cantidad,registros) ) ); //error line
              hilocliente->Start(ClienteMBTCP);
            }
           
            void Send(array<byte>^ buffer)
       {
          ClienteMBTCP->GetStream()->Write(buffer, 0, buffer->Length);
          ClienteMBTCP->GetStream()->Flush();
       }
           
            void Read_Multipl_Reg (String^ dns,  int unidad,int referencia, int cantidad, array<byte>^ registros )
            {
             int i;
             array<byte>^ buffer = gcnew array<byte>(256);
           
                     for ( i=0; i<5; i++ )
                             buffer[ i ] = 0;
                     buffer[ 6 ] = (byte) unidad;
                     buffer[ 7 ] = 3;
                     buffer[ 8 ] = (byte) (referencia >> 8);
                     buffer[ 9 ] = (byte) (referencia & 0xFF);
                     buffer[ 10 ] = 0;
                     buffer[ 11 ] = (byte) cantidad;
                     buffer[ 5 ] = 6;
                     for(int e=0;e<buffer->Length;e++)
                                     cout<<buffer[e]<<"es el valor";

    //Enviar la solicitud al servidor
              Send(buffer);
    // Esperar y leer la respuesta
            Read();
            }


    void cliente:: Read()
       {  
          array<byte>^ buffer = gcnew array<byte>(256);
          int bytesRecibidos = 0;
          while (true)
          {
             try{
                      bytesRecibidos = ClienteMBTCP->GetStream()->Read(buffer, 0, 256);
                     }catch (Exception^ e)
                    {
                        // error happened
                        break;
                    }
                     // Verificar la respuesta y extraer los valores leĆ­dos
                     if ( bytesRecibidos == ( 9+2*cantidad ) && buffer[ 7 ] == 3 )
                     {
                             array<byte>^ registros = gcnew array<byte>(256);
                             for ( int i = 0; i<cantidad; i++ )
                             {
                            // Construir el valor de registro de los bytes alto y bajo
                                     registros[ i ] = ( ( (byte) buffer[ 10+2*i ] ) << 8 ) & 0xFF00 | ( (byte) buffer[ 11+2*i] & 0xFF );
                                     cout << "El valor del registros[i]= "<< registros[i];
                             }
                     }
                     else
                            cout<< "respuesta erronea";
                                   
       }
       }

    };