In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Review in the previous section: after learning about Socket in establishing a single communication between the client and the server, create a separate Socket and pass the properties of the Socket.
So how to connect the two Socket so that the client and server can establish input and output streams for communication? As we mentioned in the previous section, TCP/IP sockets are the most reliable two-way streaming protocol, and TCP/IP can be used to send any amount of data. If the sender and receiver computers determine the port, they can communicate, with the port represented by a socket.
[example 1] the server side of Socket with two-way communication between client and server
Import java.io.BufferedReader
Import java.io.BufferedWriter
Import java.io.InputStreamReader
Import java.io.OutputStreamWriter
Import java.net.ServerSocket
Import java.net.Socket
Public class Server {
Public static void main (String [] args) throws Exception {
ServerSocket server=new ServerSocket (8888)
Socket socket=server.accept ()
BufferedReader in=new BufferedReader (new InputStreamReader (socket.getInputStream ()
BufferedWriter out=new BufferedWriter (new OutputStreamWriter (socket.getOutputStream ()
BufferedReader br = new BufferedReader (new InputStreamReader (System.in))
While (true) {
String str=in.readLine ()
System.out.println ("client says:" + str)
String str2 = ""
Str2 = br.readLine (); / / until\ n, so be sure to enter a newline character!
Out.write (str2+ "\ n")
Out.flush ()
If (str.equals ("end"))
Break
}
In.close ()
Out.close ()
Socket.close ()
}
}
[example 2] client of Socket with two-way communication between client and server
Import java.io.BufferedReader
Import java.io.BufferedWriter
Import java.io.IOException
Import java.io.InputStreamReader
Import java.io.OutputStreamWriter
Import java.net.InetAddress
Import java.net.Socket
Import java.net.UnknownHostException
Public class Client {
Static Socket server
Public static void main (String [] args) {
Try {
Server = new Socket (InetAddress.getLocalHost (), 8888)
BufferedReader in = new BufferedReader (new InputStreamReader (
Server.getInputStream ())
BufferedWriter out = new BufferedWriter (new OutputStreamWriter (
Server.getOutputStream ())
BufferedReader wt = new BufferedReader (new InputStreamReader (
System.in))
While (true) {
String str = wt.readLine ()
Out.write (str + "\ n")
Out.flush ()
If (str.equals ("end")) {
Break
}
System.out.println ("Server says:" + in.readLine ())
}
Out.close ()
In.close ()
Wt.close ()
Server.close ()
} catch (UnknownHostException e) {
/ / TODO Auto-generated catch block
E.printStackTrace ()
} catch (IOException e) {
/ / TODO Auto-generated catch block
E.printStackTrace ()
}
}
}
After learning the above procedures, we will find that we must follow the arranged order, the server and the client to ask and answer! Not flexible enough! So how to use multithreading to achieve more flexible two-way communication? Let's continue in the next section.
"full Stack Java Notes" is a series of Java engineer notes that can help you grow from zero to one. The author, known as Mr. G, has 10 years of experience in Java research and development. He has been engaged in software design and development in a research and development center of China Digital and Aerospace Academy, and has gradually become an engineer, senior engineer and architect. Proficient in Java platform software development, proficient in JAVAEE, familiar with various popular development frameworks.
The notes include six parts from shallow to deep:
Getting started with A-Java
B-database from entry to proficiency
C-hand blade moving front end and Web front end
D-J2EE from knowledge to actual combat
E-Java high-level framework refinement
F-Linux and Hadoop
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.