1

我们有一个学生项目,我的队友必须通过套接字连接到我。我正在运行一个 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//////////////////////  ///////////////////////////



        }
}
4

1 回答 1

1

如果您想在 C++ 中使用 WebSocket,那么您应该考虑使用 C++ WebSocket 库。在这个 Wikipedia页面上有一个它们的列表。

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

于 2012-11-06T19:02:53.153 回答