In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains how to solve the problem that the server does not receive the message when the Java Socket customer server sends the message for the second time. The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to solve the problem that the server does not receive the message when the Java Socket customer server sends the message for the second time.
Small problem one:
When the client uses the following code, the server cannot receive the client information.
PrintWriter pw = new PrintWriter (new BufferedOutputStream (os)); pw.print ("this is a clent:" + I); pw.flush ()
At this point, I change the code to the following so that the server can see the information sent by the client.
Os = socket.getOutputStream (); PrintWriter pw = new PrintWriter (new BufferedOutputStream (os)); pw.print ("this is a clent:" + I + "\ n"); / / or pw.println ("this is a clent:" + I); pw.flush ()
The difference between these two pieces of code is that one code does not add a newline character while the other code adds a newline character.
Small question two:
After the server is turned on, the client will not check the connection of the code after the Socket code is created. As a result, when the code executes the following code, the connection of the server is closed and the Connection refused (Connection refused) prompt cannot appear.
Socket = new Socket ("127.0.0.1", 10000)
The complete code is posted below:
Client:
Public static void main (String [] args) {Socket socket = null; OutputStream os = null; try {int iTuno; socket = new Socket ("127.0.0.1", 10000); while (true) {iTunes; os = socket.getOutputStream (); PrintWriter pw = new PrintWriter (new BufferedOutputStream (os)) Thread.sleep (1000L); pw.print ("this is a clent:" + I + "\ n"); pw.flush (); / receive the message from the server InputStream is = socket.getInputStream (); BufferedReader br = new BufferedReader (new InputStreamReader (new BufferedInputStream (is) System.out.println ("Server transfer Information:" / * + br.readLine () * / + " Server information: "+ socket.getInetAddress (). GetHostName ();}} catch (IOException | InterruptedException e) {e.printStackTrace ();} finally {try {os.close (); socket.close ();} catch (IOException e) {e.printStackTrace () }
Server:
Public static void main (String [] args) {try {ServerSocket serverSocket = new ServerSocket (10000); Socket socket = null; InputStream inputStream = null; while (true) {socket = serverSocket.accept (); inputStream = socket.getInputStream (); BufferedReader bufferedReader = new BufferedReader (new InputStreamReader (inputStream)) System.out.println ("the server receives a client message:" + bufferedReader.readLine () + " Client information "+ socket.getInetAddress (). GetHostAddress () +": "+ socket.getPort ()); / / feedback message OutputStream os = socket.getOutputStream (); PrintWriter pw = new PrintWriter (new OutputStreamWriter (new BufferedOutputStream (os); pw.println (" receive data transmitted by the client, server information "+ socket.getLocalSocketAddress () .toString ()); pw.flush () }} catch (IOException e) {e.printStackTrace () }} Thank you for your reading. the above is the content of "how to solve if the server does not receive the message when the Java Socket customer server sends the message for the second time". After the study of this article, I believe you have a deeper understanding of how to solve the problem that the server does not receive the message when the Java Socket customer server sends the message for the second time, and the specific usage still needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.