-2
socket을 사용하여 클라이언트 및 서버 프로그램을 작성했지만 localhost에서 완벽하게 작동합니다. cpanel을 통해 서버에 업로드하면 제대로 작동하지 않으며 다음과 같은 오류가 발생합니다. 가장 중요한 것은 두 번 프로그램이 모두 서버에서 작동한다는 것입니다. 소켓 프로그래밍에 익숙하지 않고 stackoverflow에서 관련 게시물을 보았지만 정확한 해결책을 찾지 못했습니다.오류가 발생하는 서버에서 클라이언트 서버 프로그램을 실행할 수 없습니다. socket_bind() : 주소를 바인드 할 수 없습니다. [98] : 주소가 이미 사용 중입니다.
Client: Warning: socket_connect(): unable to connect [111]: Connection refused in .../client.php on line 10
Could not connect to server
Server:Warning: socket_bind(): unable to bind address [98]: Address already in use in ..server.php on line 10
Could not bind to socket
Client.php
<?php
// where is the socket server?
$host = "XX.XX.XXX.XX";
$port = 25763;
$message = "Hello Server This is the first message to the server";
echo "Message To server :".$message;
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
// connect to server
$result = socket_connect($socket, $host, $port) or die("Could not connect to server\n");
// send string to server
socket_write($socket, $message, strlen($message)) or die("Could not send data to server\n");
// get server response
$result = socket_read ($socket, 1024) or die("Could not read server response\n");
echo "Reply From Server :".$result;
// close socket
socket_close($socket);
// print result to browser
?>
Server.php 당신이 socket_set_opt()
를 사용하여 새로 생성 된 소켓에 옵션 SO_REUSEADDR
을 설정하는 것 같아서 서버에 대한
<?php
// set some variables
$host = "XX.XX.XXX.XX";
$port = 25763;
// don't timeout!
set_time_limit(0);
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
// bind socket to port
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
// start listening for connections
$result = socket_listen($socket, 3) or die("Could not set up socket listener\n");
// accept incoming connections
// spawn another socket to handle communication
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
// read client input
$input = socket_read($spawn, 1024) or die("Could not read input\n");
// clean up input string
$input = trim($input);
echo "Client Message : ".$input;
// reverse client input and send back
$output = strrev($input) . "\n";
echo $spawn;
socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n");
// close sockets
socket_close($spawn);
socket_close($socket);
?>
어떤 종류의 서버에 업로드하셨습니까? – Daedalus
It is Linux CentOS – user3809096
공유 되었습니까? 전용 IP를 가지고 있습니까? – Daedalus