In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
How to use socket to communicate between client and server in Java? aiming at this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
Network programming in Java is a very important part, and it is also one of its programming advantages. There is a special Java.net class library in Java to manage the methods of network programming.
Let's first describe how to use socket to communicate between client and server in Java. Finally, we will introduce one of the simplest telephone programs.
one。 How to use socket to communicate between client and server
Socket is used to program the communication between client and server in Java. Socket is an effective endpoint for communication between two entities. The source IP address and source port, the end IP address and the end port can be obtained through socket. Users can connect multiple socket to the same port so that there can be multiple connections for a single port. Through socket client / server programming, you can create a distributed program that can be used by many people, and all clients can work with a unified front end and communicate with the server.
To communicate with the server, you must have three conditions: the server program, the client program, and the socket program that connects them. None of these three parts is indispensable. However, there are many ways to communicate between the client and the server, one of which is to take the client as the taker and the server as the giver. Let's take a look at Java's server programming.
In Java, the server has three main functions:
1. By constructing an instance of the ServerSocket class in the Java.net class library, the server can detect the information of the specified port. The accept () method in ServerSocke enables the server to detect activity on the specified port. In addition, the server is responsible for detecting customers who are required to connect to it.
An instance of the Socket class represents a successful connection between the client and the server. Multiple users can be programmatically connected to the server through the same port, all of which are instances of the Socket class.
two。 You can send and capture data using the getInputStream () and getOutStream () methods of the Socket class, respectively. The method of use is as follows:
Try {
ServerSocket myServerSocket=new ServerSocket (100)
Socket my100Socket=myServerSocket.accept ()
} catch (Exception e) {}
In the above code, you first construct an instance of the ServerSocket class and pass it an integer to specify a given port that can be used by the server, as follows:
ServerSocket myServerSocket=new ServerSocket (100)
In the Java program, if you can keep catching abnormal events every time you construct a ServerSocket, you specify the port you are going to use at any time. The following code uses the accept () method to detect port activity.
Socket my100Socket=myServerSocket.accept ()
The Accept () method does not continue to execute the interrupted execution program until it receives a connection request from the user. Once the customer's connection is successful, my100Socket represents the connection and can send and receive data.
Finally, let's take a look at how the customer requests a connection. The connection method is as follows:
Try {
Socket mySocket=new Socket (www.cpcw.com, 100)
} catch (Exception e) {}
As you can see from the above code, it is also implemented through the Socket class. Let's use an example of network programming to illustrate how to communicate on the network.
two。 One of the simplest call programs
Communicator server:
Import java.net.*
Import java.io.*
Import java.lang.*
Public class myserver {
Public static void main (String args []) {
ServerSocket server
Socket socket
String s
InputStream Is
OutputStream Os
DataInputStream DIS
PrintStream PS
Try {
/ / Register the service on port 4321
Server=new ServerSocket (4321)
Socket=server.accept (); / / listening window, waiting for connection
System.out.println ("server ok")
System.out.println ("* *")
System.out.println ("")
/ / get the input / output stream of the corresponding Socket
Is=socket.getInputStream ()
Os=socket.getOutputStream ()
/ / establish data flow
DIS=new DataInputStream (Is)
PS=new PrintStream (Os)
DataInputStream in=new DataInputStream (System.in)
While (true) {
System.out.println ("")
System.out.println ("please wait client's message...")
System.out.println ("")
S=DIS.readLine (); / / read in the string from client
System.out.println ("client said:" + s); / / print string
If (s.trim () .equals ("BYE") break; / / if it is "BYE", quit
System.out.print ("you say:")
S=in.readLine (); / / read the string entered by the user
PS.println (s); / / pass the read string to client
If (s.trim () .equals ("BYE") break; / / if it is "BYE", quit
}
/ / close the connection
DIS.close (); / / close the data input stream
PS.close (); / / close the data output stream
Is.close (); / / close the input stream
Os.close (); / / close the output stream
Socket.close (); / / close sockey
}
Catch (Exception e) {
System.out.println ("Error:" + e)
}
}
}
Intercom client
Import java.net.*
Import java.io.*
Import java.lang.*
Public class myclient {
Public static void main (String args []) {
If (args.length
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.