erster Versuch eines Client-Sockets
This commit is contained in:
91
src/main.cpp
91
src/main.cpp
@@ -1,18 +1,79 @@
|
|||||||
#include <iostream> // Schnittstellen fr die Streams einbinden
|
#include <iostream>
|
||||||
using namespace std;
|
#include <unistd.h>
|
||||||
|
|
||||||
int main(void) {
|
#ifdef linux
|
||||||
char c; // Variable fr ein gelesenes Zeichen
|
#include <netdb.h> // gethostbyname(), hostent
|
||||||
bool spc = false; // Variable: War das letzte Zeichen ein Space?
|
#include <arpa/inet.h> // inet_ntoa()
|
||||||
|
#else
|
||||||
|
#include <winsock2.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
while ( cin.get(c) ) {
|
int main(){
|
||||||
if ( c == ' ' ){ // Space?
|
using namespace std;
|
||||||
if ( !spc ) cout << c; // nur das jeweils erste Space ausgeben
|
|
||||||
spc = true; // merken, da˜ Space gelesen
|
#ifndef linux
|
||||||
} else {
|
WSADATA w;
|
||||||
cout << c; // Zeichen au˜er Spaces bernehmen
|
if(int result = WSAStartup(MAKEWORD(2,2), &w) != 0){
|
||||||
spc = false; // merken, da˜ kein Space gelesen
|
cout << "Winsock 2 konnte nicht gestartet werden! Error #" << result << endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
cout << "Bitte gebe einen Hostnamen ein: ";
|
||||||
|
string hostname;
|
||||||
|
cin >> hostname;
|
||||||
|
|
||||||
|
hostent *phe = gethostbyname(hostname.c_str());
|
||||||
|
|
||||||
|
if(phe == NULL){
|
||||||
|
cout << "Host konnte nicht aufgeloest werden!" << endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << "\nHostname: " << phe->h_name << endl << "Aliases: ";
|
||||||
|
|
||||||
|
for(char** p = phe->h_aliases; *p != 0; ++p) cout << *p << " ";
|
||||||
|
cout << endl;
|
||||||
|
|
||||||
|
if(phe->h_addrtype != AF_INET){
|
||||||
|
cout << "Ungueltiger Adresstyp!" << endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(phe->h_length != 4){
|
||||||
|
cout << "Ungueltiger IP-Typ!" << endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||||
|
if(Socket == -1){
|
||||||
|
cout << "Socket konnte nicht erstellt werden!" << endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
sockaddr_in service;
|
||||||
|
service.sin_family = AF_INET;
|
||||||
|
service.sin_port = htons(80); // Das HTTP-Protokoll benutzt Port 80
|
||||||
|
|
||||||
|
char** p = phe->h_addr_list; // p mit erstem Listenelement initialisieren
|
||||||
|
int result; // Ergebnis von connect
|
||||||
|
do {
|
||||||
|
if(*p == NULL) {
|
||||||
|
cout << "Verbindung fehlgschlagen!" << endl;
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
} // Ende des Schleifen-Rumpfes
|
|
||||||
return 0;
|
service.sin_addr.s_addr = *reinterpret_cast<unsigned long*>(*p);
|
||||||
} // Ende des Hauptprogramms
|
p++;
|
||||||
|
result = connect(Socket, reinterpret_cast<sockaddr*>(&service), sizeof(service));
|
||||||
|
}
|
||||||
|
while(result == -1);
|
||||||
|
|
||||||
|
cout << "Verbindung erfolgreich!" << endl;
|
||||||
|
|
||||||
|
#ifdef linux
|
||||||
|
close(Socket);
|
||||||
|
#else
|
||||||
|
closesocket(Socket);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user