kingfinn
28-10-2009, 11:04
Hi all,
der untenstehende Code funktioniert nicht ganz.
Wenn er versucht, zu connecten, passiert nichts und es kommt zu Connection Timeout.
Was ist falsch?
MfG
/**************************************
* OverTheWire.org
* Wargame: Vortex
* Level 0
* by Kingfinn
*************************************/
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <iostream>
using namespace std;
int main()
{
/* Variables */
int sock; // Socket
unsigned int output[4]; // Four Values to read
unsigned int sum; // The Values Added together
char* passwd; // The Server Password
struct sockaddr_in addr; // Adress struct
struct hostent *host;
/* Get IP-Adress */
host = gethostbyname("vortex.labs.pulltheplug.org");
if (!host)
{
herror("gethostbyname() failed");
return 1;
}
/* Initialize struct */
addr.sin_family = AF_INET;
addr.sin_port = htonl(5842);
addr.sin_addr = *(struct in_addr*) host->h_addr_list[0];
cout << "Ip Adress of Target: " << addr.sin_addr.s_addr << endl;
/* Make Socket */
sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock < 0)
{
cout << "Failed to open Socket. Try Again." << endl;
return 1;
}
cout << "Made Socket. Deskriptor: " << sock << endl;
/* Connect */
if(connect(sock, (struct sockaddr*) &addr, sizeof(addr)) < 0)
{
perror("connect() failed");
return 1;
}
cout << "Succesfully connected" << endl;
/* Read sum */
for(int i=0; i<4; i++)
{
recv(sock, (void*)output[i], 4, 0);
sum+=output[i];
}
cout << "Reading of unsigned integers done." << endl;
/* Send sum */
send(sock, &sum, 4, 0);
cout << "Sending of sum done" << endl;
/* Read Pw */
recv(sock, passwd, 1, 0);
cout << passwd << endl;
}
der untenstehende Code funktioniert nicht ganz.
Wenn er versucht, zu connecten, passiert nichts und es kommt zu Connection Timeout.
Was ist falsch?
MfG
/**************************************
* OverTheWire.org
* Wargame: Vortex
* Level 0
* by Kingfinn
*************************************/
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <iostream>
using namespace std;
int main()
{
/* Variables */
int sock; // Socket
unsigned int output[4]; // Four Values to read
unsigned int sum; // The Values Added together
char* passwd; // The Server Password
struct sockaddr_in addr; // Adress struct
struct hostent *host;
/* Get IP-Adress */
host = gethostbyname("vortex.labs.pulltheplug.org");
if (!host)
{
herror("gethostbyname() failed");
return 1;
}
/* Initialize struct */
addr.sin_family = AF_INET;
addr.sin_port = htonl(5842);
addr.sin_addr = *(struct in_addr*) host->h_addr_list[0];
cout << "Ip Adress of Target: " << addr.sin_addr.s_addr << endl;
/* Make Socket */
sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock < 0)
{
cout << "Failed to open Socket. Try Again." << endl;
return 1;
}
cout << "Made Socket. Deskriptor: " << sock << endl;
/* Connect */
if(connect(sock, (struct sockaddr*) &addr, sizeof(addr)) < 0)
{
perror("connect() failed");
return 1;
}
cout << "Succesfully connected" << endl;
/* Read sum */
for(int i=0; i<4; i++)
{
recv(sock, (void*)output[i], 4, 0);
sum+=output[i];
}
cout << "Reading of unsigned integers done." << endl;
/* Send sum */
send(sock, &sum, 4, 0);
cout << "Sending of sum done" << endl;
/* Read Pw */
recv(sock, passwd, 1, 0);
cout << passwd << endl;
}