?? windows internet programming part1.html
字號:
}
server.sin_family = AF_INET;
server.sin_port = htons(PORT); /* Remember htons() from "Conversions" section? =) */
server.sin_addr.s_addr = INADDR_ANY; /* INADDR_ANY puts your IP address automatically */
if(bind(fd,(struct sockaddr*)&server,sizeof(struct sockaddr)) == -1) /* calls bind() */
{
MessageBox(0,"Bind Error","Error",0);
return -1;
}
if(listen(fd,BACKLOG) == -1) /* calls listen() */
{
MessageBox(0,"Listen Error","Error",0);
return -1;
}
while(1)
{
sin_size = sizeof(struct sockaddr_in);
if ((fd2 = accept(fd,(struct sockaddr *)&client,&sin_size)) == -1) /* calls accept() */
{
MessageBox(0,"Accept Error","Error",0);
return -1;
}
MessageBox(0,"Client connected to server.","Connection",0);
send(fd2,"Welcome to my server.\n",22,0); /* send to the client welcome message */
WSACleanup();
return -1;
}
}
if (message == WM_DESTROY)
PostQuitMessage(0);
return DefWindowProc(hwnd, message, wParam, lParam);
}
/* <---- SOURCE CODE ENDS HERE ----> */
This is the same code as the streaming server in bracamans tutorial, except this has been ported to windows,
the names bracaman uses in the program and the comments are the same.
4.2 BRACAMANS CLIENT EXAMPLE - WIN32
=======================================
And of course every server has to have a client...
/* <---- SOURCE CODE STARTS HERE ----> */
#include <windows.h>
#include <stdio.h>
#define PORT 3550 /* Port that will be opened */
#define MAXDATASIZE 100 /* Max number of bytes of data */
struct hostent *he; /* structure that will get information about remote host */
struct sockaddr_in server; /* server's address information */
int numbytes; /* files descriptors */
char buf[MAXDATASIZE]; /* buf will store received text */
WSADATA wsdata;
SOCKET sock;
DWORD wsock;
LRESULT CALLBACK recall (HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("Interface");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = recall;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = "Interface";
RegisterClass (&wndclass);
hwnd = CreateWindow (szAppName, // Windows Class Name
"Interface", // Windows Caption
WS_OVERLAPPEDWINDOW, // Windows Style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
200, // initial x size
200, // initial y size
0, // parent window handle
0, // parent menu handle
hInstance, // program instance handle
0); // creation parameters
ShowWindow(hwnd, iCmdShow);
while (GetMessage (&msg, NULL, 0, 0))
{
DispatchMessage (&msg);
}
return 1;
}
LRESULT CALLBACK recall (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
if (message == WM_LBUTTONDOWN)
{
WSAStartup(0x0101,&wsdata);
he = gethostbyname("localhost"); /* calls gethostbyname() */
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) /* calls socket() */
{
MessageBox(0,"Socket Error","Error",0);
return -1;
}
server.sin_family = AF_INET;
server.sin_port = htons(PORT); /* htons() is needed again */
server.sin_addr = *((struct in_addr *)he->h_addr); /* he->h_addr passes "*he"'s info to "h_addr" */
if(connect(sock,(struct sockaddr *)&server,sizeof(struct sockaddr)) == -1) /* calls connect() */
{
MessageBox(0,"Connection Error","Error",0);
return -1;
}
if ((numbytes = recv(sock,buf,MAXDATASIZE,0)) == -1) /* calls recv() */
{
MessageBox(0,"Receiving Error","Error",0);
return -1;
}
buf[numbytes]='\0';
MessageBox(0,buf,"Server Message",0); /* it prints server's welcome message =) */
WSACleanup();
return -1;
}
if (message == WM_DESTROY)
PostQuitMessage(0);
return DefWindowProc(hwnd, message, wParam, lParam);
}
/* <---- SOURCE CODE ENDS HERE ----> */
Again the code is almost the exact same as the original just ported and 1 or 2 different names for things.
We have covered all of this stuff already so theres nothing new here for you to learn except of course
for how to put it all together :).
As you can tell from the code, both the client and server are stream orientated, and what does this mean?
It means that they use TCP rather than UDP as the protocol.
________________________________________________________________________________________________________
5.0 EXPLORING THE WINSOCK
=======================================
Of course its the winsock that makes all this code work and ive already told you what functions like
htons() and inet_addr() do to numbers, but now its time to show you.
5.1 LOOKING AT INET_ADDR
=======================================
/* <---- SOURCE CODE STARTS HERE ----> */
#include <windows.h>
#include <stdio.h>
void outtie(char *p)
{
FILE *fp=fopen("c:\\inet_addr.txt","a+");
fprintf(fp,"%s\n",p);
fclose(fp);
}
long adrss;
char output[200];
LRESULT CALLBACK recall (HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("Interface");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = recall;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = "Interface";
RegisterClass (&wndclass);
hwnd = CreateWindow (szAppName, // Windows Class Name
"Interface", // Windows Caption
WS_OVERLAPPEDWINDOW, // Windows Style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
200, // initial x size
200, // initial y size
0, // parent window handle
0, // parent menu handle
hInstance, // program instance handle
0); // creation parameters
ShowWindow(hwnd, iCmdShow);
while (GetMessage (&msg, NULL, 0, 0))
{
DispatchMessage (&msg);
}
return 1;
}
LRESULT CALLBACK recall (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
if (message == WM_LBUTTONDOWN)
{
adrss = inet_addr("127.0.0.1");
sprintf(output,"Internet Address = %ld",adrss);
outtie(output);
}
if (message == WM_DESTROY)
PostQuitMessage(0);
return DefWindowProc(hwnd, message, wParam, lParam);
}
/* <---- SOURCE CODE ENDS HERE ----> */
This program is simple, in the beginning we set up the function outtie,
this function takes the char, output, and prints it to a file called inet_addr.txt in the C:\ drive.
We go all inet_addr on the IP address 127.0.0.1's ass and put the result in output.
We then call outtie(); and the result of inet_addr is put into the text file.
Taadaaaa.
Run this program and click on the client area, wait a second then close it.
Goto your C:\ drive and open the file inet_addr.txt.
The file should look something like this
[ inet_addr.txt ]
Internet Address = 537001100
and that is that.
5.2 LOOKING AT HTONS()
=======================================
/* <---- SOURCE CODE STARTS HERE ----> */
#include <windows.h>
#include <stdio.h>
long port;
char output[200];
LRESULT CALLBACK recall (HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("Interface");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = recall;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = "Interface";
RegisterClass (&wndclass);
hwnd = CreateWindow (szAppName, // Windows Class Name
"Interface", // Windows Caption
WS_OVERLAPPEDWINDOW, // Windows Style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
200, // initial x size
200, // initial y size
0, // parent window handle
0, // parent menu handle
hInstance, // program instance handle
0); // creation parameters
ShowWindow(hwnd, iCmdShow);
while (GetMessage (&msg, NULL, 0, 0))
{
DispatchMessage (&msg);
}
return 1;
}
LRESULT CALLBACK recall (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
if (message == WM_LBUTTONDOWN)
{
port = htons(80);
sprintf(output,"Port 80 = %ld",port);
MessageBox(0,output,"Port",0);
}
if (message == WM_DESTROY)
PostQuitMessage(0);
return DefWindowProc(hwnd, message, wParam, lParam);
}
/* <---- SOURCE CODE ENDS HERE ----> */
This program is very similiar to the last one but this one looks at htons() rather than inet_addr.
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -