สอบถามการเขียนโค้ดติดต่อ socket เพื่อรับค่า data ที่ส่งมา

สำหรับผู้ที่ เริ่มต้น Programming - PHP มีอะไร แนะนำ หรือข้อสงสัยต้องบอร์ด นี้ คนที่มีความรู้ แบ่งปันคนอื่นบ้างนะ ปัญหาการเขียนโปรแกรม แบบ OOP Session Cookies php network

Moderator: mindphp, ผู้ดูแลกระดาน

ภาพประจำตัวสมาชิก
map
PHP Jr. Member
PHP Jr. Member
โพสต์: 10
ลงทะเบียนเมื่อ: 01/01/1970 7:00 am

สอบถามการเขียนโค้ดติดต่อ socket เพื่อรับค่า data ที่ส่งมา

โพสต์ที่ยังไม่ได้อ่าน โดย map »

สอบถามการเขียนโค้ดติดต่อ socket เพื่อรับค่า data ที่ส่งมาที่ port ที่เรากำหนดไว้ เขียน data นั้นมาเก็บไว้ในรูปแบบของ file เพื่อนำไปใช้ในการทำ report ต่อไปครับ ไม่ทราบท่านใดพอจะแนะนำ และมีตัวอย่างโค้ดที่นำมาใช้งานลักษณะนี้บ้างครับ ผมจะนำมารับค่าเพื่อ เอา data นี้มาใช้งานครับ
ขอบคุณครับ
ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41336
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

โพสต์ที่ยังไม่ได้อ่าน โดย mindphp »

socket ที่ติดต่อมา ใช้วิธีไหน มาครับ
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
ภาพประจำตัวสมาชิก
map
PHP Jr. Member
PHP Jr. Member
โพสต์: 10
ลงทะเบียนเมื่อ: 01/01/1970 7:00 am

ส่งมาจาก GSM Modlue ครับ

โพสต์ที่ยังไม่ได้อ่าน โดย map »

ส่งมาจาก GSM Modlue ครับโดยการเชื่อมต่อ GSM module ผ่านเครือข่าย GPRS และก็ส่ง data มา โดยกำหนด IP เครื่อง server ที่เราจะติดต่อ และ port ที่เราใช้เชื่อมต่อ จากนั้นก็ทำการส่ง data เป็น Text ครับ แต่ที่ผมทดสอบ ผมใช้โปแกรมพวก TCP server เป็นตัว เปิด port รอ ไว้ที่ฝั่ง server และจากนั้นก็จะมีหน้าต่าง monitor ดูครับ ก็จะเห็น data ที่เราส่งมา แต่โปแกรมเหล่านี้ไม่สามารถที่จะ save หรือ ทำอะไรได้นอกจาก เปิด port รอ เฉยๆ ครับ ผมต้องการนำเอา data นี้ มาในการทำงาน แสดง ผล และ รายงานอย่างอื่นครับ โดยการที่เราจะ save เก็บไว้เป็น file หรือ จะรับแล้ว ให้ inset เข้าที่ mysql เลยก็ได้ครับ เพราะ data นี้จะมีการส่งมา ทุก 1 นาที ครับ เพื่อนำมาใช้งานครับ
ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41336
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

โพสต์ที่ยังไม่ได้อ่าน โดย mindphp »

กำหนดให้ ส่งมาที่ ไฟล์ php ส่งมาแบบ post หรือ get โดย ผ่าน ทาง http ก็ได้ครับ php จะรับค่าพวกนั้นได้
แต่ ผมไม่รู้ วิธี ส่ง นะ แต่รู้ ว่าส่งแบบนั้น php รับได้

แต่ ถ้าไม่ได้ จริงๆ ก็ สั่งให้ php รันคำสั่ง ระบบ เพื่อ รับ ค่า และเก็บ ค่าก็ได้ ครับ :) :)
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
ภาพประจำตัวสมาชิก
map
PHP Jr. Member
PHP Jr. Member
โพสต์: 10
ลงทะเบียนเมื่อ: 01/01/1970 7:00 am

ตัวอย่าง file การรับค่า data

โพสต์ที่ยังไม่ได้อ่าน โดย map »

