   [C++] Got totally stucked with non-working code
  Hello,I'm trying to make a server with a switch that handles the respons, my code:--- Working, check bottom for working codeI'm 100% I do it totally wrong but I've no new ideas anymore how to get it to work.Thanks![Edited by - Paul-K on May 1, 2010 5:58:52 AM]
  Your DataHandler function takes a char instead of a char * (and it's the same for the return value).Also, to compare two strings stored in char arrays, you have to use strcmp. The == operator will just compare the two pointers, not the strings.   ;#include <iostream>#include <winsock2.h>#include <time.h>#pragma comment(lib,"ws2_32.lib")using namespace std;char* DataHandler(char* Input);char BufBuf[1024];//ZeroMemory(BufBuf,1024);clock_t end;double runTime;clock_t start = clock();int main(){char buffer[2048];char buffer2[1024];WSADATA WsaDat;if(WSAStartup(MAKEWORD(2,2),&WsaDat)!=0){std::cout<<"WSA Initialization failed!\r\n";WSACleanup();system("PAUSE");return 0;}SOCKET Socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);if(Socket==INVALID_SOCKET){std::cout<<"Socket creation failed.\r\n";WSACleanup();system("PAUSE");return 0;}SOCKADDR_IN serverInf;serverInf.sin_family=AF_INET;serverInf.sin_addr.s_addr=INADDR_ANY;serverInf.sin_port=htons(5555);if(bind(Socket,(SOCKADDR*)(&serverInf),sizeof(serverInf))==SOCKET_ERROR){std::cout<<"Unable to bind socket!\r\n";WSACleanup();system("PAUSE");return 0;}listen(Socket,1);SOCKET TempSock=SOCKET_ERROR;while(TempSock==SOCKET_ERROR){std::cout<<"Waiting for incoming connections...\r\n";TempSock=accept(Socket,NULL,NULL);}// If iMode!=0, non-blocking mode is enabled.u_long iMode=1;ioctlsocket(Socket,FIONBIO,&iMode);Socket=TempSock;std::cout<<"Client connected!\r\n\r\n";// Main loopfor(;;){ZeroMemory(buffer2,1024);ZeroMemory(buffer,2048);recv(Socket,buffer2,1024,0);//Handle Datachar* Temp = DataHandler(buffer2);//Send Responssend(Socket,Temp,strlen(Temp),0);int nError=WSAGetLastError();if(nError!=WSAEWOULDBLOCK&&nError!=0){std::cout<<"Winsock error code: "<<nError<<"\r\n";std::cout<<"Client disconnected!\r\n";// Shutdown our socketshutdown(Socket,SD_SEND);// Close our socket entirelyclosesocket(Socket);break;}Sleep(5);}WSACleanup();system("PAUSE");return 0;}char* DataHandler(char* Input){if(strcmp(Input,"!uptime") == 0){end = clock();  runTime = ((end - start) / (double) CLOCKS_PER_SEC );int Days = runTime/60/60/24;int Hours = (runTime-(Days*60*60*24))/60/60;int Minutes = (runTime-(Days*60*60*24)-(Hours*60*60))/60;int Seconds = (runTime-(Days*60*60*24)-(Hours*60*60)-(Minutes*60));sprintf(BufBuf,"Uptime is: %dd %dh %dm %ds",Days,Hours,Minutes,Seconds);}else{sprintf(BufBuf,"Unknown command: %s",Input);}cout << "Returning| " << BufBuf << "\r\n";return BufBuf;}[Edited by - Paul-K on May 1, 2010 6:27:22 AM]