Potrzebuję pomocy - bo jush nie daję rady ...
Starałem się napisać prosty programik klient-server - a dokładnie klient'a łączącego się z www i wyświetlającego na stdout to co otrzyma, i niestety - moje plany spełzły na niczym - bo nie rozumiem dlaczego tak prosta rzecz nie działa tak jak to sobie wyobrażałem.
Kod: Zaznacz cały
#include <sys/types.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <syslog.h>
#include <errno.h>
#define PORT 80
/* --- Main code --- */
int main(int argc, char *argv[]) {
int gniazdodp;
int fd;
int dlugosc;
int cmd = 0, ip = 0;
int result;
struct sockaddr_in adres;
char netcommand[64];
char srv_ip[15];
char buffer[64];
int opt;
memset(&srv_ip, 0, sizeof(srv_ip));
memset(&netcommand, 0, sizeof(netcommand));
strcpy(srv_ip, "192.168.1.20");
strcpy(netcommand, "GET /");
printf("Target: %s\n", srv_ip);
printf("Command: %s\n", netcommand);
printf("Socket: ", srv_ip, netcommand);
if ((gniazdodp = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("Error: Can't socket\n");
exit(1);
} else {
printf("OK:\n");
}
adres.sin_family = AF_INET;
adres.sin_addr.s_addr = inet_addr(srv_ip);
adres.sin_port = htons(PORT);
dlugosc = sizeof(adres);
printf("Connect: ");
result = connect(gniazdodp, (struct sockaddr *)&adres, dlugosc);
if (result == -1) {
printf("Error: Can't connect: \n");
exit(1);
} else
printf("OK:\n");
printf("Send data (%d Bytes): ", strlen(netcommand));
result = write(gniazdodp, &netcommand, strlen(netcommand));
if (result <= 0) {
printf("Error: Cannot sending data to server.\n");
exit(1);
} else {
printf("OK:\n");
}
printf("Read data:\n ");
while ((result = read(gniazdodp, &buffer, sizeof(buffer))) > 0) {
if (result < 0) {
printf("Error: Cannot read from socket\n");
break;
}
printf("Some data...\n");
printf(buffer);
}
shutdown(gniazdodp, SHUT_RDWR);
close(gniazdodp);
return 0;
}
Pozdrawiam bardzo serdecznie