Two programs on the network exchange data through a two-way communication connection, one end of which is called a socket. To establish a network communication connection, at least a pair of port numbers (sockets) are required. The essence of sockets is a programming interface (API), which is the encapsulation of TCP/IP. TCP/IP also provides an interface for programmers to use for network development, which is the Socket programming interface; HTTP is a car, which provides a specific form of encapsulation or display of data; Socket is an engine, which provides the ability of network communication. Download Eclipse: https://www.eclipse.org/ This article will give a complete Socket communication code, which is tested and effective in eclipse. It is the same to develop with Java in Android Studio, just match the code with the control and register the listener. The difference is that the import declaration in AS is different, and the window does not need to be created by yourself. The effect is as shown below: In eclipse, File → New → Java Project → give it an English name → Finish (I named it TCP) Directory structure: After the new package is created, there is a src default package. Right-click src → New → Package → enter com.net (because the name of the package I use in the program is this, you can change both at the same time) → Finish Then right-click com.net → New → Class → enter TCPClient (because this is the class name I use in my program) Then right-click com.net → New → Class → enter TCPServer (because this is the class name I use in my program) Paste the code in each of them: TCPServer
TCPClient
Then run the server first, then run the client, otherwise an error will be reported, because the essence of Socket communication is to open the server listening port first. Then the effect diagram will appear. The local loopback IP is 127.0.0.1. You can change it to other IP addresses to achieve cross-machine Socket communication. This article is reprinted from the WeChat public account "Embedded Linux System Development". You can follow it through the following QR code. To reprint this article, please contact the WeChat public account of Embedded Linux System Development. |
<<: Slow SQL Management Sharing
>>: Weibu Online was shortlisted for CDM 2021 Black Unicorn Awards
At present, the core network is in a critical per...
10gbiz has released this month's promotion, w...
CloudCone offers several special packages for Val...
"Vigorously develop the digital economy, imp...
Previously, we talked about how the domain name i...
Hostodo released two special packages in Septembe...
TmhHost launched a promotion during the May Day I...
SoftShellWeb is a foreign hosting company registe...
DediPath recently offered special discount codes ...
After the rapid development in 2020, 2021 is a cr...
Safety monitoring of transportation infrastructur...
Xi'an, a world-renowned ancient capital and c...
1. What is AP? Answer: AP - Wireless Access Point...
【51CTO.com original article】 Do you know how much...
[[415817]] 5G is the abbreviation for the fifth g...