xbdev - software development
Wednesday February 21, 2024
home | contact | Support

Internet Tips - by example.

To check if the user is connected to the internet :-
	You can use the InternetGetConnectedState function

	DWORD dwFlags;
	BOOL bResult = InternetGetConnectedState(&dwFlags,0);
	// If bResult is TRUE, then the user is connected.
	// If its FALSE, then the connection has not been made.

To download a file from the internet :-
HINTERNET hInternet = ::InternetOpen(NULL,INTERNET_OPEN_TYPE_PRECONFIG,0,0,0); if(hInternet != NULL) { HINTERNET hopenfile = ::InternetOpenUrl(hInternet,"http://www.yahoo.com/main.html", "Accept: binary/*\r\nUser-Agent: gethttpfile\r\n",-1L,PRE_CONFIG_INTERNET_ACCESS, 0); if(hopenfile != NULL) { int BuffSize = 1024; char *pBuff = new char[BuffSize]; DWORD byteRead; while(::InternetReadFile(hopenfile, pBuff, BuffSize, &byteRead) && byteRead>0) { // pBuff contains part (BuffSize bytes max) of desired HTML file TRACE("%d bytes read\n", byteRead); } delete pBuff; } else { MessageBox("InternetOpenUrl failed."); } } else MessageBox("InternetOpen failed."); NOTE: 1. Add #include 2. Do not forget to link your project with "wininet.lib"
To launch the Web Browser from your application :-
ShellExecute(NULL, "open", "http://www.foo.com", NULL, NULL, SW_SHOWNORMAL);
To open a URL from your application :-
CInternetSession Session; CString sUrl = "http://codeguru.com"; CStdioFile *pFile; pFile = Session.OpenURL(sUrl);
To send an email from your application :-
ShellExecute( NULL, NULL,"mailto:somebody@somewhere.com",NULL, NULL, SW_SHOW ); This will open the default email program ( Outlook express..) and add the specified address to the Recipient's column of the email program.
To use FTP to upload and download files from the net :-
1) Add the following statement in your header file. #include "afxinet.h" 2) In your header file, add the following Member Variables CFtpConnection *m_pFtpConnection; CInternetSession m_Session; 3) In your application's initialization ( OnInitDialog or InitInstance functions), add the following lines. m_pFtpConnection = NULL; try { // Here usr is the username, pwd is the password and ftpsite.com is the name // of the ftp site which you want to connect to. m_pFtpConnection = m_Session.GetFtpConnection("ftpSite.com","usr","pwd", INTERNET_INVALID_PORT_NUMBER); } catch(CInternetException *pEx) { pEx->ReportError(MB_ICONEXCLAMATION); m_pFtpConnection = NULL; pEx->Delete(); } return TRUE; 4) To upload a file, add the following lines of code :- CFileFind Finder; CString strFileName; // Here c:\\Myfile.bmp is the name of the file that you want to upload // It neednt necessarily be a bitmap file. You can upload any file that you want to. // The CString strFileName is used so that the same name is uploaded to the ftp server. // After uploading, the file in the ftp server will have the same name as your local file. // You can also rename it to anything if(Finder.FindFile("C:\\Myfile.bmp")==TRUE) { Finder.FindNextFile(); strFileName = Finder.GetFileName(); Finder.Close(); } BOOL bUploaded = m_pFtpConnection->PutFile("C:\\Myfile.bmp",strFileName, FTP_TRANSFER_TYPE_BINARY,1); AfxMessageBox("Uploaded Successfully"); 5) To download a file from a ftp site, you can use the following code. Here the first parameter is the file in the ftp server. The 2nd parameter is the location where you want to store it in your hard disk. m_pFtpConnection->GetFile("File.ext","C:\\File.ext",TRUE,FILE_ATTRIBUTE_NORMAL, FTP_TRANSFER_TYPE_BINARY,1); 6) To close the connection m_Session.Close(); m_pFtpConnection->Close(); if(m_pFtpConnection!=NULL) delete m_pFtpConnection; Thats it. Its so simple to use FTP. Isnt it ?
To get the name of the local computer and its IP address :-
WSADATA wsaData; struct sockaddr_in sock; char hostname[128]; WSAStartup(0x0101, &wsaData); gethostname(hostname, 128); HOSTENT * lpHost = gethostbyname(hostname); memcpy(&(sock.sin_addr), lpHost->h_addr_list[0], lpHost->h_length); printf("Host Name: %s IP address : %s",hostname, inet_ntoa(sock.sin_addr)); WSACleanup();
Advert (Support Website)

Copyright (c) 2002-2024 xbdev.net - All rights reserved.
Designated articles, tutorials and software are the property of their respective owners.