Question: Can I get a brief description of this C code to understand more. #include Decrypt.h #include #include #include #include using namespace std; #pragma comment(lib, WS2_32.lib)
Can I get a brief description of this C code to understand more.
#include "Decrypt.h"
#include
#include
#include
#include
using namespace std;
#pragma comment(lib, "WS2_32.lib")
int main()
{
WSADATA wsaData;
WORD sockVersion = MAKEWORD(2, 2); //windows web programing database version information.
SOCKET sListen = 0; //create a socket struct
sockaddr_in sin = { 0 }; //create a local socket type programmable wed address structure sin.
sockaddr_in remoteAddr = { 0 }; //create a remove socket type programmable wed address structure remoteAddr.
char szText[] = "TCP Server Demo";
int nAddrLen = 0;
nAddrLen = sizeof(sockaddr_in); //calculate the size of sockaddr_in
sin.sin_port = htons(4567); //set the server port.
sin.sin_family = AF_INET; //set the sin type
sin.sin_addr.S_un.S_addr = INADDR_ANY; //set local IP address, normally INADDR_ANY will generate init wsa of local IP address when the program start.
if (WSAStartup(sockVersion, &wsaData) != 0) //WSAStartup is for initializing and loading windows net
{
cout << "initialization failed!" << endl;
exit(0); //if fail to initialize, exit
}
sListen = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //create a socket in local computer, use bind function to put the
//local IP address and Port to the socket object and initialize socket.
//in the web client, we use connect to initialize the socket while we use bind in web server
if (bind(sListen, (LPSOCKADDR)&sin, sizeof(sin)) == SOCKET_ERROR)
{
cout << "bind failed!" << endl;
return 0; //if fail to bind then return 0 and exit.
}
if (listen(sListen, 2) == SOCKET_ERROR) //listen is used to open the local port,2 means there can be 2 client connecting to it at the same time
{
cout << "listen failed!" << endl;
return 0;
//if fail to open the local port then return 0 and exit.
}
SOCKET sClient = INADDR_ANY;//create another socket in the server for storing the connecting instants with the clients
while (true)
{
//accept function is used for creating a TPC communication instants,
//it takes the IP address in a remote computer and a local socket as parameters to build a connection.
sClient = accept(sListen, (SOCKADDR*)&remoteAddr, &nAddrLen);
if (sClient == INVALID_SOCKET)
{
cout << "accept failed!" << endl;
continue;
}
//send function is used to send data to the client
send(sClient, szText, strlen(szText), 0);
char* buffer = new char[10240];
//create a buffer for receive data from server
int nRecv = 0; //count how many bytes received
nRecv = recv(sClient, buffer, 1024, 0); //recv is a function for counting how many bytes received from the server
generateDecryptInfor(buffer); //put the data into function for decryption
}
closesocket(sListen);
//close slisten
WSACleanup();
//release ws2_32.dll
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
