Hello all this is my c++ web server that i can get working on my computer but not connecting to others for some reason. I am not on a router, just using my external ip address. If i run the server and type my external ip address into my browser it will connect to the server properly. even with all firewalls turned off it wont work, this is my code.
#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[])
{
bool t=true;
while(t){
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;
}
struct hostent *host_entry;
char server_name[40] = "216.232.99.98";
host_entry = gethostbyname(server_name);
sockaddr_in serverService;
serverService.sin_family = AF_INET;
serverService.sin_addr.s_addr = inet_addr(server_name); //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(t){
struct sockaddr_in from;
int fromLen = sizeof(from);
SOCKET msgSocket = accept(listenSocket, (struct sockaddr*)&from, &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>SENT From me!</H1></BODY></HTML>";
char sendBuff[40];
char recvBuff[40];
int bytesSent = 0;
int bytesRecv = 0;
//recieve from client
bytesRecv = recv(msgSocket, recvBuff, (int)strlen(recvBuff), 0);
cout<<"\n"; cout<<"\n"; cout<<"\n";
printf("\nRecieved data = %s ", recvBuff);
cout<<"\n"; cout<<"\n"; cout<<"\n"; cout<<"\n";
//istream in(msgSocket);
if(bytesRecv == SOCKET_ERROR){
cout<<" Server, error at recv() "<<WSAGetLastError()<<endl;
closesocket(listenSocket);
WSACleanup(); return 0;
}
}
return 0;
}
this is extremely annoying because i know its good code and its not a port forwarding issue , not a firewall issue and my code works, does anyone else know why i would not be able to contact my server from another computer? thanks