⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 windows internet programming part1.html

📁 黑客培训教程
💻 HTML
📖 第 1 页 / 共 5 页
字号:
[ WSAStartup() ]



WSAStartup() must be called every time that we start internet code in an application. This function calls

the winsock so that we can use it. Every windows winsock program begins with this function.





[ WSACleanup() ]



Like WSAStartup();, WSACleanup(); must be called in every winsock program, it however is called at the end 

of the code as opposed to the beginning like startup(); is. This function is the equivilant to unix's,

close() and shutdown() functions.



Its very important that every single WSAStartup() has a corresponding WSACleanup().





[ Socket() ]



The next important function is socket(); and is used by both protocols. This function contains 3 parameters,



1. Domain

2. Type

3. Protocol





1. Domain   - There are several values you can set domain to depending on the purpose of the socket.

              In this tutorial were going to set the domain to AF_INET in order to use internet protocols.



2. Type     - The value of type depends on what sockets you use. Remember there are 2 types of sockets,

              Streaming and Datagram. TCP is stream orientated and UDP is datagram orientated. Therefore

              if we wish to use TCP we set the type to SOCK_STREAM and if we want to use UDP we set it to

              SOCK_DGRAM



3. Protocol - This isn't important we can set it to 0.





Once we create the socket trough this function we can read and write to and from it. So anything we send to

the socket is transmitted across the internet connection and anything sent to us can be read from the connection.





[ closessocket() ]



closesocket() function is used to close a socket that you have already created.

This function has 1 parameter, the name of the socket you created.



1. Socket   - The name of the socket you declared.





This function closes the socket and once called no more reading or writing can be made to the socket.

To use this function you must first declare socket. Any-1 that try's to read or write to the socket

will recieve errors once this function is called.





[ gethostbyname() ]



This function returns the IP address of a host, for example;



gethostbyname("www.microsoft.com");



would return the IP address of the misrosoft website. This function is whats behind the DNS program and 

theres many occassions when obtaining the unknown IP address of a website can be useful in programming.





[ gethostbyaddr() ]



This function does the opposite of gethostbyname(). In this function we pass an IP address as the parameter 

and a host name is returned.





3.5 UDP SPECIFIC FUNCTIONS 

=======================================



Unlike the previous section which contained functions used by both UDP and TCP applications this section 

contains functions which are specific to UDP, that is you only use them in a program that is using UDP as 

the protocol of choice.





[ sendto() ]



The sendto() function is used to send datagrams across the connection we have established and we pass it 6

parameters.



1. Sock        	-  The socket that weve made earlier.

2. Msge           -  The information we want to send.

3. Len      	-  The length of the information we wish to send.

4. Flags      	-  This is set to 0.

5. To      	      -  The pointer to struct sockaddr.

6. Tolen      	-  The sizeof(struct sockaddr).





sendto() returns the number of bytes sent and returns -1 on error.





[ recvfrom() ]



This is sendto() 's counterpart, where we use sendto() to send information and write to a socket, we use 

recvfrom() to read from that socket and recieve information from a computer on the internet.



1. Sock      	-  The socket that weve made earlier.

2. Buf      	-  The buffer to read information into.

3. Len      	-  The maximum length of the buffer.

4. Flags      	-  This is set to 0.

5. From      	-  The pointer to struct sockaddr

6. Fromlen      	-  The pointer to an int with the value of sizeof(struct sockaddr)





recvfrom() returns the number of bytes recieved and returns -1 on error.





3.6 TCP SPECIFIC FUNCTIONS 

=======================================



Like UDP, TCP has 2 specific fuctions for sending and receiving information on the internet,

these functions look very similiar but dont have as many parameters.



[ send() ]



send() is the same as UDP's sendto() and has 4 parameters.



1. Sock        	-  The same as sendto().

2. Msge           -  The same as sendto().

3. Len      	-  The same as sendto().

4. Flags      	-  The same as sendto().





send() returns the number of bytes sent and returns -1 on error.





[ recv() ]



recv() is the same as UDP's recvfrom() and has 4 parameters.



1. Sock        	-  The same as recvfrom().

2. Msge           -  The same as recvfrom().

3. Len      	-  The same as recvfrom().

4. Flags      	-  The same as recvfrom().





recv() returns the number of bytes recieved and returns -1 on error.





Unlike UDP, TCP is a connection orientated protocol, what this means is that you first must form a 

connection with the host before you can begin sending and recieving information to one another.



TCP has several specific functions for forming this connection.





[ connect() ]



After we start up the winsock in a TCP application and set up the socket we must call this function

in order to form the connection.



It has 3 parameters.



1. Sock      	-  The socket that weve made earlier.

2. To      	      -  The pointer to struct sockaddr.

3. ToLen      	-  The sizeof(struct sockaddr).





You will recognise all of these parameters from earlier and theres nothing new to explain here.



This function is used to connect to an IP address on a defined port and returns -1 on error.





TCP again varies from UDP by requiring special functions when creating a server.

These are bind() and listen().





[ bind() ]



bind() has 3 parameters.





1. Sock      	-  The socket that weve made earlier.

