Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3429358
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:04:18+00:00 2026-05-18T07:04:18+00:00

I have a server running on one machine and have the port it uses

  • 0

I have a server running on one machine and have the port it uses forwarded to my router, and another machine running the client connecting to the server using my ISP assigned external IP address instead of the local address. This all works fine and it connects but When I check the address of the connected socket (client), The IP address that it shows is completely different? it shows me 148.49.68.0. I can’t find this on ipconfig and don’t understand where this is popping up from. Shouldn’t the client show my external address? (seeing as both computers use the same External IP address).

[EDIT] added server source

#include <iostream>
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <string.h>
using namespace std;

int PORT;
const int winsock_version = 2;
const int max_con = 10;
string SERVER_ADDRS;

void Bind(SOCKET &serv,struct sockaddr_in &serv_info,int size);
void Listen(SOCKET &serv,int max_con);
void connection_info(struct sockaddr_in &client);
bool communication(SOCKET &client); 
SOCKET Accept(SOCKET &serv);

int main(void){

    WSADATA wsadata;
    if ( WSAStartup(MAKEWORD(winsock_version,0),&wsadata) == 0 ){
        cout<<"-[Initialized.]" << endl;
        cout<<"-[Server Address (leave blank to scan for all IP's)]: ";
        getline(cin,SERVER_ADDRS);
        cout<<"-[Port]: ";
        cin>>PORT;

        struct sockaddr_in serv_info;
        serv_info.sin_family = AF_INET;
        serv_info.sin_port = htons(PORT);
        if( sizeof(SERVER_ADDRS) > 5 ){
            cout<<"-[Listening on: " << SERVER_ADDRS << "]" << endl;
            serv_info.sin_addr.s_addr = inet_addr(SERVER_ADDRS.c_str());
        }else{
            cout<<"-[Scanning for All IP's]" << endl;
            serv_info.sin_addr.s_addr = INADDR_ANY;
        }

        SOCKET serv;
        serv = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
        if ( serv != INVALID_SOCKET ){
            //------------------------------------------------------------
            Bind(serv,serv_info,sizeof(serv_info));
            Listen(serv,max_con);


            struct sockaddr_in client_info;
            int size = sizeof(client_info);

            SOCKET client_sock = Accept(serv);
            connection_info(client_info);

            if (communication(client_sock) == true){
                closesocket(serv);
                closesocket(client_sock);
            }
            //------------------------------------------------------------
        }

    }else{
        cout<<"-[Initialization failed, running cleanup.]" << endl;
    }

    if (WSACleanup() == 0){
        cout<<"-[Cleanup Successful.]" << endl;
    }

    return 0;
}

void Bind(SOCKET &serv,struct sockaddr_in &serv_info,int size){

    if ( bind(serv,(sockaddr*)&serv_info,size) != -1 ){
        //Binding complete, now clear the port and allow for reuse if needed using setsockopt
        char yes = '1';
        if ( setsockopt(serv,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(yes)) != SOCKET_ERROR){
                cout<<"-[Binding Successful.]" << endl;
        }
    }
}

void Listen(SOCKET &serv,int max_con){
    if ( listen(serv,max_con) != -1 ){
        cout<<"-[Listening for connections.] " << endl;
    }
}

SOCKET Accept(SOCKET &serv){

    struct sockaddr_in client_info;
    int size = sizeof(client_info);
    SOCKET recv;

    recv = accept(serv,(sockaddr*)&client_info,&size);
    if (recv != INVALID_SOCKET ) {
        return recv;
    }else{
        cout<<"-[Invalid Socket.]" << endl;
    }
}

void connection_info(struct sockaddr_in &client){
    char *connected_ip= inet_ntoa(client.sin_addr); 
    int port = ntohs(client.sin_port);

    cout<<"-[IP:" << connected_ip <<", Connected on PORT:"<< port  << "]"<< endl;
}

