In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Today, I will talk to you about how to establish a relationship between Java Socket programming. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
In fact, the simple understanding of network programming is that two computers communicate data with each other. It is relatively easy for programmers to master a programming interface and use a programming model. Java SDK provides some relatively simple Api to do this work. Java Socket programming is one of them. For Java. These Api are stored in the package with java.net. So as long as you import this package, you can prepare network programming.
The basic model of network programming is the client-server model. To put it simply, two processes communicate with each other, and then one of them must provide a fixed location, while the other only needs to know that fixed location. And to establish a connection between the two.. Then complete the communication of the data. The one that provides the fixed location is usually called the server, while the one who establishes the contact is usually called the client. Based on this simple model, you can enter the network programming.
There are many kinds of Api that Java supports this model. Here I just want to introduce the programming interface of Java Socket programming. For Java, the programming interface of Socket has been simplified. First of all, let's discuss how to set up a fixed-location service. Java provides ServerSocket to support it. In fact, when you create a strength object of this class and provide a port resource, you create a fixed location for other computers to access you. ServerSocket server=new ServerSocket (6789); a little note here is that the port allocation must be *. Because the port is used to identify each computer service. In addition, the port number ranges from 0 to 65535, and the first 1024 ports have been reserved by Tcp/Ip, so you can only assign 1024 ports. Okay。
We have a fixed position. All that is needed now is a cable. The connection line is first requested by the customer. So Java also provides a Java Socket programming object to support it. As long as the client creates an instance object of Socket to support it. Socket client=new Socket (InetAddress.getLocalHost (), 5678); the client must know the IP address of the server. For this point, Java also provides a related class InetAddress. An instance of this object must be provided through its static method. Its static methods mainly provide methods to get native IP and InetAddress directly by name or IP.
Okay。 The above method can basically establish a connection for the two computers to communicate with each other. But how is the data transmitted? As a matter of fact, Icano operation is always closely related to network programming. Because the underlying network is to continue the data. Unless called remotely, the core of dealing with the problem is execution. Otherwise, the interaction of data depends on IO operation. So you also have to import the package java.io. Java's IO operation is not complicated. It provides readers and writers for byte streams and Unicode, and then also provides a buffer for data reading and writing.
BufferedReader in=new BufferedReader (new InputStream
Reader (server.getInputStream ())
PrintWriter out=new PrintWriter (server.getOutputStream ())
The above two sentences are to establish a buffer and convert the original byte stream into Unicode operable. The original byte stream comes from two methods of Java Socket programming. GetInputStream () and getOutputStream (). Used to get input and output respectively. So now there are basic models and basic operating tools. We can do a simple Socket routine.
Service party:
Import java.io.*
Import java.net.*
Public class MyServer {
Public static void main (String [] args) throws IOException {
ServerSocket server=new ServerSocket (5678)
Socket client=server.accept ()
BufferedReader in=new BufferedReader (new InputStreamReader
(client.getInputStream ())
PrintWriter out=new PrintWriter (client.getOutputStream ())
While (true) {
String str=in.readLine ()
System.out.println (str)
Out.println ("has receive....")
Out.flush ()
If (str.equals ("end"))
Break
}
Client.close ()
}
}
The main purpose of this Java Socket programming is that the server continues to receive the information written by the client. The client exits the program when it sends the "End" string. And the server will also make "Receive" in response. Informs the client that the message has been received.
After reading the above, do you have any further understanding of how Java Socket programming establishes the relationship between the two? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.