#include #include #include #include #include #include #include #include #include #include "sqlite3.h" using namespace std; int write_log(char *myinput){ char to_write[200]; strcpy(to_write,myinput); return 0; } DWORD WINAPI SocketHandler(void*); int main(int argv, char** argc){ //The port you want the server to listen on int host_port= 1101; int addr_size = sizeof(SOCKADDR); //Initialize socket support WINDOWS ONLY! unsigned short wVersionRequested; WSADATA wsaData; int err; wVersionRequested = MAKEWORD( 2, 2 ); err = WSAStartup( wVersionRequested, &wsaData ); if ( err != 0 || ( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion ) != 2 )) { fprintf(stderr, "Error with sock dll %d\n",WSAGetLastError()); goto FINISH; } //Initialize sockets and set any options SOCKET hsock; int * p_int ; hsock = socket(AF_INET, SOCK_STREAM, 0); if(hsock == -1){ printf("Error initializing socket %d\n",WSAGetLastError()); goto FINISH; } p_int = (int*)malloc(sizeof(int)); *p_int = 1; if( (setsockopt(hsock, SOL_SOCKET, SO_REUSEADDR, (char*)p_int, sizeof(int)) == -1 )|| (setsockopt(hsock, SOL_SOCKET, SO_KEEPALIVE, (char*)p_int, sizeof(int)) == -1 ) ){ printf("Error setting options %d\n", WSAGetLastError()); free(p_int); goto FINISH; } free(p_int); //Bind and listen struct sockaddr_in my_addr; my_addr.sin_family = AF_INET ; my_addr.sin_port = htons(host_port); memset(&(my_addr.sin_zero), 0, 8); my_addr.sin_addr.s_addr = INADDR_ANY ; if( bind( hsock, (struct sockaddr*)&my_addr, sizeof(my_addr)) == -1 ){ fprintf(stderr,"Error binding to socket, make sure nothing else is listening on this port %d\n",WSAGetLastError()); goto FINISH; } if(listen( hsock, 10) == -1 ){ fprintf(stderr, "Error listening %d\n",WSAGetLastError()); goto FINISH; } //Wait for connection SOCKET csock; sockaddr_in sadr; while(true){ printf("\nWaiting for a connection\n"); if((csock = accept( hsock, (SOCKADDR*)&sadr, &addr_size))!= INVALID_SOCKET ){ printf("Connection from %s",inet_ntoa(sadr.sin_addr)); //Create Thread for data CreateThread(0,0,&SocketHandler, (void*) csock , 0,0); } else{ fprintf(stderr, "Error accepting %d\n",WSAGetLastError()); } } FINISH: ; } DWORD WINAPI SocketHandler(void* lp){ SOCKET csock = (SOCKET)lp; //Send banner char banner[1000000]; strcat(banner, "Custom Service v10.4\n"); send(csock, banner, strlen(banner) , 0); char buffer[1000000]; char query[200] = "SELECT * FROM customers WHERE ID = "; int buffer_len = 1000000; int bytecount; if((bytecount = recv(csock, buffer, buffer_len, 0))==SOCKET_ERROR){ fprintf(stderr, "Error receiving data %d\n", WSAGetLastError()); } // Create the query strcat(query,buffer); //Write query to log write_log(query); //Connect to DB sqlite3 * db; sqlite3_open("customers.db3", &db); sqlite3_stmt *statement; string s; // Get data from DB if ( sqlite3_prepare(db, query, -1, &statement, 0 ) == SQLITE_OK ) { int ctotal = sqlite3_column_count(statement); int res = 0; while ( 1 ) { res = sqlite3_step(statement); if ( res == SQLITE_ROW ) { for ( int i = 1; i < ctotal; i++ ) { s += (char*)sqlite3_column_text(statement, i); s += " "; // print or format the output as you want cout << s << " " ; } cout << endl; } if ( res == SQLITE_DONE || res==SQLITE_ERROR) { cout << "done " << endl; break; } } } // Create data to send back strcat(buffer, "\nParsing results... \nCustomer information: "); char *a=new char[s.size()+1]; a[s.size()]=0; memcpy(a,s.c_str(),s.size()); strcat(buffer, a); //Send data back if((bytecount = send(csock, buffer, strlen(buffer), 0))==SOCKET_ERROR){ fprintf(stderr, "Error sending data %d\n", WSAGetLastError()); goto FINISH; } FINISH: //Close connection closesocket(csock); printf("Sock cloes\n", bytecount); return 0; }