I was told olny the computer / home that the "host program" was in needed to be port forwarded, but you need port forwarding just to use the client too.


Base code for client program:

//Main.cpp
#include <iostream>
#include "Socket.h"
#include <fstream>
#include <string>

using namespace std;


string getcot(char *fnam)
{
     char joe[2000];
string pao;
ifstream file (fnam, ios::in);
  if (file.is_open())
  {
   cout<<"Writing.\n";

        while(!file.eof()) 
        {
              file.getline(joe,2000);
              cout << joe << "\n";
        }      


    file.close();
pao = static_cast<std::string>(joe);


return pao;
}

}


int main()
{
    int choice;
    int port = 4521;
    //char *ipAddress = "127.0.0.1";
    string ipAddress;
    bool done = false;
    char recMessage[STRLEN];
    char sendMessage[STRLEN];


        //Client

string wait;
cout<<"Azjherbeniex Hetnev Version 0.2\n\n";
cout<<"Input anything and press enter.\n";
cout<<"This lets us know you are here...\n";
cin>>wait;
cout<<"\n192.168.1.122\n";
        ClientSocket sockClient;
        cout<<"ATTEMPTING TO CONNECT..."<<endl;
        cout<<"IP:\n";
        string ipsas;
        cin>>ipsas;
        sockClient.ConnectToServer(ipsas.c_str(), port );


        //Connected

//74.76.149.190







        while ( !done )
        {     
        cout<<"Page: ";
            sockClient.GetAndSendMessage();
            cout<<"\t--WAIT--"<<endl;
            system("cls");
            sockClient.RecvData( recMessage, STRLEN );
            cout<<recMessage<<endl;
            cout<<"\n\n\n\n\n";

        }
}

SOCKETS.CPP FILE CODE:

//Socket.cpp
#include "Socket.h"

Socket::Socket()
{
    if( WSAStartup( MAKEWORD(2, 2), &wsaData ) != NO_ERROR )
    {
        cerr<<"Socket Initialization: Error with WSAStartup\n";
        system("pause");
        WSACleanup();
        exit(10);
    }

    //Create a socket
    mySocket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );

    if ( mySocket == INVALID_SOCKET )
    {
        cerr<<"Socket Initialization: Error creating socket"<<endl;
        system("pause");
        WSACleanup();
        exit(11);
    }

    myBackup = mySocket;
}

Socket::~Socket()
{
    WSACleanup();
}

bool Socket::SendData( char *buffer )
{
    send( mySocket, buffer, strlen( buffer ), 0 );
    return true;
}

bool Socket::RecvData( char *buffer, int size )
{
    int i = recv( mySocket, buffer, size, 0 );
    buffer[i] = '\0';
    return true;
}

void Socket::CloseConnection()
{
    //cout<<"CLOSE CONNECTION"<<endl;
    closesocket( mySocket );
    mySocket = myBackup;
}

void Socket::GetAndSendMessage()
{
    char message[STRLEN];
    cin.ignore();//without this, it gets the return char from the last cin and ignores the following one!
    //cout<<"Send > ";
    cin.get( message, STRLEN );
    SendData( message );
}

void ServerSocket::StartHosting( int port )
{
     Bind( port );
     Listen();
}

void ServerSocket::Listen()
{
    //cout<<"LISTEN FOR CLIENT..."<<endl;
    
    if ( listen ( mySocket, 1 ) == SOCKET_ERROR )
    {
        cerr<<"ServerSocket: Error listening on socket\n";
        system("pause");
        WSACleanup();
        exit(15);
    }
    
    //cout<<"ACCEPT CONNECTION..."<<endl;
    
    acceptSocket = accept( myBackup, NULL, NULL );
    while ( acceptSocket == SOCKET_ERROR )
    {
        acceptSocket = accept( myBackup, NULL, NULL );
    }
    mySocket = acceptSocket;
}

void ServerSocket::Bind( int port )
{
    myAddress.sin_family = AF_INET;
    myAddress.sin_addr.s_addr = inet_addr( "0.0.0.0" );
    myAddress.sin_port = htons( port );
    
    //cout<<"BIND TO PORT "<<port<<endl;

    if ( bind ( mySocket, (SOCKADDR*) &myAddress, sizeof( myAddress) ) == SOCKET_ERROR )
    {
        cerr<<"ServerSocket: Failed to connect\n";
        system("pause");
        WSACleanup();
        exit(14);
    }
}

void ClientSocket::ConnectToServer( const char *ipAddress, int port )
{
    myAddress.sin_family = AF_INET;
    myAddress.sin_addr.s_addr = inet_addr( ipAddress );
    myAddress.sin_port = htons( port );
    
    //cout<<"CONNECTED"<<endl;

    if ( connect( mySocket, (SOCKADDR*) &myAddress, sizeof( myAddress ) ) == SOCKET_ERROR )
    {
        cerr<<"ClientSocket: Failed to connect\n";
        system("pause");
        WSACleanup();
        exit(13);
    } 
}

Okay...

I was told that you needed to port forward if you were the host. (running the host on your pc)

But that the users (people running the clients) didn';t have to port forward to use their clients.

Well, they do with my program.
So how can I get the clients to work without port fowarding?

This article has been dead for over six months. Start a new discussion instead.