I set it up to where my laptop is the server, with an IP of "192.168.1.20" on port "5001". The client is the gateway and I am having trouble with the "server.accept()" function where it is waiting for a response from the gateway, but it never comes. Here is the code for that.
Code: Select all
bool CANGateway::connect()
{
#pragma region server_connect
SocketServer server;
server.bind("192.168.1.20", "5001");
SOCKET clientSock;
while(true)
{
clientSock = server.accept();
if(clientSock != INVALID_SOCKET)
break;
}
cout << "connected and bound to server" << endl;
SocketServerConn client(clientSock);
while(true)
{
char val;
int ret = client.recv(&val, 1, VirtualSocket::WAIT);
if(ret < 0)
{
cout<<"Error receiving data: "<<ret<<endl;
exit(1);
}
else if(ret > 0)
{
cout<<"got "<<val<<endl;
client.send(&val, 1);
if(val == '\n')
{
cout<<"Got end of string!"<<endl;
break;
}
}
else {/*empty*/}
}
#pragma endregion server_connect
int ret = _txSock.connect("192.168.1.10", "5000"); //PCAN gateway IP/port
cout << "_txSock connected status: " << ret << endl;
return ret;
}
Code: Select all
SOCKET SocketServer::accept()
{
if(_listenSock == INVALID_SOCKET)
{
throw runtime_error("Server not bound!");
}
// Accept a client socket
SOCKET clientSock;
clientSock = ::accept(_listenSock, NULL, NULL); // stuck here
if(clientSock == INVALID_SOCKET)
{
cout << "nothing connected" << endl;
}
return clientSock;
}