Network Socket Learning Notes (1)

xiaoxiao2021-03-06  14

The first step: The wsastartup function initiates use of ws2_32.dll by a process.

In all Windows Sockets functions, only start function wsastartup () and termination functions WSACLEANUP () must be used. The startup function must be the first function, and it allows you to specify the version of the Windows Sockets API and get some specific technical details for Sockets. This structure is as follows: int Pascal Far WSAStartup (Word WVersionRequested, LPWSADATA LPWSADATA); where WVersionRequested ensures that Sockets can run the DLL version, if not, return error messages. Let's take a look at the following code, look at how to call WSASTARTUP () call word wrsionRequested; // Define version information variables wsadata wsadata; // Define data information variable int err; // Define Error number variable wversionRequested = MakeWord (1, 1); // Assign the version information Err = WSAStartup (WVersionRequested, & WSADATA); / / Assign the error message IF (Err! = 0) {return; // tells the user to find the appropriate version} // Confirm Windows Sockets DLL Support 1.1 // DLL version can be higher than 1.1 // The version number returned by the system is always the minimum requirements 1.1, that is, the minimum version number IF (LobyTe (Wsadata.WVersion)! = 1 in the application and DLL! | HiByte (wsadata.wversion)! = 1) {wsacleanup (); // tells the user that the appropriate version return;} // Windows Sockets DLL is accepted, you can go to the next operation Close the function, any open And the connected SOCK_STREAM socket is reset, but the sockets that have been closed by the closesocket () function but still have not transmitted data are not affected, and the unmitted data will still be sent. The WSASTARTUO () function may be called multiple times when running, but must ensure that the value of WVersionRequested each time the call is the same.

Step 2: Establish Socket after initializing WSASTARTUP

MSDN: The Socket Function Creates A Socket That Is Bound to a Specific Service Provider.

After initializing the dynamic connection library of Winsock, you need to create a listening socket on the server side. To this end, you can call the socket () function to create this listening socket, and define the communication protocol used by this socket. This function call successfully returns the Socket object, returns invalid_socket (invoking wsagetlasterror () can be learned, all Winsocket functions can use this function to get the cause of the failure). Socket Pascal Far Socket (int AF, INT TYPE, INT Protocol) Parameters: AF: Currently only PF_INET (AF_INET); type: SOCKET type (SOCK_STREAM, SOCK_DGRAM); Protocol: Communication Agreement (if the user is not specified, set to 0);

If you want to build a socket, the second parameter TYPE should be SOCK_STREAM, such as the SOCKET of UDP (Data), should be SOCK_DGRAM. Step 3: Binding (server-side needs)

MSDN: The Bind Function Associates a local address with a socket.

Specify an address and port (port) for the server-side Socket, so that the client knows which port you want to connect to which address will be connected to this, to call the bind () function, the function call successfully returned 0 Otherwise, returning socket_error. INT Pascal Far Bind (Socket S, Const Struct Sockaddr Far * Name, Int Namelen); Paramese: S: Socket Object Name; Name: Socket's address value, this address must be the IP address of the machine in this program; Namelen: Name Length; if the user does not care about the value of the address or port, the address can be set to INADDR_Addr_any, and the port is 0, and Windows Sockets will automatically set it appropriate address and port (1024 to 5000). Thereafter, the getSockName () function can then be invoked to know the value set.

The SocketAddr above is the definition: The SockAddr structure varies depending on the protocol selected. Except for the sin * _family parameter, SockAddr Contents Are Expressed in Network Byte Order.

This structure is often

Struct sockaddr_in {

Short sin_family;

U_SHORT SIN_PORT;

Struct in_addr sin_addr;

Char sin_zero [8];

}

Replaced

IN_ADDR is a consortium

Definition as follows

TYPEDEF STRUCT IN_ADDR {UNION {struct {u_CHAR S_B1, S_B2, S_B3, S_B4;} S_UN_B; STRUCT {U_SHORT S_W1, S_W2;} S_UN_W; U_LONG S_ADDR;} S_UN;} IN_ADDR;

转载请注明原文地址:https://www.9cbs.com/read-50616.html

New Post(0)