Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
#include <windows.h> #include <stdio.h> #include <iostream> #pragma comment( lib, "ws2_32.lib" ) class CInternet { private: SOCKET s; SOCKADDR_IN addr; WSADATA wsa; HOSTENT* Host; char URL[30]; char recv_buf[1024]; public: bool CreateConnection(char *Seite, char *Server, char* VariablenPerGet) { sprintf(URL,"http://%s",Server); if(WSAStartup(MAKEWORD(2,0),&wsa)) { std::cout << "WinSock nicht gestartet." << std::endl; return false; } addr.sin_family=AF_INET; addr.sin_port=htons(80); std::cout << "WinSock gestartet." << std::endl; if(!(Host=gethostbyname(URL))) { std::cout << "Host nicht gefunden." << std::endl; return false; } strncpy((char*)&addr.sin_addr.s_addr,Host->h_addr,4); //IP Adresse in die Adresse des Servers kopieren std::cout << "Host gefunden." << std::endl; if(addr.sin_addr.s_addr==INADDR_NONE) //Ist es eine Richtige IP adresse? { std::cout << "IP Adresse fehlerhaft." << std::endl; return false; } std::cout << "Richtigen Host gefunden." << std::endl; if((s=socket(AF_INET,SOCK_STREAM,0))==INVALID_SOCKET) //Socket erstellen { std::cout << "Sockel konnte nicht erstellt werden." << std::endl; return false; } std::cout << "Socket erstellt." << std::endl; if( connect(s,(SOCKADDR*)&addr,sizeof(SOCKADDR))) //Verbindung aufbauen { std::cout << "Verbindung nicht aufgebaut." << std::endl; return false; } std::cout << "Verbindung aufgebaut." << std::endl; char send_buf[1024]; sprintf(send_buf,"GET /%s HTTP/1.1\nHost: %s\nUser-Agent: 3D Blobby Space\n\n%s", Seite, Server, VariablenPerGet); if((send(s,send_buf,strlen(send_buf),0))<strlen(send_buf)) { std::cout << "Senden fehlgeschlagen." << std::endl; return false; } return true; } char* GetText() { if(recv(s,recv_buf,1024,0)<0) return ""; return recv_buf; } bool CloseConnection() { closesocket(s); WSACleanup(); return true; } }; int main(int args, char** argv) { CInternet* test = new CInternet(); char* host = "floclan.fl.funpic.de"; char* seite = "index.html"; char* var = ""; char input[1024]; test->CreateConnection(seite, host, var); char* text = test->GetText(); std::cout << text << std::endl; std::cin >> input; delete test; return 0; } |
C-/C++-Quelltext |
|
1 |
new CInternet();
|
C-/C++-Quelltext |
|
1 |
new CInternet;
|
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
#include <windows.h> #include <cstdio> #include <iostream> #include <sstream> #pragma comment( lib, "ws2_32.lib" ) class Internet { ::SOCKET m_socket; public: bool create_connection(const char* page, const char* server, const char* var_get) { std::string url("http://"); url.append(server); ::WSADATA wsa; if (::WSAStartup(MAKEWORD(2, 0), &wsa)) { std::cerr << "FEHLER: WinSock konnte nicht gestartet werden!" << std::endl; return false; } HOSTENT* ptr_host(::gethostbyname(url.c_str())); if (ptr_host == NULL) { std::cerr << "FEHLER: Host nicht gefunden!" << std::endl; return false; } SOCKADDR_IN addr = { }; addr.sin_family = AF_INET; addr.sin_port = ::htons(80); std::strncpy(static_cast<char*>(&addr.sin_addr.s_addr), ptr_host->h_addr, 4); if (addr.sin_addr.s_addr == INADDR_NONE) { std::cerr << "FEHLER: IP-Adresse fehlerhaft!" << std::endl; return false; } if ((m_socket = ::socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) { std::cerr << "FEHLER: Socket konnte nicht erstellt werden!" << std::endl; return false; } if (::connect(m_socket, static_cast<::SOCKADDR*>(&addr), sizeof(SOCKADDR))) { std::cerr << "FEHLER: Es konnte keine Verbindung aufgebaut werden!" << std::endl; return false; } std::ostringstream ss; ss << "GET /" << page << " HTTP/1.1\nHost: " << server << "\nUser-Agent: 3D Blobby Space\n\n" << var_get; const std::size_t command_length(ss.str().length()); if ((::send(m_socket, ss.str().c_str(), command_length, 0)) < command_length) { std::cerr << "FEHLER: Es wurden nicht alle Daten übertragen!" << std::endl; return false; } return true; } const char* get_text() { static char buffer[10024]; return (::recv(m_socket, buffer, 10024, 0) < 0 ? "" : buffer); } void close_connection() { ::closesocket(m_socket); ::WSACleanup(); } }; |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
tbResult CFTP::CreateConnection(char *Server, char *User, char *Passwort) { if((hInternet = InternetOpen("FoxFtp", INTERNET_OPEN_TYPE_DIRECT, 0,0,0))==NULL) return TB_ERROR; hFTPConnection=InternetConnect(hInternet,Server,INTERNET_DEFAULT_FTP_PORT,User,Passwort,INTERNET_SERVICE_FTP,INTERNET_FLAG_PASSIVE,0); if(hFTPConnection==NULL) return TB_ERROR; return TB_OK; } tbResult CFTP::CloseConnection() { InternetCloseHandle(hInternet); return TB_OK; } tbResult CFTP::Write(char *filename, char *Data, unsigned int Size) { HINTERNET hFile= FtpOpenFile(hFTPConnection,filename,GENERIC_WRITE,FTP_TRANSFER_TYPE_BINARY,0); if( hFile==NULL) return TB_ERROR; DWORD nBytesWritten = 0; DWORD nBytesLeft = Size; do { if(nBytesWritten+Size>=Size || nBytesLeft>=0) { InternetWriteFile (hFile, Data, nBytesLeft, &nBytesWritten); nBytesLeft-=nBytesWritten; } }while(nBytesWritten>=Size); return TB_OK; } tbResult CFTP::Read(char* filename, void** Data, unsigned int Size) { HINTERNET hFile= FtpOpenFile(hFTPConnection,filename,GENERIC_READ,FTP_TRANSFER_TYPE_BINARY,0); if( hFile==NULL) return TB_ERROR; DWORD nBytesRead=0; DWORD nBytesLeft=Size; char *File; File=new char[Size]; do { if(!InternetReadFile (hFile, &File, nBytesLeft, &nBytesRead)) return TB_ERROR; }while(nBytesLeft<=nBytesRead); Data=(void**)File; return TB_OK; } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <TriBase.h> #include <windows.h> #include <winsock.h> #include <stdio.h> #include <stdlib.h> #include <iostream> #include <wininet.h> #include <string> #pragma comment( lib, "ws2_32.lib" ) #pragma comment( lib, "wininet.lib" ) using namespace std; #define WOELKCHEN_API __declspec(dllexport) class WOELKCHEN_API CFTP { private: HINTERNET hInternet, hFTPConnection; public: tbResult CreateConnection(char *Server, char *User, char *Passwort); tbResult CloseConnection(); tbResult Write(char *filename, char *Data, unsigned int Size); tbResult Read(char* filename, void** Data, unsigned int Size); }; |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
tbResult CFTP::Read(char* filename, void** ppOut, unsigned int Size) { HINTERNET hFile= FtpOpenFile(hFTPConnection,filename,GENERIC_READ,FTP_TRANSFER_TYPE_BINARY,0); DWORD dGeleseneBytes; void* Data; if(!InternetReadFile(hFile, Data, Size, &dGeleseneBytes)) return TB_ERROR; *ppOut=Data; return TB_OK; } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
void ShowLastErrorString() { LPVOID lpMsgBuf; FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language (LPTSTR)&lpMsgBuf, 0, NULL ); MessageBox(NULL, (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION); LocalFree(lpMsgBuf); } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 |
unsigned int Size=4096; void* Data; DWORD dGeleseneBytes; if(!InternetReadFile(hFile, Data, Size, &dGeleseneBytes)) { tbWriteToLog("Es kann nicht aus dem File gelelesen werden!"); return TB_ERROR; } |
Werbeanzeige