bool communication(SOCKET &client){
    cout<<"[---------------{CHAT}---------------]" << endl;
    int bytes_in;
    int bytes_out;
    char recvd_text[80];
    string send_text;

    while(true){
        cout<<"-[SERVER]: ";
        getline(cin,send_text);
        if (sizeof(send_text) > 0 ){
            bytes_out = send(client,send_text.c_str(),send_text.length()+1,0);
            cout<< endl;
            if (bytes_out == SOCKET_ERROR){
                cout<<"-[SERVER error in sending.]" << endl;
                break;
            }
        }

        bytes_in = recv(client,recvd_text,sizeof(recvd_text),0);
        if (bytes_in > 0 ){
            cout<<"-[CLIENT]: " << recvd_text << endl;  //output on screen
        }
        if (bytes_in == 0){
            cout<<"-[CLIENT has disconnected.]" << endl;
            break;
        }
        if (bytes_in == SOCKET_ERROR){
            cout<<"-[CLIENT closed unexpectedly.]" << endl; 
            break;
        }

    }
    return true;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T07:04:19+00:00Added an answer on May 18, 2026 at 7:04 am

    Try this:

    #include <iostream>
    #include <windows.h>
    #include <winsock2.h>
    #include <ws2tcpip.h>
    #include <string>
    
    int PORT;
    const int winsock_version = 2;
    const int max_con = 10;
    std::string SERVER_ADDRS;
    
    void Bind(SOCKET &serv, const struct sockaddr_in &serv_info);
    void Listen(SOCKET &serv, int max_con);
    void connection_info(struct sockaddr_in &client);
    bool communication(SOCKET client);
    SOCKET Accept(SOCKET serv, sockaddr_in &client_info); 
    
    int main(void)
    {
        WSADATA wsadata; 
        if ( WSAStartup(MAKEWORD(winsock_version,0),&wsadata) == 0 )
        { 
            std::cout << "-[Initialized.]" << std::endl; 
            std::cout << "-[Server Address (leave blank to scan for all IP's)]: "; 
            std::getline(std::cin, SERVER_ADDRS); 
                std::cout << "-[Port]: "; 
            std::cin >> PORT; 
    
            struct sockaddr_in serv_info = {0}; 
            serv_info.sin_family = AF_INET; 
            serv_info.sin_port = htons(PORT); 
            if( SERVER_ADDRS.length() > 0 )
            { 
                std::cout << "-[Listening on: " << SERVER_ADDRS << "]" << std::endl; 
                serv_info.sin_addr.s_addr = inet_addr(SERVER_ADDRS.c_str()); 
            }
            else
            { 
                std::cout << "-[Scanning for All IP's]" << std::endl; 
                serv_info.sin_addr.s_addr = INADDR_ANY; 
            } 
    
            SOCKET serv = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 
            if ( serv != INVALID_SOCKET )
            { 
                //------------------------------------------------------------ 
                Bind(serv, serv_info); 
                Listen(serv, max_con); 
    
                struct sockaddr_in client_info = {0}; 
    
                SOCKET client_sock = Accept(serv, client_info);
                if ( client_sock != INVALID_SOCKET )
                {
                    connection_info(client_info); 
                    communication(client_sock);
    
                    closesocket(client_sock); 
                }
                //------------------------------------------------------------ 
    
                closesocket(serv); 
            } 
    
    
            if (WSACleanup() == 0)
            { 
                std::cout << "-[Cleanup Successful.]" << std::endl; 
            }
        }
        else
        { 
            std::cout << "-[Initialization failed.]" << std::endl; 
        } 
    
        return 0; 
    } 
    
    void Bind(SOCKET serv, const struct sockaddr_in &serv_info)
    { 
        //clear the port and allow for reuse before binding it
        int yes = 1; 
        if ( setsockopt(serv, SOL_SOCKET, SO_REUSEADDR, (char*)&yes, sizeof(yes)) != SOCKET_ERROR)
        {
            std::cout << "-[Reuse Address Successful.]" << std::endl; 
        }
    
        if ( bind(serv, (sockaddr*)&serv_info, sizeof(serv_info)) != -1 )
        { 
            std::cout << "-[Binding Successful.]" << std::endl; 
        } 
    } 
    
    void Listen(SOCKET serv, int max_con)
    { 
        if ( listen(serv, max_con) != -1 )
        { 
            std::cout << "-[Listening for connections.] " << std::endl; 
        } 
    } 
    
    SOCKET Accept(SOCKET &serv, sockaddr_in &client_info)
    { 
        int size = sizeof(client_info); 
        SOCKET recv = accept(serv, (sockaddr*)&client_info, &size); 
        if ( recv == INVALID_SOCKET )
        { 
                std::cout << "-[Invalid Socket.]" << std::endl; 
        } 
        return recv; 
    } 
    
    void connection_info(struct sockaddr_in &client)
    { 
        char *connected_ip = inet_ntoa(client.sin_addr);
        int port = ntohs(client.sin_port); 
    
        st::cout << "-[IP:" << connected_ip << ", Connected on PORT:" << port << "]" << std::endl; 
    } 
    
    bool communication(SOCKET client)
    { 
        std::cout << "[---------------{CHAT}---------------]" << std::endl; 
        int bytes_in; 
        int bytes_out; 
        char recvd_text[81]; 
        std::string send_text; 
    
        while(true)
        { 
            std::cout << "-[SERVER]: "; 
            std::getline(std::cin,send_text); 
            if (send_text.length() > 0 )
            { 
                bytes_out = send(client, send_text.c_str(), send_text.length()+1, 0); 
                std::cout << std::endl; 
                if (bytes_out == SOCKET_ERROR)
                { 
                    std::cout << "-[SERVER error in sending.]" << std::endl; 
                    break; 
                } 
            } 
    
            bytes_in = recv(client, recvd_text, sizeof(recvd_text)-1, 0); 
            if (bytes_in == SOCKET_ERROR)
            { 
                std::cout << "-[CLIENT closed unexpectedly.]" << std::endl; 
                break; 
            } 
            else if (bytes_in == 0)
            { 
                std::cout << "-[CLIENT has disconnected.]" << std::endl; 
                break; 
            } 
            else
            {
                recvd_text[bytes_in] = 0;
                std::cout << "-[CLIENT]: " << recvd_text << std::endl;  //output on screen 
            }
        } 
        return true; 
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

On one Linux Server running Apache and PHP 5, we have multiple Virtual Hosts
We have 3 identical HP DL380 G5 server here, one of them is running
I have a server running Gentoo 2.6.12 r6 and I want to run Apache
I have a production server running with the following flag: - XX:+HeapDumpOnOutOfMemoryError Last night
I have an OLD server running DG/UX that will in the near future be
I have a build server running CruiseControl.NET. It works well for the 7 projects
We have SQL Server 2005 running on Win2003/64, and we push it hard. We
I have a headless server running Windows Server 2003, and administer it via VNC.
I have a IIS server running on Windows Server 2003. The server hosts multiple
We have a CruiseControl server running various AntUnit, MbUnit and NUnit tests via Ant.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.