ccubed
June 24th, 2007, 08:44 PM
Okay, i'm working on a mud client and right now i'm in the hard part, the sockets. I have a socket class to make it easier for me to use them since i'll only ever use one socket per instance of my program running. But no matter what I type it won't connect to anything. Anyone help?
Here is main.cpp
#include "socket_funcs.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main(){
char hostname[50];
string tmph;
bool skip=false;
int port;
novasock NSock;
cout << "#Nova Client is awaiting the User to enter the Host." << endl;
cin >> hostname;
cout << "#Nova Client is awaiting the User to enter the port." << endl << endl;
cin >> port;
cout << "#Nova Client is connecting to " << hostname << " on " << port << "." << endl;
NSock.sock_info( port, hostname, NSock );
if ( NSock.open_socket( NSock ) == true ){
cout << "#Nova Client has connected to " << hostname << " on " << port << "." << endl;
int i = 0;
while( i != 1 ){
if ( !NSock.get_data( NSock ) ){
i = 1;
cout << "#Error Receiving Data." << endl;
}
cin >> NSock.buffer_out;
if ( !NSock.send_data( NSock ) ){
i = 1;
cout << "#Error sending data." << endl;
}
}
}
else{
cout << "#Nova Client couldn't connect to " << hostname << " on " << port << "." << endl;
}
cout << "#Nova Client is exiting." << endl;
NSock.close_Nsock( NSock );
system("pause");
}
Below is the Nsocket class file.
/*
Nova Client
Created: June 22, 2007
Last Updated: June 22, 2007
Description: This file contains all the socket functions this client uses
*/
#include <winsock.h>
#include <windows.h>
#include <iostream>
//a socket class
class novasock{
public:
void sock_info( int num, char *hostn, novasock &NSock){
NSock.port = num;
NSock.address = hostn;
}
bool get_data( novasock &NSock ){
int nlet=sizeof(struct sockaddr);
NSock.error_ret = recvfrom(NSock.Nova, NSock.buffer_in, sizeof(NSock.buffer_in), 0, (LPSOCKADDR) &NSock.sin, &nlet);
printf("%s\n",NSock.buffer_in);
}
bool send_data( novasock &NSock ){
if ( sizeof(NSock.buffer_out) >= 1 ){
NSock.error_ret = sendto(NSock.Nova, NSock.buffer_out, strlen(NSock.buffer_out), 0, (LPSOCKADDR) &NSock.sin, sizeof(struct sockaddr) );
return true;
}
else{
return false;
}
}
bool open_socket( novasock &NSock ){
NSock.WVer = MAKEWORD(1,1);
if (!WSAStartup( NSock.WVer, &NSock.WsaData )){
std::cout << "Couldn't Initilize Socket. Check your Internet Connnection." << std::endl;
return false;
}
else{
NSock.LPHostEnt = gethostbyname(NSock.address);
if ( NSock.LPHostEnt == NULL ){
std::cout << "Couldn't find " << NSock.address << "." << std::endl;
return false;
}
else{
NSock.Nova = socket( AF_INET, SOCK_STREAM, 0 );
return true;
}
}
}
void close_Nsock(novasock &NSock){
closesocket(NSock.Nova);
}
char buffer_in[200]; //these two are used too much to be private
char buffer_out[200];//" "
private:
int port;
int error_ret;
LPHOSTENT LPHostEnt;
WSADATA WsaData;
WORD WVer;
SOCKET Nova;
char *address;
struct sockaddr_in sin;
struct hostent host;
};
So, what am I doing wrong here that I can't connect to anything? I mean, it's like it's not even trying. I don't even get a firewall error.
Here is main.cpp
#include "socket_funcs.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main(){
char hostname[50];
string tmph;
bool skip=false;
int port;
novasock NSock;
cout << "#Nova Client is awaiting the User to enter the Host." << endl;
cin >> hostname;
cout << "#Nova Client is awaiting the User to enter the port." << endl << endl;
cin >> port;
cout << "#Nova Client is connecting to " << hostname << " on " << port << "." << endl;
NSock.sock_info( port, hostname, NSock );
if ( NSock.open_socket( NSock ) == true ){
cout << "#Nova Client has connected to " << hostname << " on " << port << "." << endl;
int i = 0;
while( i != 1 ){
if ( !NSock.get_data( NSock ) ){
i = 1;
cout << "#Error Receiving Data." << endl;
}
cin >> NSock.buffer_out;
if ( !NSock.send_data( NSock ) ){
i = 1;
cout << "#Error sending data." << endl;
}
}
}
else{
cout << "#Nova Client couldn't connect to " << hostname << " on " << port << "." << endl;
}
cout << "#Nova Client is exiting." << endl;
NSock.close_Nsock( NSock );
system("pause");
}
Below is the Nsocket class file.
/*
Nova Client
Created: June 22, 2007
Last Updated: June 22, 2007
Description: This file contains all the socket functions this client uses
*/
#include <winsock.h>
#include <windows.h>
#include <iostream>
//a socket class
class novasock{
public:
void sock_info( int num, char *hostn, novasock &NSock){
NSock.port = num;
NSock.address = hostn;
}
bool get_data( novasock &NSock ){
int nlet=sizeof(struct sockaddr);
NSock.error_ret = recvfrom(NSock.Nova, NSock.buffer_in, sizeof(NSock.buffer_in), 0, (LPSOCKADDR) &NSock.sin, &nlet);
printf("%s\n",NSock.buffer_in);
}
bool send_data( novasock &NSock ){
if ( sizeof(NSock.buffer_out) >= 1 ){
NSock.error_ret = sendto(NSock.Nova, NSock.buffer_out, strlen(NSock.buffer_out), 0, (LPSOCKADDR) &NSock.sin, sizeof(struct sockaddr) );
return true;
}
else{
return false;
}
}
bool open_socket( novasock &NSock ){
NSock.WVer = MAKEWORD(1,1);
if (!WSAStartup( NSock.WVer, &NSock.WsaData )){
std::cout << "Couldn't Initilize Socket. Check your Internet Connnection." << std::endl;
return false;
}
else{
NSock.LPHostEnt = gethostbyname(NSock.address);
if ( NSock.LPHostEnt == NULL ){
std::cout << "Couldn't find " << NSock.address << "." << std::endl;
return false;
}
else{
NSock.Nova = socket( AF_INET, SOCK_STREAM, 0 );
return true;
}
}
}
void close_Nsock(novasock &NSock){
closesocket(NSock.Nova);
}
char buffer_in[200]; //these two are used too much to be private
char buffer_out[200];//" "
private:
int port;
int error_ret;
LPHOSTENT LPHostEnt;
WSADATA WsaData;
WORD WVer;
SOCKET Nova;
char *address;
struct sockaddr_in sin;
struct hostent host;
};
So, what am I doing wrong here that I can't connect to anything? I mean, it's like it's not even trying. I don't even get a firewall error.