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

        }
}

Dani AI

Generated

Short answer for : a client that only initiates an outgoing TCP connection should not need port forwarding. The only machines that need their routers to forward a port are the ones that must accept inbound connections. The confusion seen in and ’s posts usually comes from two real-world quirks: NAT traversal and “hairpin”/loopback behavior on consumer routers.

Why it might look like the client needs forwarding

  • If the client ever needs to accept a connection (server connects back, peer-to-peer, callbacks), that client’s router must map an external port to it.
  • If you test from inside the same LAN but use the server’s public IP, many routers don’t loop that traffic back to the internal host (hairpin NAT). That makes it look like forwarding is required when it’s just a routing quirk. See NAT and NAT loopback for details: Network Address Translation and NAT loopback.

Practical checklist to diagnose and fix it

  • Test from a true external network (use a phone on cellular or ask a friend).
  • Verify the server is listening and bound to the correct interface. Use netstat or equivalent to confirm a listening socket.
  • Check the server machine’s firewall and the router’s port-forward entry (forward the external port to the server’s LAN IP).
  • If you need peer-to-peer without manual forwarding on both sides, implement NAT traversal (STUN/ICE) or use a relay (TURN/central server). RFC 5389 covers STUN basics: RFC 5389.

If tests from outside succeed but inside fail, it’s almost certainly hairpin/NAT-loopback. If external tests fail, double-check firewall and router port-forward settings and that the server binds to the intended address.

Recommended Answers

All 4 Replies

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

what the hell! :-O Am I the only one who has absolutely no clue what he wants?

I have no fucking idea what the question is

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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.