你能从C ++程序连接到Socket.IO吗?

时间:2012-10-24 10:19:08

标签: c++ html5 websocket socket.io

我们有一个学生项目,我的队友必须通过套接字连接到我。我运行一个HTML5网页,并与我创建一个Socket.IO服务器分开。他正在运行一个C ++程序,在那里他扫描RFID,他必须通过套接字发送到我的网页。我们正努力与他建立联系。有没有办法让他用C ++连接我的websocket?

我在没有包含的情况下复制他的所有代码(因为我没有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 个答案:

答案 0 :(得分:1)

如果要在C ++中使用WebSockets,则应考虑使用C ++ WebSocket库。这个维基百科上有一个列表page

我之前使用libwebsockets将HTML 5应用程序连接到C ++应用程序。该库附带了示例客户端和服务器代码here