6 Replies - 242 Views - Last Post: 01 September 2019 - 09:52 PM Rate Topic: -----

#1 prako2   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 01-September 19

How to use exception handling?

Posted 01 September 2019 - 02:14 PM

I have a code which gets public IP address and one segment of code causes crash if not connected to internet. So I'm trying to apply exception handling, but I don't really understand how it works in this case, can you help me?

#include <string.h>
#include <winsock2.h>
#include <windows.h>
#include <iostream>
#include <vector>
#include <locale>
#include <sstream>
using namespace std;
#pragma comment(lib,"ws2_32.lib")


string website_HTML;
locale local;
void get_Website(string url);
char lineBuffer[200][80] = { ' ' };
char buffer[10000];
char ip_address[16];
int i = 0, bufLen = 0, j = 0, lineCount = 0;
int lineIndex = 0, posIndex = 0;

//****************************************************

int main(void) {
	cout << "\n\n\n";
	try{
	get_Website("api.ipify.org");
	}
	catch (const char* error)
	{

	}
	for (size_t i = 0; i < website_HTML.length(); ++i) website_HTML[i] = tolower(website_HTML[i], local);

	istringstream ss(website_HTML);
	string stoken;

	while (getline(ss, stoken, '\n')) {

		//cout <<"-->"<< stoken.c_str() << '\n';

		strcpy_s(lineBuffer[lineIndex], stoken.c_str());
		int dot = 0;
		for (int ii = 0; ii < strlen(lineBuffer[lineIndex]); ii++) {

			if (lineBuffer[lineIndex][ii] == '.') dot++;
			if (dot >= 3) {
				dot = 0;
				strcpy_s(ip_address, lineBuffer[lineIndex]);
			}
		}

		lineIndex++;
	}
	cout << "Your IP Address is  " << ip_address << " \n\n";


	cout << "\nPress ANY key to close.\n\n";
	cin.ignore(); cin.get();

	return 0;
}

//****************************************************

void get_Website(string url) {
	WSADATA wsaData;
	SOCKET Socket;
	SOCKADDR_IN SockAddr;
	int lineCount = 0;
	int rowCount = 0;
	struct hostent *host;
	string get_http;


	get_http = "GET / HTTP/1.1\r\nHost: " + url + "\r\nConnection: close\r\n\r\n";

	if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
		cout << "WSAStartup failed.\n";
		system("pause");
		//return 1;
	}

	Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	host = gethostbyname(url.c_str());

	SockAddr.sin_port = htons(80);
	SockAddr.sin_family = AF_INET;
	SockAddr.sin_addr.s_addr = *((unsigned long*)host->h_addr);

	if (connect(Socket, (SOCKADDR*)(&SockAddr), sizeof(SockAddr)) != 0) {
		cout << "Could not connect";
		system("pause");
		//return 1;
	}
	send(Socket, get_http.c_str(), strlen(get_http.c_str()), 0);

	int nDataLength;
	while ((nDataLength = recv(Socket, buffer, 10000, 0)) > 0) {
		int i = 0;
		while (buffer[i] >= 32 || buffer[i] == '\n' || buffer[i] == '\r') {

			website_HTML += buffer[i];
			i += 1;
		}
	}

	closesocket(Socket);
	WSACleanup();

}


Is This A Good Question/Topic? 0
  • +

Replies To: How to use exception handling?

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7056
  • View blog
  • Posts: 23,995
  • Joined: 05-May 12

Re: How to use exception handling?

Posted 01 September 2019 - 02:35 PM

What exception are you getting? On what line is it being thrown?
Was This Post Helpful? 0
  • +
  • -

#3 prako2   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 01-September 19

Re: How to use exception handling?

Posted 01 September 2019 - 02:38 PM

https://imgur.com/a/FL8KFm2
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7056
  • View blog
  • Posts: 23,995
  • Joined: 05-May 12

Re: How to use exception handling?

Posted 01 September 2019 - 03:19 PM

In this particular case, you don't need exception handling. You simply need error handling. Check to see if host is null after you call getbyhostname(). This is just like the way you were (trying to) do error handling when WSAStartup() or connect() fails.

Out of curiosity, why did you comment out out the returns in those error handling cases? Why commenting out the return, the code flow would continue to execute the succeeding lines, but you are already in a bad state to successfully continue on. If WSAStartup() fails, then you won't be able to call any of the Windows Sockets API like connect(), socket(), gethostbyname(), send(), recv(), etc.. If connect() fails, then you won't be able to send() or recv() because you won't have a valid socket handle.
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7056
  • View blog
  • Posts: 23,995
  • Joined: 05-May 12

Re: How to use exception handling?

Posted 01 September 2019 - 03:32 PM

None of this code is for the .NET Framework C++/CLI (CLI == Common Language Interface). This looks to be just standard C++, and the OP is thinking of CLI == Command Line Interface.

Moving the thread...
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg   User is online

  • member icon

Reputation: 5761
  • View blog
  • Posts: 17,640
  • Joined: 25-December 09

Re: How to use exception handling?

Posted 01 September 2019 - 06:23 PM

Also here.
Was This Post Helpful? 2
  • +
  • -

#7 Salem_c   User is online

  • void main'ers are DOOMED
  • member icon

Reputation: 2381
  • View blog
  • Posts: 4,516
  • Joined: 30-May 10

Re: How to use exception handling?

Posted 01 September 2019 - 09:52 PM

It's out of control!
http://forums.codegu...eption-handling
http://www.cplusplus...windows/261322/
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1