[TASK] TCP Server - Single Connection

[TASK] TCP Server - Single Connection

Postby admin » Wed Oct 18, 2017 3:38 pm

What is TCP?
Abbreviation of Transmission Control Protocol, and pronounced as separate letters. TCP is one of the main protocols in TCP/IP networks. Whereas the IP protocol deals only with packets, TCP enables two hosts to establish a connection and exchange streams of data. TCP guarantees delivery of data and also guarantees that packets will be delivered in the same order in which they were sent.

From https://www.webopedia.com/TERM/T/TCP.html

Before exchanging data between two PCs or two devices through network using TCP, a TCP connection must be established. In a TCP connection, one side is TCP client and the other one is TCP server.

TCP server will listen on a specific port number, waiting for connection request from TCP client.
TCP client will actively make request for connection to TCP server. To make request for connection, TCP client must know IP address of TCP server and port number TCP server is listening on.

Create TCP client on PHPoC and make request to a TCP server
This tutorial is aim to show how to create TCP server. For TCP client on PHPoC, refer to this example.

Create TCP server on PHPoC - Single Connection

This examples shows how to create a TCP server on PHPoC, listen on port 14700 (users can change it as they want). After connection is established, TCP server will receive data from TCP client and send back data to the TCP client.

This example only accepts one TCP connection. Therefore, there is only one TCP client can connect to this TCP server at the same time.

Let's do it step by step:

1. Make sure that your PHPoC Blue or Black is connected to the Internet.

2. Please download a PSP (PHPoC Support Package) of libraries and examples from PHPoC.

3. Connect PHPoC Blue or Black to your PC via USB cable.

4. Run PHPoC Debugger.

5. Upload all libraries in PSP to your PHPoC Blue or Black through PHPoC Debugger.
In fact, you only need to upload sn_tcp_ac.php file in this example. But for future use, you can upload all.
※ Note that the libraries should be located in the /lib folder.
psp_lib.png (8.84 KiB) Viewed 193 times

6. Find an example of tcp0_echo_ac in the PSP.
psp_tcp0_files.JPG (14.57 KiB) Viewed 193 times

7. Upload init.php and task0.php files to your PHPoC Blue or Black.
※ Note that the init.php and task0.php files should be located in the root folder.
psp_phpoc_files.png (2.22 KiB) Viewed 193 times

8. Modify port number if needed in task0.php file.
psp_tcp0_modified.JPG (37.31 KiB) Viewed 193 times

9. Upload the task0.php to your PHPoC Blue or Black again.

10. We need a TCP client to test it. You can you any kind of software/program on PC, smartphone which can create a TCP client and make request to TCP Server on PHPoC. You can also use another PHPoC to create TCP client for testing.
In this example, I use a TCP client app on Android.
TCP_client_on_play.JPG (41.98 KiB) Viewed 193 times

11. Type IP address of PHPoC and port number used in the code and connect
TCP_client_1.JPG (24.68 KiB) Viewed 193 times

12. Send message from Android to PHPoC via this app.
TCP_client_2.JPG (23.97 KiB) Viewed 193 times

13. See the response message.
TCP_client_3.JPG (21.35 KiB) Viewed 193 times

PHPoC will send back the same message as received. Therefore, Android app receives the same message as what it sent
Site Admin
Posts: 168
Joined: Mon Jan 20, 2014 4:40 pm

Return to Example

Who is online

Users browsing this forum: No registered users and 1 guest