Click to See Complete Forum and Search --> : Eh...It won't connect at all


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.

wildfrog
June 24th, 2007, 08:59 PM
Where do you attempt to connect the socket? I can see you create the socket, but none attempts to connect it to a remote host.

- petter

ccubed
June 24th, 2007, 09:08 PM
Well, actually, i hadn't noticed that so thank you. However, my problem is with WSAStartup which keeps returning a false apparently because my program keeps doing that if statement, the if ( !WSAStartup... ) one.


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;
}


that part. i have if (!WSAStartup...) and it apparently keeps failing.

wildfrog
June 24th, 2007, 09:48 PM
You should read the documentation on WSAStartup.

If successful, the WSAStartup function returns zero. Otherwise, it returns one of the error codes listed below.

http://msdn2.microsoft.com/en-us/library/ms742213.aspx

- petter

ccubed
June 24th, 2007, 10:58 PM
I figured it out. It was always working. Only thing is that now i get weird output. I'll include the program so you can download it. It outputs weird greek letters. for the purpose of testing i've been using:

host: sw.shatteredequinox.com
port: 6666

Since it's a text based mud and that's my clients purpose.

also, it's GPL so if you want to take my source code and use it go ahead, just sorta list me as a contributor or some such. Ccubed will work.