sauertopf
07-02-2003, 12:53
Hallo neues Forum!
Bei dem Progrämmchen im Anhang wird nach der Ausgabe von bufr auch immer der Inhalt von buf auf dem Bildschirm ausgegeben. Kann mir das bitte einer erklären?
Falls diese Frage irgendwie daneben ist, bitte hinweisen. Bin eben neu.
Gruß, sauertopf
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <unistd.h>
int create_inet_socket(uint16_t port){
int socket1;
struct sockaddr_in name, namer;
socket1=socket(PF_INET, SOCK_STREAM, getprotobyname("IP")->p_proto);
name.sin_family = AF_INET;
name.sin_port = htons (port);
name.sin_addr.s_addr = htonl (INADDR_ANY);
if (bind(socket1, (struct sockaddr *) &name, sizeof(name)) < 0)
{
perror ("bind");
//exit (EXIT_FAILURE);
}
uint32_t ip=192*2^24+168*2^16+100*2^8+100;
char ipc[3];
ipc[0]=192; ipc[1]=168; ipc[2]=100; ipc[3]=100;
namer.sin_family = AF_INET;
namer.sin_port = htons (80);
struct hostent *hostinfo;
//char gmx[20]="www.gmx.de";
//hostinfo=gethostbyname(gmx);
//cout << ipc << endl;
namer.sin_addr = *(struct in_addr *) ipc;
if (connect(socket1, (struct sockaddr *) &namer, sizeof(namer)) < 0)
{
perror ("connect");
//exit (EXIT_FAILURE);
}
return(socket1);
}
int main(int argc, char *argv[])
{
cout << "Hello, World!" << endl;
int sock=create_inet_socket(57655);
char buf[30]="GET /index.html HTTP/1.0\n\n";
write(sock, &buf, 31);
char bufr[10];
read(sock, &bufr, 11);
cout << bufr << endl;
read(sock, &bufr, 11);
cout << bufr << endl;
close(sock);
return EXIT_SUCCESS;
}
Ausgabe:
Hello, World!
ˬdd\
HTTP/1.0 20@GET /index.html HTTP/1.0
0 OK
Date:@GET /index.html HTTP/1.0
Bei dem Progrämmchen im Anhang wird nach der Ausgabe von bufr auch immer der Inhalt von buf auf dem Bildschirm ausgegeben. Kann mir das bitte einer erklären?
Falls diese Frage irgendwie daneben ist, bitte hinweisen. Bin eben neu.
Gruß, sauertopf
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <unistd.h>
int create_inet_socket(uint16_t port){
int socket1;
struct sockaddr_in name, namer;
socket1=socket(PF_INET, SOCK_STREAM, getprotobyname("IP")->p_proto);
name.sin_family = AF_INET;
name.sin_port = htons (port);
name.sin_addr.s_addr = htonl (INADDR_ANY);
if (bind(socket1, (struct sockaddr *) &name, sizeof(name)) < 0)
{
perror ("bind");
//exit (EXIT_FAILURE);
}
uint32_t ip=192*2^24+168*2^16+100*2^8+100;
char ipc[3];
ipc[0]=192; ipc[1]=168; ipc[2]=100; ipc[3]=100;
namer.sin_family = AF_INET;
namer.sin_port = htons (80);
struct hostent *hostinfo;
//char gmx[20]="www.gmx.de";
//hostinfo=gethostbyname(gmx);
//cout << ipc << endl;
namer.sin_addr = *(struct in_addr *) ipc;
if (connect(socket1, (struct sockaddr *) &namer, sizeof(namer)) < 0)
{
perror ("connect");
//exit (EXIT_FAILURE);
}
return(socket1);
}
int main(int argc, char *argv[])
{
cout << "Hello, World!" << endl;
int sock=create_inet_socket(57655);
char buf[30]="GET /index.html HTTP/1.0\n\n";
write(sock, &buf, 31);
char bufr[10];
read(sock, &bufr, 11);
cout << bufr << endl;
read(sock, &bufr, 11);
cout << bufr << endl;
close(sock);
return EXIT_SUCCESS;
}
Ausgabe:
Hello, World!
ˬdd\
HTTP/1.0 20@GET /index.html HTTP/1.0
0 OK
Date:@GET /index.html HTTP/1.0