DO NOT DEAL WITH THIS MEMBER
I'm finally beginning to understand the Networking on Winsock. I was following this tutorial ; https://www.binarytides.com/winsock-sock...-tutorial/
But the way I'm doing is taking on reference and writing my own version of it.
I've came across a problem.
a value of type "const char " cannot be used to initialize an entity of type "char
Error Line :
PHP Code
<?php char* msg = "GET / HTTP/1.1\r\n\r\n";
And this is the part where that Variable is being used :
PHP Code
<?php // Send Data cout << "Sending Get request.." << endl;
if (send(sock, msg, strlen(msg), 0) < 0) { cout << "Failed to Send.. " << endl; getchar(); return 1; } cout << "Request Sent yo!" << endl; getchar(); return 0;
Other Information :
- IDE : Visual Studio Community.
- OS : Windows 10.
change Code char* msg = "GET / HTTP/1.1\r\n\r\n";
to Code const char* msg = "GET / HTTP/1.1\r\n\r\n";
const means the variable is not going to get modified during the execution of program, it will be read only.
Because the variable is const (constant at after initialization, but unlike constexpr doesn't have to be compile-time constant), trying to pass it to a function that takes a non-const variable would essentially remove said constant protection.
You can either call const_cast<char*>(var) to remove the respective const modifier, or simply define the original value as const char* ('auto' would do that)
DO NOT DEAL WITH THIS MEMBER
(Jul 23, 2018 11:46 AM)3DES Wrote: change
Code char* msg = "GET / HTTP/1.1\r\n\r\n";
to
Code const char* msg = "GET / HTTP/1.1\r\n\r\n";
const means the variable is not going to get modified during the execution of program, it will be read only. Dang I already tried that when the Error Occurred. It does not send the data then.
PHP Code
<?php #include "stdafx.h" #include <iostream> #include <string> #include <Windows.h> #include <stdio.h> #include <winsock.h>
#pragma comment(lib, "ws2_32.lib")
using namespace std;
int main() { WSADATA wsa; SOCKET sock; struct sockaddr_in server; const char* msg = "GET / HTTP/1.1\r\n\r\n";
// Start up Winsock cout << "Starting Winsock" << endl;
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) { cout << "Winsock Startup Failed."; return 1; }
cout << "Winsock Started." << endl; // Create a Network Socket. if (sock = socket(AF_INET, SOCK_STREAM, 0) == INVALID_SOCKET) { cout << "Server Socket was unable to start." << endl; } cout << "Socket created!" << endl; server.sin_addr.s_addr = inet_addr("www.google.com"); server.sin_family = AF_INET; server.sin_port = htons(80);
// Try to Connect to Server, Server data is defined above. if (connect(sock, (struct sockaddr*)&server, sizeof(server)) < 0) { cout << "Connection Failure" << endl; } cout << "Connected yo!" << endl; // Send Data cout << "Sending Get request.." << endl;
if (send(sock, msg, strlen(msg), 0) < 0) { cout << "Failed to Send.. " << endl; getchar(); return 1; } cout << "Request Sent yo!" << endl; getchar(); return 0; }
(Jul 23, 2018 01:14 PM)F.A.S.T Wrote: Dang I already tried that when the Error Occurred. It does not send the data then.
![[Image: J1gLfRi.png]](https://i.imgur.com/J1gLfRi.png)
PHP Code
<?php #include "stdafx.h" #include <iostream> #include <string> #include <Windows.h> #include <stdio.h> #include <winsock.h>
#pragma comment(lib, "ws2_32.lib")
using namespace std;
int main() { WSADATA wsa; SOCKET sock; struct sockaddr_in server; const char* msg = "GET / HTTP/1.1\r\n\r\n";
// Start up Winsock cout << "Starting Winsock" << endl;
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) { cout << "Winsock Startup Failed."; return 1; }
cout << "Winsock Started." << endl; // Create a Network Socket. if (sock = socket(AF_INET, SOCK_STREAM, 0) == INVALID_SOCKET) { cout << "Server Socket was unable to start." << endl; } cout << "Socket created!" << endl; server.sin_addr.s_addr = inet_addr("www.google.com"); server.sin_family = AF_INET; server.sin_port = htons(80);
// Try to Connect to Server, Server data is defined above. if (connect(sock, (struct sockaddr*)&server, sizeof(server)) < 0) { cout << "Connection Failure" << endl; } cout << "Connected yo!" << endl; // Send Data cout << "Sending Get request.." << endl;
if (send(sock, msg, strlen(msg), 0) < 0) { cout << "Failed to Send.. " << endl; getchar(); return 1; } cout << "Request Sent yo!" << endl; getchar(); return 0; }
Change this
Code if (connect(sock, (struct sockaddr*)&server, sizeof(server)) < 0) {
cout << "Connection Failure" << endl;
}
To
Code if (connect(sock, (struct sockaddr*)&server, sizeof(server)) != SOCK_ERROR) {
cout << "Connection Failure" << endl;
}
and call WSAGetLastError() to get the actual error
Selling signature space - PM if interested
hi mr fbi
DO NOT DEAL WITH THIS MEMBER
Jul 24, 2018 04:43 AM
(This post was last modified: Jul 24, 2018 05:08 AM by F.A.S.T.)
(Jul 23, 2018 04:35 PM)Mr. Kim Wrote: Change this
Code if (connect(sock, (struct sockaddr*)&server, sizeof(server)) < 0) {
cout << "Connection Failure" << endl;
}
To
Code if (connect(sock, (struct sockaddr*)&server, sizeof(server)) != SOCK_ERROR) {
cout << "Connection Failure" << endl;
}
and call WSAGetLastError() to get the actual error Thanks Mr.Kim it works. No more Connection error but It still gives an Error when sending Data.
PHP Code
<?php // Send Data cout << "Sending Get request.." << endl;
if (send(sock, msg, strlen(msg), 0) < 0) { cout << "Failed to Send.. " << WSAGetLastError() << endl; getchar(); return 1; } cout << "Request Sent yo!" << endl;
Also, How do you receive an answer from the server properly.
C++ requires you to specify const while C doesn't.
(Jul 24, 2018 04:43 AM)F.A.S.T Wrote: Also, How do you receive an answer from the server properly.
define properly
winapi, and winsock programmer
|