ccubed
June 25th, 2007, 11:26 PM
Okay, i've added in all the suggestions from various people and it's gone from bad to worse. I am completely lost as to why this thing doesn't work. So i'll post it and maybe someone can tell me why it doesn't. I'm really confused here. It connects, it receives data, but then it just stops. I get the intro even, but it just stops and won't receive or send data. Help me out please. I know it's a lot to ask, but i'm at an empass and i have no idea what's wrong.
/*
Nova Client
Description: This is the main file. it handles the blunt of the program
*/
#include "socket_funcs.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main(){
std::string addr;
int prt;
novasock NSock;
int cmd_ret;
cout << "#Nova Client Version 1.0" << endl;
begin:
cout << "#Nova Client is awaiting the User to enter the Host." << endl;
getline(cin, addr);
cout << "#Nova Client is awaiting the User to enter the port." << endl << endl;
cin >> prt;
cin.ignore(1000,'\n');
if ( NSock.init_wsa() == false ){
cout << "#Error initilizing WSADATA." << endl;
goto ask;
}
cout << "#Nova Client is connecting to " << addr << " on " << prt << "." << endl;
NSock.sock_info( prt, addr );
if ( NSock.open_socket() == true ){
cout << "#Nova Client has connected to " << addr << " on " << prt << "." << endl;
int i = 0; //get errors if it's declared in the while loop
while( i != 1 ){
if ( NSock.get_data() == false ){
i = 1;
cout << "#Error Receiving Data." << endl;
}
//make sure the above didn't fail
if ( i != 1 ){
getline(cin, NSock.out );
cmd_ret = NSock.cmd( NSock.out );
if ( cmd_ret == 1 ){
i = 1;
cout << "#Nova Client is exiting." << endl;
}
else if ( cmd_ret == 2 ){
NSock.out = NSock.last_cmd;
if ( NSock.send_data() == false ){
i = 1;
cout << "#Error Sending data." << endl;
}
}
else if ( cmd_ret == 0 ){
if ( NSock.send_data() == false ){
i = 1;
cout << "#Error sending data." << endl;
}
NSock.last_cmd = NSock.out;
}
}
}
}
else{
cout << "#Nova Client couldn't connect to " << addr << " on " << prt << "." << endl;
}
ask:
cout << "Exit Nova Client?(Y/N)" << endl;
std::string temp;
getline(cin,temp);
if ( temp == "Y" ){
cout << "#Nova Client is exiting." << endl;
NSock.close_Nsock();
WSACleanup();
system("pause");
}
else if ( temp == "N" ){
goto begin;
}
else{
cout << "#Invalid Option, please enter Y for yes and N for no." << endl;
goto ask;
}
}
Main.cpp is above and Socket_funcs.h below
/*
Nova Client
Description: This file contains all the socket functions this client uses
*/
#include <winsock.h>
#include <windows.h>
#include <winsock2.h>//for select, implemented later.
#include <iostream>
#include <string>
#define DEBUG
//Socket Class for Nova Client
class novasock{
public:
int cmd( std::string command ){
if ( command == "\\exit" ){
return 1;
}
else if ( command == "\\!" ){
return 2;
}
else{
return 0;
}
}
void sock_info( int num, std::string hostn ){
port = num;
address = hostn;
}
void set_maxbuff( int num ){
MXBUFA = num;
}
bool get_data(){
datasize = recv( Nova, buffer_in, MXBUFA, 0);
if ( datasize > 0 || datasize <= 1024 ){
#ifdef DEBUG
std::cout << "#DEBUG: " << datasize << " Bytes received." << std::endl;
#endif
printf("%s\n",buffer_in);
return true;
}
else if( datasize > 512 || datasize != 0 ){
int totrev;
int esize = datasize;//make sure we don't lose the original amount of data we SHOULD receive
printf("%s\n",buffer_in);
#ifdef DEBUG
int times;
#endif
while ( totrev != esize ){
datasize = recv( Nova, buffer_in, 1024, 0 );
printf("%s\n",buffer_in);
totrev += datasize;
times += 1;
//if datasize is somehow greater then what we expected, treat it as new data. Should always be lower.
if ( datasize > esize ){
#ifdef DEBUG
std::cout << "Debug:MISC WARN:Datasize is bigger then what we SHOULD have received. DATASIZE " << datasize << ". ESIZE " << esize << ". treating it as new data we should get." << std::endl;
#endif
esize = datasize;
}
}
#ifdef DEBUG
std::cout << "#debug: Received a total of " << datasize << " bytes in " << times << " recv() processes." << std::endl;
#endif
return true;
}
else if( datasize == 0 ){
#ifdef DEBUG
std::cout << "#debug: No data to receive" << std::endl;
#endif
return true;
}
else{
return false;
}
}
bool send_data(){
if ( sizeof(out) >= 1 ){
datasize = send( Nova, out.c_str(), sizeof(out), 0 );
#ifdef DEBUG
std::cout << "#DEBUG: " << datasize << " Bytes sent." << std::endl << "#DEBUG:SENT: " << out << " . " << std::endl;
#endif
return true;
}
else{
return false;
}
}
bool init_wsa(){
WVer = MAKEWORD(1,1);
error_ret = WSAStartup( WVer, &WsaData );
if ( error_ret != NO_ERROR ){
std::cout << "#Couldn't Initilize WSAData. Error " << error_ret << " returned." << std::endl;
return false;
}
return true;
}
bool open_socket(){
Nova = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
if ( Nova == INVALID_SOCKET ){
printf("#socket failed with error: %ld\n", WSAGetLastError());
return false;
}
if ( Nova != INVALID_SOCKET){
LPHostEnt = gethostbyname(address.c_str());
sin.sin_family = AF_INET;
sin.sin_addr = *((LPIN_ADDR)*LPHostEnt->h_addr_list);
sin.sin_port=htons(port);
}
if ( LPHostEnt == NULL ){
std::cout << "Couldn't find " << address << "." << std::endl;
return false;
}
if ( connect( Nova, (SOCKADDR*) &sin, sizeof(sin) ) == SOCKET_ERROR ){
std::cout << "Error Connecting to " << address << " on port " << port << "." << std::endl;
return false;
}
return true;
}
void close_Nsock(){
closesocket(Nova);
}
//these two need to be able to be accessed by non members of novasock
char buffer_in[1024];
std::string out;
std::string last_cmd;//last command sent to server
private:
int port;
int datasize;
int error_ret;
LPHOSTENT LPHostEnt;
WSADATA WsaData;
WORD WVer;
std::string address;
SOCKET Nova;
int MXBUFA;
struct sockaddr_in sin;
};
MXBUFA isn't used yet, it's there for later implementation.
/*
Nova Client
Description: This is the main file. it handles the blunt of the program
*/
#include "socket_funcs.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main(){
std::string addr;
int prt;
novasock NSock;
int cmd_ret;
cout << "#Nova Client Version 1.0" << endl;
begin:
cout << "#Nova Client is awaiting the User to enter the Host." << endl;
getline(cin, addr);
cout << "#Nova Client is awaiting the User to enter the port." << endl << endl;
cin >> prt;
cin.ignore(1000,'\n');
if ( NSock.init_wsa() == false ){
cout << "#Error initilizing WSADATA." << endl;
goto ask;
}
cout << "#Nova Client is connecting to " << addr << " on " << prt << "." << endl;
NSock.sock_info( prt, addr );
if ( NSock.open_socket() == true ){
cout << "#Nova Client has connected to " << addr << " on " << prt << "." << endl;
int i = 0; //get errors if it's declared in the while loop
while( i != 1 ){
if ( NSock.get_data() == false ){
i = 1;
cout << "#Error Receiving Data." << endl;
}
//make sure the above didn't fail
if ( i != 1 ){
getline(cin, NSock.out );
cmd_ret = NSock.cmd( NSock.out );
if ( cmd_ret == 1 ){
i = 1;
cout << "#Nova Client is exiting." << endl;
}
else if ( cmd_ret == 2 ){
NSock.out = NSock.last_cmd;
if ( NSock.send_data() == false ){
i = 1;
cout << "#Error Sending data." << endl;
}
}
else if ( cmd_ret == 0 ){
if ( NSock.send_data() == false ){
i = 1;
cout << "#Error sending data." << endl;
}
NSock.last_cmd = NSock.out;
}
}
}
}
else{
cout << "#Nova Client couldn't connect to " << addr << " on " << prt << "." << endl;
}
ask:
cout << "Exit Nova Client?(Y/N)" << endl;
std::string temp;
getline(cin,temp);
if ( temp == "Y" ){
cout << "#Nova Client is exiting." << endl;
NSock.close_Nsock();
WSACleanup();
system("pause");
}
else if ( temp == "N" ){
goto begin;
}
else{
cout << "#Invalid Option, please enter Y for yes and N for no." << endl;
goto ask;
}
}
Main.cpp is above and Socket_funcs.h below
/*
Nova Client
Description: This file contains all the socket functions this client uses
*/
#include <winsock.h>
#include <windows.h>
#include <winsock2.h>//for select, implemented later.
#include <iostream>
#include <string>
#define DEBUG
//Socket Class for Nova Client
class novasock{
public:
int cmd( std::string command ){
if ( command == "\\exit" ){
return 1;
}
else if ( command == "\\!" ){
return 2;
}
else{
return 0;
}
}
void sock_info( int num, std::string hostn ){
port = num;
address = hostn;
}
void set_maxbuff( int num ){
MXBUFA = num;
}
bool get_data(){
datasize = recv( Nova, buffer_in, MXBUFA, 0);
if ( datasize > 0 || datasize <= 1024 ){
#ifdef DEBUG
std::cout << "#DEBUG: " << datasize << " Bytes received." << std::endl;
#endif
printf("%s\n",buffer_in);
return true;
}
else if( datasize > 512 || datasize != 0 ){
int totrev;
int esize = datasize;//make sure we don't lose the original amount of data we SHOULD receive
printf("%s\n",buffer_in);
#ifdef DEBUG
int times;
#endif
while ( totrev != esize ){
datasize = recv( Nova, buffer_in, 1024, 0 );
printf("%s\n",buffer_in);
totrev += datasize;
times += 1;
//if datasize is somehow greater then what we expected, treat it as new data. Should always be lower.
if ( datasize > esize ){
#ifdef DEBUG
std::cout << "Debug:MISC WARN:Datasize is bigger then what we SHOULD have received. DATASIZE " << datasize << ". ESIZE " << esize << ". treating it as new data we should get." << std::endl;
#endif
esize = datasize;
}
}
#ifdef DEBUG
std::cout << "#debug: Received a total of " << datasize << " bytes in " << times << " recv() processes." << std::endl;
#endif
return true;
}
else if( datasize == 0 ){
#ifdef DEBUG
std::cout << "#debug: No data to receive" << std::endl;
#endif
return true;
}
else{
return false;
}
}
bool send_data(){
if ( sizeof(out) >= 1 ){
datasize = send( Nova, out.c_str(), sizeof(out), 0 );
#ifdef DEBUG
std::cout << "#DEBUG: " << datasize << " Bytes sent." << std::endl << "#DEBUG:SENT: " << out << " . " << std::endl;
#endif
return true;
}
else{
return false;
}
}
bool init_wsa(){
WVer = MAKEWORD(1,1);
error_ret = WSAStartup( WVer, &WsaData );
if ( error_ret != NO_ERROR ){
std::cout << "#Couldn't Initilize WSAData. Error " << error_ret << " returned." << std::endl;
return false;
}
return true;
}
bool open_socket(){
Nova = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
if ( Nova == INVALID_SOCKET ){
printf("#socket failed with error: %ld\n", WSAGetLastError());
return false;
}
if ( Nova != INVALID_SOCKET){
LPHostEnt = gethostbyname(address.c_str());
sin.sin_family = AF_INET;
sin.sin_addr = *((LPIN_ADDR)*LPHostEnt->h_addr_list);
sin.sin_port=htons(port);
}
if ( LPHostEnt == NULL ){
std::cout << "Couldn't find " << address << "." << std::endl;
return false;
}
if ( connect( Nova, (SOCKADDR*) &sin, sizeof(sin) ) == SOCKET_ERROR ){
std::cout << "Error Connecting to " << address << " on port " << port << "." << std::endl;
return false;
}
return true;
}
void close_Nsock(){
closesocket(Nova);
}
//these two need to be able to be accessed by non members of novasock
char buffer_in[1024];
std::string out;
std::string last_cmd;//last command sent to server
private:
int port;
int datasize;
int error_ret;
LPHOSTENT LPHostEnt;
WSADATA WsaData;
WORD WVer;
std::string address;
SOCKET Nova;
int MXBUFA;
struct sockaddr_in sin;
};
MXBUFA isn't used yet, it's there for later implementation.