In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Java Socket communication how to deal with its own port problem, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.
There are many problems in the use of Java Socket communication, which is very important in port programming. Let's take a look at how Java Socket communication can better use the relevant code.
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 accomplish these tasks. Java Socket communication 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 for Java Socket communication. 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 Socket object to support it. As long as the client creates an instance object of Java Socket communication 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 InputStreamReader
(server.getInputStream ())
PrintWriter out=new PrintWriter (server.getOutputStream ())
The above two sentences transform the original byte stream into Unicode operable (that is, characters) and establish buffers to improve efficiency. The original byte stream comes from two methods of Java Socket communication. 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 Java 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 InputStream
Reader (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 ()
}
}
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, 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.