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.