CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    May 2010
    Posts
    2

    Client/Server communication over the internet.

    I have written some programs with both bsd sockets on linux and winsock in windows and have created a few client/server apps that work well. However, these programs only work over the LAN. This leads to my question.
    I don't know what I have to learn next to make my server/client programs capable to send and receive data over the internet. Are there tutorials on this?
    It seems like a lot of tutorials show how to get a client/server to communicate over LAN, but not the internet.

  2. #2
    Join Date
    Feb 2005
    Posts
    2,160

    Re: Client/Server communication over the internet.

    There's really no difference as far as the client/server is concerned. I suspect that one of the two (client or server) is behind a firewall and thus not visible to the other across the Internet.

  3. #3
    Join Date
    May 2010
    Posts
    2

    Re: Client/Server communication over the internet.

    Thanks for replying.

    Well it can't be a firewall issue, I am certain. I believe I am doing something wrong in the code.
    Here is the portion of my server code where I am filling out the SOCKADDR_IN struct:


    SOCKADDR_IN serverInf;
    serverInf.sin_family=AF_INET;
    serverInf.sin_addr.s_addr=inet_addr("The server remote IP here");
    serverInf.sin_port=htons(8888);

    if(bind(Socket,(SOCKADDR*)(&serverInf),sizeof(serverInf))==SOCKET_ERROR)
    {
    std::cout<<"Unable to bind socket.\r\n";
    WSACleanup();
    system("pause");
    return 0;
    }


    For testing sakes, if I hard-code my remote IP where it says "Remote IP Here", I get a winsock error code 40049 when I try to bind, which states that it cannot assign the requested address. I am definately not getting something critical here. What am I doing wrong? This source works fine for a LAN situation( of course given s_addr is given a local IP).

  4. #4
    Join Date
    Feb 2005
    Posts
    2,160

    Re: Client/Server communication over the internet.

    serverInf.sin_addr.s_addr should be set to INADDR_ANY to listen on any interface. You can set it to a specific address if you have more than one interface and only want to listen on that interface, but that's rare. You can't bind to an address that is on the server machine.

  5. #5
    Join Date
    May 2006
    Location
    Indonesia & Japan
    Posts
    399

    Re: Client/Server communication over the internet.

    Quote Originally Posted by hoxsiew View Post
    You can't bind to an address that is on the server machine.
    As far as i know that binding to IP address of server machine where program is running is available.

    From TCP/IP network programming perspective, there is no difference between LAN and Internet environment.
    henky
    ----------------------------------
    [email protected] is not my email address anymore...
    Jangan Pernah Menyerah

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured