Can I get a brief description of this C code to understand more. #include <iostr
ID: 3827236 • Letter: C
Question
Can I get a brief description of this C code to understand more.
#include <iostream>
#include "Encrypt.h"
#include <stdio.h>
#include <windows.h>
#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] = ""; //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] = ''; //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");
}
Explanation / Answer
#include <iostream>
#include "Encrypt.h"
#include <stdio.h>
#include <windows.h>
#pragma comment(lib, "WS2_32.lib")
using namespace std;
int main()
{
/*WSADATA structure holds the information about the window socket. And window socket used to create advaced internet and other nertwork applications*/
WSADATA wsaData;
/*In order to create WSADATA structure, need define version information */
WORD sockVersion = MAKEWORD(2, 2); //Version information
/*Create and initialize socket with the value of 0*/
SOCKET sock = 0; //create a socket struct
/*WSAStartup is used to initiate the window socket DDL, so the required params are socket version and pointer to the WSADATA structure.*/
/*If it's initiated successfully then it return zero otherwise return error with error message*/
if (WSAStartup(sockVersion, &wsaData) != 0) //WSAStartup is for initializing and loading windows web
{
/*If it's failed then display the error message and exit*/
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();
/*szText will assign the value which is return by generateEncryptInfor method*/
const char* szText = generateEncryptInfor();
/*Initiate nAddrLen with the value of 0*/
int nAddrLen = 0;
/*This is the way to connect socket in your local system. In order to connect socket it requires, */
/*AF_INET - address domain of the socket like Ipv4*/
/*SOCK_STREAM - is the type of socket like TCP or UDP*/
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //create a socket in local computer
/*If socket is not create or any failure occur during this process then it goes here*/
if (sock == INVALID_SOCKET) //
{
/*Display error message if unable/failed to create 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
/*connect() is used to establish the connection to the server, and the required params are, sock-socket file descriptor, (sockaddr*)&sin - address of the host and sizeof(sockaddr)) - size of socket address*/
/*If the connection successfully established then return 0 otherwise return -1*/
if (connect(sock, (sockaddr*)&sin, sizeof(sockaddr)) == -1) //initialize socket and connect to remove computer
{
/*Display error message and return 0 it will stop the process to execute further*/
cout << "connect failed!" << endl;
return 0;
}
/*Initialize required variables*/
char buffer[256] = ""; //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
/*nRecv initially assigned with 0 value and then reassign with the value of recv() method, Hence it should have some value, if so... */
if (nRecv > 0)
{
buffer[nRecv] = ''; //put an end mark at the end of received information.
cout << "receive data: " << buffer << endl; //print out the information received.
}
/*Send, received information to window socket(connected socket)*/
/*sock - connected socket information*/
/*szText - data to be transmitted*/
/*strlen(szText) - length of the data to be transmitted*/
send(sock, szText, strlen(szText), 0);
closesocket(sock); //close the connection
WSACleanup(); //clean up ws2_32.dll
system("pause");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.