Hello Hello Hello. At the moment i am working on contacting my web server through my browser. I have got it working. I can communicate with my web server by typing http://localhost into my browser.

// fakewebserver.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
	using namespace std;
	 
	#include <winsock2.h>
	#include <string.h>
	 
	#define LISTEN_PORT 80
	#define BACKLOG 5
	#define PROTOCOL "HTTP/1.1"

int _tmain(int argc, _TCHAR* argv[]){
	 while(true){

		   WSAData wsadata;	    if(WSAStartup(MAKEWORD(2,2), &wsadata) != NO_ERROR){
	        cout<<"Error at WSAStartup\n";
	        return 0;
	    }

	    SOCKET listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	    if(listenSocket == INVALID_SOCKET){
	        cout<<"Error opening socket: "<<WSAGetLastError()<<endl;
	        WSACleanup();
	        return 0;
	    }
	 
	    sockaddr_in serverService;
	    serverService.sin_family = AF_INET;
	    serverService.sin_addr.s_addr = INADDR_ANY;
	    serverService.sin_port = htons(LISTEN_PORT);

	    if(bind(listenSocket, (SOCKADDR*)&serverService, sizeof(serverService)) == SOCKET_ERROR){
	        cout<<"Error at bind: "<<WSAGetLastError()<<endl;
	        closesocket(listenSocket);
	        WSACleanup();
	        return 0;
	    }	 
	    if(listen(listenSocket, BACKLOG) == SOCKET_ERROR){
	        cout<<"Error at listen: "<<WSAGetLastError()<<endl;
        closesocket(listenSocket);
	        WSACleanup();
        return 0;
	    }
	    cout<<"Server connected\n\n";
	    while(true){
	 
	        struct sockaddr_in from;

		
		

	        int fromLen = sizeof(from);
        SOCKET msgSocket = accept(listenSocket, (struct sockaddr*)&"66.183.109.234", &fromLen);
	        if(msgSocket == INVALID_SOCKET){
	            cout<<"Error at accept: "<<WSAGetLastError()<<endl;
	            closesocket(listenSocket);
	            WSACleanup();
	            return 0;
	        }
	 
	        cout<<"Clinet: address "<<inet_ntoa(from.sin_addr)<<" port: "<<ntohs(from.sin_port)<<endl;
 
	        char HTML[] = "<HTML><TITLE></TITLE><BODY><H1>Hi!</H1></BODY></HTML>";
	        char sendBuff[1000];
	        char recvBuff[1000];
	        int bytesSent = 0;
	        int bytesRecv = 0;
	 
	         
	        //recieve from client
	        bytesRecv = recv(msgSocket, recvBuff, (int)strlen(recvBuff), 0);
			
			// istream in(msgSocket);

			cout<<bytesRecv;

	        if(bytesRecv == SOCKET_ERROR){
	            cout<<"Server, error at recv() "<<WSAGetLastError()<<endl;
	            closesocket(listenSocket);
	            WSACleanup();	            return 0;
        }
        recvBuff[strlen(recvBuff)] = '\0';	 
        //send response to client
	        cout<<"Server recieved message: "<<recvBuff<<" from Client address: "<<inet_ntoa(from.sin_addr)<<" port: "<<ntohs(from.sin_port)<<"\nbytes recv: "<<bytesRecv<<endl;
	         
	        sprintf(sendBuff, "%s %d %s", PROTOCOL, 200, "OK");
	        send(msgSocket, sendBuff, (int)strlen(sendBuff), 0);
	        cout<<"Server sent message: "<<sendBuff<<endl;
	        //send content
	         
	        send(msgSocket, HTML, (int)strlen(HTML), 0);
	        cout<<"Server sent message: "<<HTML<<endl;
	 
	        closesocket(msgSocket);
	    }
	    closesocket(listenSocket);
	    WSACleanup();


	    return 0;


	  }
}

this will output Clinnet : address 127.0.0.1 port : 4943

I know that 127.0.0.1 is the loop back address. Why cant i type in my external ip address into the browser. I read somewhere that i need to set up a dns sub domain, for this to work. It kind of makes sense to me. Can anyone tell me what i cant connect to my web server with with external ip address?
thanks

Edited 4 Years Ago by jonnyboy12: n/a

This article has been dead for over six months. Start a new discussion instead.