2. My_addr      	-  The pointer to struct sockaddr.

3. ToLen      	-  The sizeof(struct sockaddr).





bind() is used to form a connection to a port on your own computer. All servers have specific ports,

an http servers port is 80 and an ftp servers port is 21.



With servers we must bind() to a specific port so that clients know how to talk to us. When I bind();

to port 13 all IP packets sent to port 13 on my computer are directed to my program.





[ listen() ]



listen() only contains 2 parameters.





1. Sock      	-  The socket that weve made earlier.

2. BackLog      	-  The number of connections allowed.





We use listen() when we are waiting for a connection. You must call bind() before listen() so the

program knows what port it is supposed to be listening to for packets.





3.7 STRUCTURES

=======================================



There are several structures used in winsock programming to make referencing objects that much easier.





[ sockaddr ]



You'll notice that in the last section I refered to "struct sockaddr" but what is it?



sockaddr is a c++ structure which holds 2 pieces of information about the socket.



1. Sa_family      -  The address family.

2. Sa_data[14];      -  14 bytes of the protocol address.





[ sockaddr_in ]



Another structure is sockaddr_in which we use to reference elements of the socket.



1. Sin_family     -  The address family.

2. Sin_port      	-  The Port number.

3. Sin_addr      	-  The Internet Address.





sin_family is usually set to AF_INET in most programs. It tells the program what kind of network its going

to be used for, in this case AF_INET (the internet).



sin_port is a number used for the internet connection such as 80.

sin_addr is the internet address to be used in the connection such as 127.0.0.1.





[ hostent ]



The final structure we are going to look at is hostent.

This structure is used to refer to remote hosts, like www.microsoft.com or blacksun.box.sk.



This structure is most commonly used to find the IP address or the host name of a remote computer.



We can reference information mainly by using only 1 part.



1. Hostent->h_name



This is used to get the IP number when provided the host name.





The rest of the structure isn't important right now but we may cover it later.





3.8 CONVERTING

=======================================



[ htons() ]



Theres a little problem with numbers on the internet, intel chips store them one way and motorola

chips store them a different way. So how do we make sure that our programs can work alongside macintosh

computers which use the motorola chip? There is a function called htons() which will convert the number

thankfully and its really easy to use.



This function has only 1 paramater



1. Port      	-  The port number.



It takes the port number we pass to it and converts it, saving us from annoying overhead.

We'll use htons() later when we have to fill up structures.





[ inet_addr ]



Before we can fill up our structures tough we first have to supply an IP address and in order for the

program to do this we must convert the address into an unsigned long.



To do this we pass the IP address to the function inet_addr() as its only parameter.



1. Address      -  The IP Address.





3.9 THE APPLICATION LAYER

=======================================



Mostly on the internet you'll hear things like ftp and http and how they are protocols. This is true

as what a protocol is, is a defined set of rules on how to interact, like a certain way for 2 computers

to talk to eachother.



These protocols however are not quiet the same as TCP or UDP, just as those 2 aren't the same as IP.



We can seperate and define these protocols by placing them in layers. IP is the base layer the one where

most fundamental communication takes place (getting from one place to another), and protocols like

TCP are built on top of IP so they are on a different layer. The same way FTP and HTTP are built on top

of TCP so they are on a higher level.



This can best be shown with a diagram I think (or maybe its not I just like making diagrams ;),).







      +-------------------+

      | Application Layer |      -  FTP, HTTP.

      +-------------------+

      |  Transport Layer  |      -  TCP, UDP.

      +-------------------+

      |  Protocol  Layer  |      -  IP.

      +-------------------+

      |  Hardware  Layer  |      -  Network Card.

      +-------------------+







      Fig 4. - Protocol Layering.





The Hardware Layer is of course where packets are created and everything on the network and ethernet

cards themselves.





________________________________________________________________________________________________________





4.0 CLIENTS AND SERVERS

=======================================



Since this tutorial is trying to run parallel to BracaMans unix sockets tutorial (also available from 

blacksun.box.sk).



We are going to use the same programs as in his tutorial except ported for use in windows.







4.1 BRACAMANS SERVER EXAMPLE - WIN32

=======================================





/* <---- SOURCE CODE STARTS HERE ----> */



#include <windows.h>

#include <winsock.h>

#include <stdio.h>



#define PORT 3550   /* Port that will be opened */ 

#define BACKLOG 1   /* Number of allowed connections */





struct sockaddr_in server; /* server's address information */

struct sockaddr_in client; /* client's address information */



int sin_size;





WSADATA  wsdata;

SOCKET   fd, fd2;

DWORD    wsock;





LRESULT CALLBACK recall (HWND, UINT, WPARAM, LPARAM);



int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)



{

      static TCHAR szAppName[] = TEXT("Server");



      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  = "Server";

      

      RegisterClass (&wndclass);



      hwnd = CreateWindow (szAppName,      	// Windows Class Name

      	      	 "Server",      	      // 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);  



   if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1 )  /* calls socket() */

   {

         MessageBox(0,"Socket Error","Error",0);

         return -1;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -