Jump to content
  • Advertisement
Sign in to follow this  
Paul-K

[C++] Got totally stucked with non-working code

This topic is 3120 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I'm trying to make a server with a switch that handles the respons, my code: --- Working, check bottom for working code I'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]

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites

#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 loop
for(;;)
{
ZeroMemory(buffer2,1024);
ZeroMemory(buffer,2048);
recv(Socket,buffer2,1024,0);
//Handle Data

char* Temp = DataHandler(buffer2);
//Send Respons

send(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 socket
shutdown(Socket,SD_SEND);

// Close our socket entirely
closesocket(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]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!