 |
สอบถามเรื่องการรับค่าจาก Socket TCP มาเขียนลงไฟล์ text บน Server ครับ |
|
 |
|
|
 |
 |
|
รบกวนสอบถามตามนี้ครับ
จะเขียนโปรแกรมเพื่อรับข้อมูลที่ส่งมาจาก TCP Socket ครับ เพื่อนำมาเขียนลง ไฟล์ text ครับ
(OS : Window Server 2012R2, Web Server : IIS, IP : 111.221.44.134)
โดยที่ทาง Client จะส่งข้อมูลผ่าน TCP Socket มาที่ IP: 111.221.44.134:9090 โดยจะมีข้อมูลเข้ามาทุกๆ 1 นาทีครับ
ผมได้ลองเขียนตามโค้ดนี้ครับ
Code (PHP)
// set some variables
$host = "11.221.44.134";
$port = 9090;
// 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);
//Write data on text file --------
$fileName = "data.txt";
$fileopen = fopen($fileName, 'a+');
$write = $input."\r\n";
fwrite($fileopen, $write);
fclose($fileopen);
// reverse client input and send back
$output = strrev($input) . "n";
socket_write($spawn, $output, strlen ($output)) or die("Could not write outputn");
// close sockets
socket_close($spawn);
socket_close($socket);
แต่พอทดสอบแล้วมันขึ้น Error ตามนี้ครับ

รบกวนด้วยครับว่าต้องทำยังไง หรือว่ามีตัวอื่น(ภาษา) ที่สามารถใช้ได้ง่ายกว่า php แนะนำด้วยครับ
ขอบคุณมากครับ
Tag : PHP, IIS, Windows
|
|
 |
 |
 |
 |
Date :
2018-05-05 05:13:09 |
By :
tanata |
View :
1523 |
Reply :
1 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
|