Question: Can I get a brief description of this C code to understand more. #include #include Encrypt.h #include #include #pragma comment(lib, WS2_32.lib) using namespace std; int

Can I get a brief description of this C code to understand more.

#include

#include "Encrypt.h"

#include

#include

#pragma comment(lib, "WS2_32.lib")

using namespace std;

int main()

{

WSADATA wsaData;

WORD sockVersion = MAKEWORD(2, 2); //Version information

SOCKET sock = 0; //create a socket struct

if (WSAStartup(sockVersion, &wsaData) != 0) //WSAStartup is for initializing and loading windows web

{

cout << "initialization failed!" << endl;

exit(0); //if the return value of WSAStartup is 1, it means some error with ws2_32.dll file.

}

//string temp = generateEncryptInfor();

const char* szText = generateEncryptInfor();

int nAddrLen = 0;

sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //create a socket in local computer

if (sock == INVALID_SOCKET) //

{

cout << "failed socket!" << endl;

return 0; //if fail to create

}

sockaddr_in sin; //create a socket type programmable wed address structure sin, so function Connect can initialize the local socket struct.

sin.sin_family = AF_INET; //set the sin type

sin.sin_port = htons(4567); //set port

sin.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");//set remove computer's IP address

if (connect(sock, (sockaddr*)&sin, sizeof(sockaddr)) == -1) //initialize socket and connect to remove computer

{

cout << "connect failed!" << endl;

return 0;

}

char buffer[256] = "\0"; //create a buffer for receive data from server

int nRecv = 0; //count how many bytes received

nRecv = recv(sock, buffer, 256, 0); //recv is a function for counting how many bytes received from the server

if (nRecv > 0)

{

buffer[nRecv] = '\0'; //put an end mark at the end of received information.

cout << "receive data: " << buffer << endl; //print out the information received.

}

send(sock, szText, strlen(szText), 0);

closesocket(sock); //close the connection

WSACleanup(); //clean up ws2_32.dll

system("pause");

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!