 |
สอบถามเรื่อง php socket เพื่อรับข้อมูลจากอุปกรณ์ภายนอกเข้ามาเก็บลงใน Server |
|
 |
|
|
 |
 |
|
พอดีกำลังทำ php socket เพื่อที่รับเอาข้อมูลจากภายนอกมาเก็บไว้ใน server ครับ ซึ่งข้อมูลนั้นจะถูกส่งผ่านอุปกรณ์ Gprs Module ครับ
ผมได้ทำการศึกษาและลองทดสอบโค้ดทางฝั่งของ Server ดู แต่มันติด Error ตรงส่วนของ socket_bind() ครับ มันฟ้อง Error ว่า
Code
Warning: socket_bind() [function.socket-bind]: unable to bind address [0]: Only one usage of each socket address (protocol/network address/port) is normally permitted. in D:\AppServ\www\php_socket\php_socket.php on line 15
Could not bind to socket
ผมพยายามหาวิธีแก้แล้วแต่ก็ยังไม่ได้ครับ รบกวนท่านที่เคยเจอปัญหานี้ช่วยชี้แนะด้วยครับ....ขอบคุณครับ
ตัวอย่งโค้ดครับ...
Code (PHP)
<?php
// set some variables
$host = "127.0.0.1";
$port = 80;
// 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";
socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n");
// close sockets
socket_close($spawn);
socket_close($socket);
?>
Tag : PHP, MySQL, HTML/CSS, JavaScript, Ajax, jQuery
|
|
 |
 |
 |
 |
Date :
2013-04-06 12:29:42 |
By :
prcancle-13 |
View :
3466 |
Reply :
5 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
มัน bind ไม่ได้ก็เป็นเรื่องปกติอยู่แล้วครับ
เพราะมี sokcet อื่น bind พอร์ตนี้อยู่แล้ว
80 เป็นพอร์ตทั่วไปสำหรับ HTTP ซึ่ง apache หรือ IIS (หรือ webserver อื่นๆ) จะจองพอร์ตนี้ไว้อยู่
|
 |
 |
 |
 |
Date :
2013-04-07 05:04:57 |
By :
cookiephp |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
พอร์ตเลขอะไรก็ได้ครับ ที่ไม่ซ้ำกับโปรแกรมอื่น
หรือไม่ก็ เวลาทดสอบโปรแกรมนี้ ให้ปิด service ของ apache หรือ IIS ไว้ชั่วคราว
|
 |
 |
 |
 |
Date :
2013-04-07 14:22:31 |
By :
cookiephp |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอบความคิดเห็นที่ : 4 เขียนโดย : prcancle-13 เมื่อวันที่ 2013-04-07 14:37:16
รายละเอียดของการตอบ ::
อีกนิดครับ คือผมลองจำลองเครื่องตัวเองเป็นทั้ง Server และ Client แล้วทำการส่งข้อมูลจาก Client ไปให้ยัง Server ประมวลผล
ซึ่งสามารถทำได้ปกติครับ แต่พอลองนำโค้ดไปทดสอบใน Server จริง ปรากฏว่าผมจะต้องเปลี่ยนพอร์ตใหม่ทุกครั้งหลังจากการส่งข้อมูลให้ Server
ตัวอย่างโค้ดฝั่ง Server
Code (PHP)
<?php
// set some variables
$host = "122.155.13.143";
$port = 5364;
// 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;
//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 output\n");
// close sockets
socket_close($spawn);
socket_close($socket);
?>
ตัวอย่างโค้ดฝั่ง Client
Code (PHP)
<?
$host = "122.155.13.143";
$port = 5364;
$message = "Test_send Information";
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);
?>
จากโค้ดเมื่อทดสอบใน Server จริง ผมจะต้องทำการเปลี่ยนพอร์ตใหม่ทุกทั้งหลังจากการรันครับ
จะต้องแก้ใขอย่างไรครับ....
|
 |
 |
 |
 |
Date :
2013-04-07 15:04:25 |
By :
prcancle-13 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|