1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
| #include <WINSOCK.H>
#include "Interpreter.h"
using namespace std;
void Interpreter::Read()
{
// Call reader connection method
string beaconID = "";
string beaconDist = "";
unsigned int posSP = 0;
unsigned int posDB = 0;
unsigned int positionComma = 0;
string line;
WSADATA wsdata;
if (WSAStartup(WSVERS, &wsdata)) // open window dll
printf("WSAStartup failed\n");
/* put the server information into the server structure */
/* The port must be put into network byte order */
strcpy(svc_address, DEFAULTSERVADDR);
port = DEFAULTPORT;
strcpy(buf1024, DEFAULTMSG);
server.sin_family = AF_INET;
server.sin_port = htons(port);
server.sin_addr.s_addr = inet_addr(svc_address);
printf("Server Address: %s\n", svc_address);
printf("Port Client will connect to: %d\n", port);
CRICKET_client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (CRICKET_client == INVALID_SOCKET)
{
printf("Error\n");
exit(INVALID_SOCKET);
} else if ((int)CRICKET_client == -1)
{
printf("Fails to open a socket\n");
exit(-1);
}
printf("CRICKET Client opens a socket successfully...\n");
/* Connect to CRICKET daemon */
status = connect(CRICKET_client, (struct sockaddr *) &server, sizeof(server));
printf("Status of connection: %d\n", status);
if (status == -1)
{
printf("CRICKET Client fails to connect to the CRICKET daemon.....\n");
printf("The CRICKET daemon may not be active\n");
exit(status);
}
printf("CRICKET Client connects to the CRICKET daemon successfully...\n");
memset(buf1,0,16);
strcpy(buf1, "r\r\n");
if ((status = send(CRICKET_client, buf1, sizeof(buf1), 0)) < 0)
{
printf("%d\n", status);
printf("CRICKET Client fails to send Username to the CRICKET daemon.....\n");
exit(status);
}
/* receive the results of the command */
while (1)
{
memset(buf1, 0, 2);
if (recv(CRICKET_client, buf1, sizeof(buf1), 0) < 0)
{
printf("CRICKET Client fails to receive the message from the CRICKET daemon successfully.....\n");
exit(status);
}
else
{
line.assign(buf1);
//printf(buf1);
posSP = line.find("SP=");
posDB = line.find("DB=");
if(posSP != string::npos && posDB != string::npos)
{
positionComma = line.find(",",posSP);
if(positionComma != string::npos)
beaconID = line.substr(posSP+3, (positionComma-posSP)-3);
positionComma = line.find(",",posDB);
if(positionComma != string::npos)
beaconDist = line.substr(posDB+3, (positionComma-posDB)-3);
cout << beaconID << "," << beaconDist << endl;
}
else
{
if(line == "")
{
emptyLines++;
if(emptyLines > 10)
{
cout << "No data received from the cricket client." << endl;
//Game.over();
}
}
else
{
cout << "Line ignored" << endl;
}
}
}
}
/* close the socket */
closesocket(CRICKET_client);
} |
Partager