Server doesn't serve!

This is a discussion on Server doesn't serve! within the Networking/Device Communication forums, part of the General Programming Boards category; When I run this program on my computer, then open an IE, then type in http://127.0.0.1/ , I recive the ...

  1. #1
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342

    Unhappy Server doesn't serve!

    When I run this program on my computer, then open an IE, then type in http://127.0.0.1/, I recive the text that I inputed to the program. But when I have my friend download it and run it, then type in http://(HIS_IP)/, I get nothing.

    Code:
     
    #include "Server2.h"
    // Some globals:
    HINSTANCE hInstance;
    LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
    char szClassName[ ] = "Win32_HTTP_Server2";
    char aa[100], bb[100];
    struct sockaddr_in A, A1;
    WSAData ws;
    long d;
    SOCKET s, s1;
    int dw;
    bool t = true;
    int i;
    void Display(HWND hParent, char szListBox[])
    {
       SendMessage(GetDlgItem(hParent,
                              IDC_REPORT_LIST),
                   LB_ADDSTRING,
                   0,
                   (LPARAM)szListBox);
       return;
    }
    int Init(HWND hwnd)
    {
       if(t == true) t = false;
       else{ Display(hwnd, "This program is already serving.");
       return TRUE;  }
       Display(hwnd, "Starting up winsock...");
       d = WSAStartup(0x0101, &ws);
       sprintf(aa, "Winsock startup error code %ld.", d);
       Display(hwnd, aa);
       Display(hwnd, "Creating stream socket...");
       s = socket(AF_INET, SOCK_STREAM, 0);
       sprintf(aa, "Stream socket creation error code %ld.", s);
       Display(hwnd, aa);
       GetWindowText(GetDlgItem(hwnd, IDC_PORT_NUMBER), aa, 100);
       int nPort = atoi(aa);
       Display(hwnd, "Binding stream socket to port...");
       A.sin_family = AF_INET;
       A.sin_port = htons(nPort);
       A.sin_addr.s_addr = INADDR_ANY;
       d = bind(s, (struct sockaddr *)&A, sizeof(A));
       sprintf(aa, "Stream socket binding error code %ld.", d);
       Display(hwnd, aa);
       Display(hwnd, "Listening for acceptance...");
       d = listen(s, 100);
       sprintf(aa, "Listen error code %ld.", d);
       Display(hwnd, aa);
       WSAAsyncSelect(s, hwnd, WM_USER + 1, FD_READ | FD_WRITE | FD_ACCEPT | FD_CLOSE);
       Display(hwnd, "Ready to start serving.");
       return TRUE;
    }
    // Main funtion:
    int WINAPI WinMain (HINSTANCE hThisInstance,
                        HINSTANCE hPrevInstance,
                        LPSTR lpszArgument,
                        int nFunsterStil)
    {
        HWND hwnd;               /* This is the handle for our window */
        MSG messages;            /* Here messages to the application are saved */
        WNDCLASSEX wincl;        /* Data structure for the windowclass */
        /* The Window structure */
        wincl.hInstance = hThisInstance;
        wincl.lpszClassName = szClassName;
        wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
        wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
        wincl.cbSize = sizeof (WNDCLASSEX);
        /* Use default icon and mouse-pointer */
        wincl.hIcon = LoadIcon (hThisInstance, "A");
        wincl.hIconSm = LoadIcon (hThisInstance, "A");
        wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
        wincl.lpszMenuName = NULL;                 /* No menu */
        wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
        wincl.cbWndExtra = 0;                      /* structure or the window instance */
        /* Use Windows's default color as the background of the window */
        wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
        /* Register the window class, and if it fails quit the program */
        if (!RegisterClassEx (&wincl))
            return 0;
        int winX = 425;
        int winY = 270;
        /* The class is registered, let's create the program*/
        hwnd = CreateWindowEx (
               0,                   /* Extended possibilites for variation */
               szClassName,         /* Classname */
               "HTTP Server",       /* Title Text */
               WS_OVERLAPPEDWINDOW&~WS_THICKFRAME&~WS_MAXIMIZEBOX,
               GetSystemMetrics(SM_CXSCREEN)/2-winX/2,       /* Windows decides the position */
               GetSystemMetrics(SM_CYSCREEN)/2-winY/2,       /* where the window ends up on the screen */
               winX,                 /* The programs width */
               winY,                 /* and height in pixels */
               HWND_DESKTOP,        /* The window is a child-window to desktop */
               NULL,                /* No menu */
               hThisInstance,       /* Program Instance handler */
               NULL                 /* No Window Creation data */
               );
        /* Make the window visible on the screen */
        ShowWindow (hwnd, nFunsterStil);
        /* Run the message loop. It will run until GetMessage() returns 0 */
        while (GetMessage (&messages, NULL, 0, 0))
        {
            /* Translate virtual-key messages into character messages */
            TranslateMessage(&messages);
            /* Send message to WindowProcedure */
            DispatchMessage(&messages);
        }
        /* The program return-value is 0 - The value that PostQuitMessage() gave */
        return messages.wParam;
    }
    
    /*  This function is called by the Windows function DispatchMessage()  */
    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        switch (message)                  /* handle the messages */
        {
            case WM_CREATE:
            {
                CreateWindowEx( WS_EX_CLIENTEDGE,
                                WC_LISTBOX,
                                "Display_ListBox",
                                WS_CHILD | WS_VISIBLE | WS_VSCROLL,
                                10,
                                10,
                                400,
                                200,
                                hwnd,
                                (HMENU)IDC_REPORT_LIST,
                                0,
                                NULL);
                SendMessage( GetDlgItem( hwnd, IDC_REPORT_LIST), WM_SETFONT,
                             (WPARAM)GetStockObject(DEFAULT_GUI_FONT),
                             MAKELPARAM(TRUE, 0) );
                CreateWindowEx( WS_EX_CLIENTEDGE,
                                WC_EDIT,
                                "",
                                WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL,
                                10,
                                210,
                                250,
                                20,
                                hwnd,
                                (HMENU)IDC_SEND_TEXT,
                                0,
                                NULL);
                SendMessage( GetDlgItem( hwnd, IDC_SEND_TEXT), WM_SETFONT,
                             (WPARAM)GetStockObject(DEFAULT_GUI_FONT),
                             MAKELPARAM(TRUE, 0) );
                CreateWindowEx( WS_EX_CLIENTEDGE,
                                WC_EDIT,
                                "80",
                                WS_CHILD | WS_VISIBLE | ES_NUMBER | ES_AUTOHSCROLL,
                                270,
                                210,
                                40,
                                20,
                                hwnd,
                                (HMENU)IDC_PORT_NUMBER,
                                0,
                                NULL);
                SendMessage( GetDlgItem( hwnd, IDC_PORT_NUMBER), WM_SETFONT,
                             (WPARAM)GetStockObject(DEFAULT_GUI_FONT),
                             MAKELPARAM(TRUE, 0) );
                CreateWindowEx( 0,
                                WC_BUTTON,
                                "Init",
                                WS_CHILD | WS_VISIBLE,
                                320,
                                205,
                                90,
                                30,
                                hwnd,
                                (HMENU)IDC_INIT_BUTTON,
                                0,
                                NULL);
                SendMessage( GetDlgItem( hwnd, IDC_INIT_BUTTON), WM_SETFONT,
                             (WPARAM)GetStockObject(DEFAULT_GUI_FONT),
                             MAKELPARAM(TRUE, 0) );
            }
            break;
            case WM_USER + 1:
            {
                if((LOWORD(lParam)&FD_ACCEPT) == FD_ACCEPT)
                {
                    Display(hwnd, "Accepting...");
                    dw = sizeof(A1);
                    s1 = accept(s, (struct sockaddr *)&A1, &dw);
                    if(s1){
                    Display(hwnd, "Accepted successfully.");  }
                    else{
                    sprintf(aa, "Acception failed, error code %ld.", s1);
                    Display(hwnd, aa);
                    return 0;  }
      
                    char *p = inet_ntoa(A1.sin_addr);
                    sprintf(aa, "Now serving address %s.", p);
                    Display(hwnd, aa);
                }
                if((LOWORD(lParam)&FD_READ) == FD_READ)
                {
      
                    sprintf(aa, "Reciving from client on port %d.", wParam);
                    Display(hwnd, aa);
                    recv(wParam, bb, 1000, 0);
                    if(bb[0] == 'G') Display(hwnd, "Recived a get command from the client.");
                    else if(bb[0] == 'P') Display(hwnd, "Recived a post command from the client.");
                    else Display(hwnd, "Recived an unknown command from the client.");
                    char sz[1000];
                    Display(hwnd, "Sending data back to client.");
                    GetWindowText(GetDlgItem(hwnd, IDC_SEND_TEXT), sz, 1000);
                    send(wParam, sz, strlen(sz) + 1, 0);
                    for(i = 0; i < strlen(sz); i++) send(wParam, "\0\0", 1, 0);
                    Display(hwnd, "Data sent back to client.");
                    closesocket(wParam);
                }
                if((LOWORD(lParam)&FD_CLOSE) == FD_CLOSE)
                {
                    Display(hwnd, "Client sent a close message.");
                }
                if((LOWORD(lParam)&FD_WRITE) == FD_WRITE)
                {
                    Display(hwnd, "Client sent a write message.");
                }
                if((LOWORD(lParam)&FD_CONNECT) == FD_CONNECT)
                {
                    Display(hwnd, "Client sent a connect message.");
                }
            }
            break;
            case WM_COMMAND:
            {
                switch(LOWORD(wParam))
                {
                    case IDC_INIT_BUTTON:
                    {
                        Init(hwnd);
                    }
                }
                break;
            }
            break;
            case WM_DESTROY:
            {
                WSACleanup();
                PostQuitMessage (0);
            }
            break;
            default:
            return DefWindowProc (hwnd, message, wParam, lParam);
        }
        return 0;
    }
    Why is that the case? I read on so many web sites that if it works by entering 127.0.0.1, then it will work any where else. But that's not how it is on my program.

    WHY!?

    BTW: Becuase my friend is on dial-up too, He gave me his IP by going through http://www.whatsmyip.net/.

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    possible firewall

    what port?

    Kuphryn

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,171
    I'm failing to see where your friend comes into the picutre. Does it work when you use your NIC's IP address instead of 127.0.0.1 on your computer? Does it work when your friend does 127.0.0.1?
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    >> possible firewall

    Both firewall's are allowing the program's full access.

    >> what port?

    Both on 80 (Defualt HTTP).

    >> I'm failing to see where your friend comes into the picutre.

    He comes in by helping me test out the program.

    >> Does it work when you use your NIC's IP address instead of 127.0.0.1 on your computer?

    What does NIC stand for?

    >> Does it work when your friend does 127.0.0.1?

    Yes.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,171
    >> What does NIC stand for?
    Network Interface Card (e.g. the IP address your computer uses to talk to other computers. Do ipconfig at the command line if you don't know what it is.)

    When you say you get nothing, do you mean you get a server not found error? 401 error? Have you tried using telnet to connect to port 80 on his computer?
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    >> >> Does it work when you use your NIC's IP address instead of 127.0.0.1 on your computer?

    Okay, yes it does.

    >> When you say you get nothing, do you mean you get a server not found error? 401 error?

    Right, Server not found.

    >> Have you tried using telnet to connect to port 80 on his computer?

    No. But I want this to work using a browser not telnet.

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,171
    >> No. But I want this to work using a browser not telnet.

    It's called troubleshooting. Telnet is typically a good tool to use for web server testing to see WTF is going on. Do you also avoid running your program through a debugger to help troubleshoot just because you want it to work outside the debugger when it's finished?

    Have you tried a traceroute to your friend's computer? Maybe there's a router firewall that needs to be set up. Try a port scan of your friend's computer when he's running the web server.

    It's going to be hard if you're unwilling to use anything other than a browser to test this thing...
    If you understand what you're doing, you're not learning anything.

  8. #8
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,109
    >>No. But I want this to work using a browser not telnet.

    Telnet is a standard tool that allows you to test all layers of the OSI network model, if telnetting doesn't work then your application probably won't work as well.

    So take his advice and try to telnet first ( be sure to have your friends firewall allow connections incoming at port 23, since that is where telnet is mapped to ).

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,171
    I actually meant for him to connect to port 80 of his friend's computer using telnet. I'm not interested in port 23.

    You can use telnet to do HTTP requests:
    itsme@itsme:~$ telnet google.com 80
    Trying 64.233.167.99...
    Connected to google.com.
    Escape character is '^]'.
    GET / HTTP/1.0

    HTTP/1.0 200 OK
    Cache-Control: private
    Content-Type: text/html
    Set-Cookie: PREF=ID=4fc7d53830741209:TM=1154469427:LM=11544694 27:S=42ocnPeHKEXdoOu0; expires=Sun, 17-Jan-2038 19:14:07 GMT; path=/; domain=.google.com
    Server: GWS/2.1
    Date: Tue, 01 Aug 2006 21:57:07 GMT
    Connection: Close

    <html><head><meta http-equiv="content-type" content="text/html; charset=ISO-8859-1"><title>Google</title><style><!--
    ...
    ...
    ...
    /en/about.html>About Google</a></font><p><font size=-2>&copy;2006 Google</font></p></center></body></html>Connection closed by foreign host.
    itsme@itsme:~$
    Last edited by itsme86; 08-01-2006 at 04:58 PM.
    If you understand what you're doing, you're not learning anything.

  10. #10
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    set up XAMPP on the computer you want to use as a server, then try and connect to that... it only takes a couple mins to set up and if you can't connect to that, then no webserver is going to work without the ports being forwarded right.

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,750
    > Server doesn't serve!
    And I was so looking forward to testing your new tennis simulator
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I was hoping it would bring me a drink.


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    *Looking for XAMMP* ... @@! Big file. I'll just try telnet.
    Friend is busy now, so I'll have to try that later.
    But thanks for the help so far.
    Now I am hoping telnet won't work. (It'll show me that it's not my program that's defective.)

  14. #14
    User
    Join Date
    Jan 2006
    Location
    Canada
    Posts
    498
    > Server doesn't serve!
    And I was so looking forward to testing your new tennis simulator
    Salem.... you are the funniest mod I've ever seen.....

    Now I am hoping telnet won't work. (It'll show me that it's not my program that's defective.)
    Oh, really? If Telnet works, at least you know that something's working. If you can, try connecting to your server over a LAN instead of WAN (internet). There may be a proxy or something other than a firewall that's blocking you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Serve image with C++ http server
    By seaders in forum C++ Programming
    Replies: 0
    Last Post: 06-11-2008, 06:40 AM
  2. Server Architecture
    By coder8137 in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-29-2008, 11:21 PM
  3. Where's the EPIPE signal?
    By marc.andrysco in forum Networking/Device Communication
    Replies: 0
    Last Post: 12-23-2006, 08:04 PM
  4. IE 6 status bar
    By DavidP in forum Tech Board
    Replies: 15
    Last Post: 10-23-2002, 06:31 PM
  5. socket question
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 02:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21