Reputation: 87
We have a student project where my team-mate has to connect to me via a socket. Im running a HTML5 web page and separately from that Im creating a Socket.IO server. He is running a C++ program in which he scans RFIDs which he has to send to my webpage via the socket. We are struggling to make a connection from his side. Is there a way for him to connect to my websocket in C++ ?
Im copying all his code without the includes (as Im no c++ programmer)
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////////////////////// ///////////////////////////
}
}
Upvotes: 1
Views: 1701
Reputation: 771
If you want to use WebSockets in C++ then you should consider using a C++ WebSocket Library. There is a list of them on this Wikipedia page.
I have previously used libwebsockets to connect a HTML 5 application to a C++ application. The library comes with sample client and server code here.
Upvotes: 1