2012-10-24 4 views
1

우리 팀 동료가 소켓을 통해 나와 연결해야하는 학생 프로젝트가 있습니다. Im은 HTML5 웹 페이지를 실행하고 Socket.IO 서버를 만드는 Im과 별도로 실행합니다. 그는 소켓을 통해 내 웹 페이지로 보내야하는 RFID를 스캔하는 C++ 프로그램을 운영하고 있습니다. 우리는 그의 편에서 연결을 위해 고심하고 있습니다. C++에서 websocket에 연결할 수있는 방법이 있습니까?C++ 프로그램에서 Socket.IO에 연결할 수 있습니까?

임 복사 (임없이 C++ 프로그래머 등)은 포함하지 않고 그의 모든 코드는

int main(array<System::String ^> ^args) 
{ 
     array<System::String ^>^ dataArray = gcnew array<System::String ^>(51); 
     Socket^ aarsServer = gcnew Socket(AddressFamily::InterNetwork, SocketType::Stream, ProtocolType::Tcp); 
     array<unsigned char>^ MSG = gcnew array<unsigned char>(1024); 
     int a; 
     String ^dataAux;// ^dataSend; 

     SerialPort^ arduino; 
     arduino = gcnew SerialPort("COM4", 2400); 
     arduino->ReadTimeout = 1000; 

     for(int b=0;b<50;b++) 
       dataArray[b]="1234569877"; 

     while(1) 
     {  
       a=0; 
       try{ 
/* //LEITURA COM TIMEOUT 
       arduino->Open(); 
       dataAux=""; 
       arduino->DiscardOutBuffer(); 
       dataAux = arduino->ReadLine(); 
       arduino->Close(); 

*/ 
       ////////BLOKO GRANDE//////////////////////  
       if(dataAux!="") 
         { 
           while(a<50) 
             { 
               //PROCURAR ESPACO FREE E BUFFERIZR 
               if(dataArray[a]=="") 
                 break; 
                 a++; 
             } 
           dataArray[a]=dataAux; 


      try{ 
         //TENTAR ESTABELER, ENVIAR EM REALTIME E LIMPAR O BUFFER 
       IPEndPoint^ iped = gcnew IPEndPoint(IPAddress::Parse("192.168.10.2"), 8765);//Server IP 
     aarsServer->Connect(iped); 
       //int rcv = aarsServer->Receive(MSG); 
       //dataSend="1, " + dataAux + ", 155.238.44.55"; 
       MSG = Encoding::ASCII->GetBytes("1," + dataArray[a] + ",192.168.10.1");//device IP 
       aarsServer->Send(MSG, MSG->Length, SocketFlags::None); 
       aarsServer->Shutdown(SocketShutdown::Both); 
       aarsServer->Close(); 
       dataArray[a]=""; 
       } 

       catch (SocketException^ BB) 
    { 
     Console::WriteLine("Connection Failed with error: {0}", BB->Message); 

    } 
     ////////BLOKO GRANDE////////////////////// /////////////////////////// 
       } 
       } 

       catch (TimeoutException ^AA) 
       { 
       arduino->Close(); 

       } 
       ////////BLOKO PEKENO//////////////////////  

      try{ 
         //TENTAR ESTABELER, ENVIAR EM OFFLINE TUDO E LIMPAR O BUFFER 
       IPEndPoint^ iped = gcnew IPEndPoint(IPAddress::Parse("192.168.10.2"), 8765);//Server IP 
     aarsServer->Connect(iped); 
       //int rcv = aarsServer->Receive(MSG); 
       //dataSend="1, " + dataAux + ", 155.238.44.55"; 
       for(int c=0; c<50; c++) 
       { 
         if(dataArray[c]!="") 
         { 
           MSG = Encoding::ASCII->GetBytes("0," + dataArray[a] + ",192.168.10.1");//device IP 
           aarsServer->Send(MSG, MSG->Length, SocketFlags::None); 
           dataArray[c]=""; 
         } 
       } 
       aarsServer->Shutdown(SocketShutdown::Both); 
       aarsServer->Close(); 

       } 

       catch (SocketException^ BB) 
    { 
     Console::WriteLine("Connection Failed with error: {0}", BB->Message); 

    } 
     ////////BLOKO PEKENO////////////////////// /////////////////////////// 



     } 
} 
+1

그가 지금까지 무엇을 시도 했습니까? – M4rc

+0

물론 C++로 소켓에 연결할 수 있습니다. 지금까지 시도한 것을 보여줄 필요가 있으므로 도움을받을 수 있습니다. – john

+0

코드를 빨리 가져올 것입니다 ... – Brad

답변

1

당신이 C에서 WebSocket을 사용하려는 경우 ++ 다음은 C++ 웹 소켓 라이브러리를 사용하는 것이 좋습니다. 이 위키 피 디아 page에 대한 목록이 있습니다.

이전에 libwebsockets을 사용하여 HTML 5 응용 프로그램을 C++ 응용 프로그램에 연결했습니다. 라이브러리에는 샘플 클라이언트 및 서버 코드 here이 있습니다.

관련 문제