ไม่ทราบว่ามีโค้ดตัวอย่างที่เอ่มารันดูได้ไหมครับคือ การส่ง data นี้ ผมคิดว่าผมสามารถกำหนดได้ครับ แต่ฝั่งรับผมรับไม่เป้นนะครับและฝั่งส่งนั้นผมคิดว่าน่าจะประมาณว่ากำหนด IPของ webserver ของเรา และกำหนด port เป็น 80 ใชไหมครับ ถ้าใช่ผมก็เปลี่ยนและ port แค่นั้นเองครับ แล้วจากนั้นผมก็สามารถส่งได้เลยครับ เพราะว่ามันเชื่อมต่อผ่านระบ GPRS ครับและ data ที่ส่งมามีรูปแบบดังนี้ครับ
16.5425758 102.8758965 08:20:33 01/10/07 50 120 A

ความหมายนะครับ
16.5425758 คือ latitude
102.8758965 คือ longtitude
08:20:33 คือ เวลาปัจจุบัน
01/10/07 คือ วันที่ปัจจุบัน
50 คือ ความเร็ว
120 คือ ทิศทาง
A คือ สามารถรับค่า GPS ได้

ค่าที่ส่งมาเป้นแบบนี้เลยครับ ต่อ 1บรรทัดครับ คั่นด้วยการเคาะ spacbar 1 ครั้งครับและทุก 20 วินาที จะมีการส่งมา 1 ครั้งครับเสร็จแล้วผมอยากให้มัน add เข้า database ครับจะได้นำมาใช้ในการทำงานและทำแผนที่ต่อไปครับ แต่ผมทำฝั่งรับนี้ไม่เป้นครับ รบกวนด้วยครับ ไม่ทราบว่ามีดค้ดฝั่งรับนี้ไหมครับ ผมจะรันทิ้งไว้ที่ server แล้วก็จะลองส่ง data เข้ามาดุครับ รบกวนต่อยอดให้อีกนิดครับ เพราะไม่เคยเขียนมาก่อนครับเลยไม่ทราบว่าจะเขียนยังไงครับไม่ทราบโพส์รูปได้ไหมครับ ถ้าได้ผมจะลงรูปโปแกรมที่ผมทดสอบ ฝั่งรับให้ดุครับ ขอบคุณครับ
ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41336
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

โพสต์ที่ยังไม่ได้อ่าน โดย mindphp »

อัพภาพไว้ที่ ฝากไฟล์ ต่างๆ แล้ว เอาลิงค์ มาใส่ ได้ครับ
การส่งค่า ผมยกตัวอย่าง โปรแกรม wget ของ unix นะครับ มันจะสามารถกำหนด ได้ว่า จะใช้ วิธีส่งมาแบบไหน เช่น POST GETถ้ารู้ว่าส่งมาแบบไหน ฝั่งรับ ก็ ไม่มีอะไรมากเลยครับ เหมือน กับการรับค่าจาก form ธรรมดา เลย
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
ภาพประจำตัวสมาชิก
map
PHP Jr. Member
PHP Jr. Member
โพสต์: 10
ลงทะเบียนเมื่อ: 01/01/1970 7:00 am

เดียวไปดูก่อนนะครับว่าจะส่งแบบไหนนะครับ

โพสต์ที่ยังไม่ได้อ่าน โดย map »

เดียวไปดูก่อนนะครับว่าจะส่งแบบไหนนะครับเพราว่าที่ผมส่งนั้นเขาเรียกว่าการส่งผ่าน socket ของ windows นะครับโดยใช้โปรดตคอล TCP ครับและฝั่งรับก็จะเขียนเปิด socket เพื่อรับค่า data ที่ส่งมาและก็ทำการ save ลงเป็น file หรือ inset เข้าไปใน database ประมาณนั้นนะครับ ที่ผมทราบมาฝั่งรับนะครับ ผมได้ไปหาโค้ด PHP มาตามนี้นะครับเป้นตัวอย่างนะครับแต่ผมไม่เป็นภาษา php เท่าไหร่ครับเลยดูไม่ค่อยเป้นครับ

<?
// set some variables
$host = "192.168.1.99";
$port = 1234;
// 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);
// 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);


?>--------------------------------------------------------------------------------
This is somewhat involved, so an explanation is in order:

