![]() |
![]() ![]() |
![]() |
![]()
Post
#1
|
|
![]() Grupa: Zarejestrowani Postów: 262 Pomógł: 5 Dołączył: 8.02.2005 Skąd: Olsztyn / Zatorze Ostrzeżenie: (0%) ![]() ![]() |
Witam,
mam nie lada problem otoz chce skomunikowac klienta (napisanego w php) z serwerem (napisanym w C++). No i problem tkwi w tym że nie dokońca to wychodzi ... (przykłądy są pobrane z roznych stron i np. jesli mam klienta C++ i serwer C++ to dziala - tak samo dziala jak oba mialbym napisane w php) serwer: Kod #include <arpa/inet.h> #include <sys/socket.h> #include <iostream> using namespace std; #define MYPORT 3490 #define BACKLOG 10 int main(void){ int sockfd, new_fd; struct sockaddr_in my_addr; struct sockaddr_in their_addr; socklen_t sin_size; sin_size = sizeof(struct sockaddr_in); int yes=1; if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); exit(1); } if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) { perror("setsockopt"); exit(1); } my_addr.sin_family = AF_INET; my_addr.sin_port = htons(MYPORT); my_addr.sin_addr.s_addr = INADDR_ANY; memset(&(my_addr.sin_zero), '\', 8); if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) { perror("bind"); exit(1); } if (listen(sockfd, BACKLOG) == -1) { perror("listen"); exit(1); } if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1) { perror("accept"); exit(0); } cout << "server: got connection from " << inet_ntoa(their_addr.sin_addr) << endl; if (send(new_fd, "Hello, world!\n", 14, 0) == -1) perror("send"); close(new_fd); close(sockfd); return 0; } klient: Kod <?php error_reporting(E_ALL); echo "<h2>TCP/IP Connection</h2>\n"; /* Get the port for the WWW service. */ $service_port = 3490; //getservbyname('www', 'tcp'); /* Get the IP address for the target host. */ $address = gethostbyname('192.168.0.3'); /* Create a TCP/IP socket. */ $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); if ($socket === false) { echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n"; } else { echo "OK.\n"; } echo "Attempting to connect to '$address' on port '$service_port'..."; $result = socket_connect($socket, $address, $service_port); if ($result === false) { echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n"; } else { echo "OK.\n"; } $in = "HEAD / HTTP/1.1\r\n"; $in .= "Host: www.example.com\r\n"; $in .= "Connection: Close\r\n\r\n"; $out = ''; echo "Sending HTTP HEAD request..."; socket_write($socket, $in, strlen($in)); echo "OK.\n"; echo "Reading response:\n\n"; while ($out = socket_read($socket, 2048)) { echo $out; } echo "Closing socket..."; socket_close($socket); echo "OK.\n\n"; ?> i wchodząc na stronkę gdzie jest klient php'owy dostaje komunikaty: Cytat TCP/IP Connection OK. Attempting to connect to '192.168.0.3' on port '3490'... Warning: socket_connect() [function.socket-connect]: unable to connect [13]: Permission denied in /var/www/html/sample/client.php on line 22 socket_connect() failed. Reason: () Permission denied Sending HTTP HEAD request... Warning: socket_write() [function.socket-write]: unable to write to socket [32]: Broken pipe in /var/www/html/sample/client.php on line 35 OK. Reading response: Warning: socket_read() [function.socket-read]: unable to read from socket [107]: Transport endpoint is not connected in /var/www/html/sample/client.php on line 39 Closing socket...OK. teraz moje pytanie: czemu mam Permission denied ? i jak to zlikwidowac. moj cel to wyslac ządanie z klienta do serwera i odpowiedz serwera do klienta. Pomożecie ? |
|
|
![]() ![]() |
![]() |
Wersja Lo-Fi | Aktualny czas: 14.08.2025 - 11:51 |