Webserver inaccessible

This is a discussion on Webserver inaccessible within the Networking/Device Communication forums, part of the General Programming Boards category; I am coding a webserver. At this moment, all the program does is accept connections on Port 80 and print ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    23

    Webserver inaccessible

    I am coding a webserver. At this moment, all the program does is accept connections on Port 80 and print out something to the console every time a connection is made.
    Code:
    #include<windows.h>
    #include<stdio.h>
    #include<wininet.h>
    #define MAX_CLIENTS (0x100)
    #define MAX_TEXT (0x1000)
    DWORD WINAPI httpThreadProc(void*data)
    {
     char a[MAX_TEXT];
     SOCKET mySocket=*(SOCKET*)data;
     printf("%X\n",mySocket);
    } 
    SOCKET acceptNext(SOCKET sock,sockaddr*addr,int*addrLength)
    {
     for(SOCKET s=SOCKET_ERROR;s==SOCKET_ERROR;s=accept(sock,addr,addrLength));
     return s;
    }
    int main(char*args[])
    {
     sockaddr_in service;
     WSAData wsaData;
     WSAStartup(MAKEWORD(2,2),&wsaData);
     SOCKET lSock;
     service.sin_family=AF_INET;
     service.sin_port=htons(INTERNET_DEFAULT_HTTP_PORT);
     service.sin_addr.s_addr=inet_addr(MY_IP);
     lSock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
     bind(lSock,(SOCKADDR*)&service,sizeof(service));
     printf("%X\n",listen(lSock,MAX_CLIENTS));
     for(;;)
     CreateThread(NULL,0,httpThreadProc,(void*)new SOCKET(acceptNext(lSock,NULL,NULL)),0,NULL);
     system("PAUSE");
    }
    The server is only accessible through my computer. If I enter my IP into the address bar, it displays an error, as I expect, but the console outputs a number. When others enter my IP, the console does not register it.

    I have disabled my firewall and DHCP. How do I allow others to access my server?
    Last edited by madmardigan53; 05-14-2005 at 01:22 PM. Reason: no return

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Are you on a local network? Did you give out your external IP address and foreward port 80 to your local computer.
    There are a few things with this code. First of all despite the way it's define LPVOID lpParameter needn't be a pointer, you can just pass the actual socket descriptor and declare you thread function like this
    Code:
    DWORD WINAPI httpThreadProc(SOCKET MySocket)
    of course you still need the use appropriate casting.
    Next your acceptNext function dosen't return a value and it dosen't seam necercary either.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    23
    I see what you mean about acceptNext not returning anything. However, this would not solve my problem. I do not know what you mean by "external IP address" or "forward port 80 to [my] local computer." I do know that my computer is not connected to any other locally.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    How do you connect to the internet? If you have a router then you likely have an internal IP and an external IP, if that is the case you will need to log into your router and foreward the port you want (80 for a webserver) to the internal IP. If your IP is in one of these ranges then you have an internal IP
    10.0.0.0 - 10.255.255.255
    172.16.0.0 - 172.31.255.255
    192.168.0.0 - 192.168.255.255

    To find your external IP go to a website like www.whatsmyip.org

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    23
    I connect to the Internet through a cable modem from Charter. The IP I get from whatismyip.org is the same as what I get through ipconfig, which is 66.214.2.99.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    hmm that should be working. Maybe your ISP blocks that port? or your modem has a built in firewall of some kind?
    Last edited by Quantum1024; 05-14-2005 at 06:47 PM.

  7. #7
    Registered User
    Join Date
    Jun 2003
    Posts
    23
    I have tested the code on two other computers. My friend's computer had the same problem, but on a computer at my high school, another computer on the same network could access the site.
    Here is the updated code. Yes, I know that the HTTP and FTP are not completely functional.
    Code:
    #include<windows.h>
    #include<stdio.h>
    #include<wininet.h>
    #include<sys/types.h>
    #include<sys/stat.h>
    #define MAX_CLIENTS (0x100)
    #define MAX_TEXT (0x1000)
    #define sendString(sock,a) send(sock,a,strchr(a,'\n')-a+1,0)
    char*MY_IP;
    DWORD WINAPI ftpThreadProc(SOCKET mySocket)
    {
     char a[MAX_TEXT],b[MAX_TEXT],c[MAX_TEXT],directory[MAX_TEXT],userName[MAX_TEXT];
     long x,y;
     FILE*file;
     sockaddr_in service;
     service.sin_family=AF_INET;
     service.sin_addr.s_addr=inet_addr(MY_IP);
     service.sin_port=htons(INTERNET_DEFAULT_FTP_PORT);
     strcpy(directory,"/");
     printf("%X\n",mySocket);
     sendString(mySocket,"220 This is the FTP server.\n");
     for(;;)
     {
      recv(mySocket,a,MAX_TEXT,0);
      *strchr(a,'\n')=0;
      puts(a);
      sscanf(a,"%s %s",c,b);
      if(!(stricmp(c,"PWD")&&stricmp(c,"XPWD")))
      {
       sprintf(a,"257 %s\n",directory);
       sendString(mySocket,a);
      }
      else if(!stricmp(c,"USER"))
      {
       strcpy(userName,b);
       sprintf(a,"230 Login successful, %s.\n",userName);
       sendString(mySocket,a);
      }
      else if(!stricmp(c,"LIST"))
      {
       if(!*b)
       {
        SOCKET dataSocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
        sendString(mySocket,"150 Opening data connection\n");
        connect(dataSocket,(SOCKADDR*)&service,sizeof(service));
        sendString(dataSocket,"This is a file.\n");
        closesocket(dataSocket);
        sendString(mySocket,"226 Closing data connection\n");
       }
      }
      else if(!stricmp(c,"PORT"))
      {
       sscanf(b,"%n,%n,%n,%n,%n,%n",&service.sin_addr.s_addr,&service.sin_addr.s_addr+1,&service.sin_addr.s_addr+2,&service.sin_addr.s_addr+3,&service.sin_port,&service.sin_port+1);
      }
      else
      {
       sprintf(a,"502 Like I'd implement %s.\n",c);
       sendString(mySocket,a);
      }
     }
     return 0;
    }
    DWORD WINAPI createFTPThreadsThreadProc(void*theVoid)
    {
     SOCKET lSock;
     sockaddr_in service;
     service.sin_family=AF_INET;
     service.sin_port=htons(INTERNET_DEFAULT_FTP_PORT);
     service.sin_addr.s_addr=inet_addr(MY_IP);
     lSock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
     bind(lSock,(SOCKADDR*)&service,sizeof(service));
     listen(lSock,MAX_CLIENTS);
     for(;;)
     CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ftpThreadProc,(void*)accept(lSock,NULL,NULL),0,NULL);
     return 0;
    }
    DWORD WINAPI httpThreadProc(SOCKET mySocket)
    {
     char a[MAX_TEXT],b[MAX_TEXT],c[MAX_TEXT];
     FILE*file;
     int x;
     recv(mySocket,a,MAX_TEXT,0);
     printf("%s",a);
     sscanf(a,"%s %s",c,b);
     if(!stricmp(c,"GET"))
     {
      strcpy(c,"webFiles");
      if(b[strlen(b)-1]=='/')
      strcat(b,"index.html");
      file=fopen(strcat(c,b),"rb");
      if(!file)
      {
       strcpy(b,"webFiles/notFound.html");
       file=fopen("webFiles/notFound.html","rb");
      }
      x=fread(c,1,MAX_TEXT,file);
      fclose(file);
      ZeroMemory(a,0x200);
      if(!strcmp(strrchr(b,'.')+1,"html"))
      {
       sprintf(a,"HTTP/1.1 200 OK\n\n<html><title>madmardigan53</title><body><font face=\"Courier New\">%s</font><body><html>",c);
       x=strlen(a);
      }
      else
      {
       strcpy(a,"HTTP/1.1 200 OK\n\n");
       x+=strlen(a);
       printf("%X\n",x);
       memcpy(a+strlen(a),c,x-strlen(a));
      }
      send(mySocket,a,max(x,0x200),0);
     }
     closesocket(mySocket);
     return 0;
    }
    int main(int argc,char**argv)
    {
     MY_IP=argv[1];
     WSAData wsaData;
     WSAStartup(MAKEWORD(2,2),&wsaData);
     CreateThread(NULL,0,createFTPThreadsThreadProc,NULL,0,NULL);
     SOCKET lSock;
     sockaddr_in service;
     service.sin_family=AF_INET;
     service.sin_port=htons(INTERNET_DEFAULT_HTTP_PORT);
     service.sin_addr.s_addr=inet_addr(MY_IP);
     lSock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
     bind(lSock,(SOCKADDR*)&service,sizeof(service));
     listen(lSock,MAX_CLIENTS);
     for(;;)
     CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)httpThreadProc,(void*)accept(lSock,NULL,NULL),0,NULL);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Destructor inaccessible
    By renanmzmendes in forum C++ Programming
    Replies: 5
    Last Post: 02-19-2008, 10:07 AM
  2. Webserver - Detect Type of File
    By Siphon in forum C Programming
    Replies: 4
    Last Post: 09-26-2007, 02:52 AM
  3. Advice: Webserver to local database
    By zackr in forum Networking/Device Communication
    Replies: 1
    Last Post: 12-07-2005, 07:16 PM
  4. Winsock Webserver Problem
    By RunningBon in forum Networking/Device Communication
    Replies: 4
    Last Post: 06-08-2005, 04:05 PM
  5. Personal webserver.
    By adrianxw in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 07-23-2002, 03:23 AM

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