1. The first step here is to set up a couple of variables to hold information on the IP address and port on which the socket server will run. You can set up your server to use any port in the numeric range 1-65535, so long as that port is not already in use.
--------------------------------------------------------------------------------<?
// set some variables
$host = "192.168.1.99";
$port = 1234;
?>--------------------------------------------------------------------------------
2. Since this is a server, it's also a good idea to use the set_time_limit() function to ensure that PHP doesn't time out and die() while waiting for incoming client connections.
--------------------------------------------------------------------------------<?
// don't timeout!
set_time_limit(0);
?>--------------------------------------------------------------------------------
3. With the preliminaries out of the way, it's time to create a socket with the socket_create() function - this function returns a socket handle that must be used in all subsequent function calls.
--------------------------------------------------------------------------------<?
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create
socket\n");
?>--------------------------------------------------------------------------------
In case you're wondering what this is all about, don't worry too much about it. The AF_INET parameter specifies the domain, while the SOCK_STREAM parameter tells the function what type of socket to create (in this case, TCP).

If you wanted to create a UDP socket, you could use the following line of code instead:
--------------------------------------------------------------------------------<?
// create socket
$socket = socket_create(AF_INET, SOCK_DGRAM, 0) or die("Could not create
socket\n");
?>--------------------------------------------------------------------------------
4. Once a socket handle has been created, the next step is to attach, or "bind", it to the specified address and port. This is accomplished via the socket_bind() function.
--------------------------------------------------------------------------------<?
// bind socket to port
$result = socket_bind($socket, $host, $port) or die("Could not bind to
socket\n");
?>--------------------------------------------------------------------------------
5. With the socket created and bound to a port, it's time to start listening for incoming connections. PHP allows you to set the socket up as a listener via its socket_listen() function, which also allows you to specify the number of queued connections to allow as a second parameter (3, in this example).
--------------------------------------------------------------------------------<?
// start listening for connections
$result = socket_listen($socket, 3) or die("Could not set up socket
listener\n");
?>--------------------------------------------------------------------------------
6. At this point, your server is basically doing nothing, waiting for incoming client connections. Once a client connection is received, the socket_accept() function springs into action, accepting the connection request and spawning another child socket to handle messaging between the client and the server.
--------------------------------------------------------------------------------<?
// accept incoming connections
// spawn another socket to handle communication
$spawn = socket_accept($socket) or die("Could not accept incoming
connection\n");
?>--------------------------------------------------------------------------------
This child socket will now be used for all subsequent communication between the client and server.

7. With a connection established, the server now waits for the client to send it some input - this input is read via the socket_read() function, and assigned to the PHP variable $input.
--------------------------------------------------------------------------------<?
// read client input
$input = socket_read($spawn, 1024) or die("Could not read input\n");
?>--------------------------------------------------------------------------------
The second parameter to socket_read() specifies the number of bytes of input to read - you can use this to limit the size of the data stream read from the client.

Note that the socket_read() function continues to read data from the client until it encounters a carriage return (\n), a tab (\t) or a \0 character. This character as treated as the end-of-input character, and triggers the next line of the PHP script.

8. The server now must now process the data sent by the client - in this example, this processing merely involves reversing the input string and sending it back to the client. This is accomplished via the socket_write() function, which makes it possible to send a data stream back to the client via the communication socket.
--------------------------------------------------------------------------------<?
// reverse client input and send back
$output = strrev($input) . "\n";
socket_write($spawn, $output, strlen ($output)) or die("Could not write
output\n");
?>--------------------------------------------------------------------------------
The socket_write() function needs three parameters: a reference to the socket, the string to be written to it, and the number of bytes to be written.

9. Once the output has been sent back to the client, both generated sockets are terminated via the socket_close() function.
--------------------------------------------------------------------------------<?
// close sockets
socket_close($spawn);
socket_close($socket);
?>
ขอโทษด้วยครับที่ลง ยาวไปนะครับแต่ที่ผมจะเป็นต้องใช้ PHP gเพราะว่าผมต้องเอาไปติดตั้งใน server linux นะครับ
ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41336
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

โพสต์ที่ยังไม่ได้อ่าน โดย mindphp »

ลอง ให้ โพส มาที่ port 80 แล้วระบุไฟล์ ที่ รับ ด้วยซิครับ
จะได้ทำใน ชั้น ของ Application layer
แต่ตัวที่ยกมาดูก็ น่าจะใช้ได้ ครับเล่นไปทาง socket โดยตรง ขึ้นอยู่กับการส่งเข้ามานะครับ
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
ตอบกลับโพส
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: Google Adsense [Bot] และบุคลทั่วไป 66