Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to introduce the code of Java Socket communication client and server

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about how to introduce the code of Java Socket communication client and server, which may not be well understood by many people. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

Java Socket communication has a lot of things to pay attention to in our usual use, first of all, the code configuration in the client and server segments. In fact, it is very simple, as long as the integrity and usability of the basic code are guaranteed. Now let's learn about the code related to Java Socket communication.

ClientTest.java client @ author @ version1.002008/8/2 / importjava.net.Socket; importjava.io.IOException; importjava.io.InputStreamReader; importjava.io.BufferedReader; importjava.io.PrintWriter; publicclassClientTest {privateSocketsocket; BufferedReaderin; PrintWriterout; publicClientTest () {try {System.out.println ("Trytoconnectto127.0.0.1:10000"); / / issue a connection request socket=newSocket ("127.0.0.1", 10000) to the server System.out.println ("TheServerConnected!"); System.out.println ("Pleaseentersomecharacters:"); / / read user input information BufferedReaderline=newBufferedReader (newInputStreamReader (System.in)); / / output information obtained from the server side out=newPrintWriter (socket.getOutputStream (), true); out.println (line.readLine ()); / / read server-side information in=newBufferedReader (newInputStreamReader (socket.getInputStream (); System.out.println (in.readLine ()); out.close () In.close (); socket.close ();} catch (IOExceptionex) {System.out.println (ex.getMessage ());}} publicstaticvoidmain (String [] args) {newClientTest ();}} / @ (#) ServerSocketTest.java server @ author @ version1.002008/8/2 / importjava.net.ServerSocket; importjava.net.Socket; importjava.io.IOException; importjava.io.BufferedReader; importjava.io.InputStreamReader; importjava.io.PrintWriter PublicclassServerSocketTest {privateServerSocketss; privateSocketsocket; privateBufferedReaderin; privatePrintWriterout; publicServerSocketTest () {try {ss=newServerSocket (10000); / / set up a server and listen. System.out.println ("Serverislisteningat10000..."); while (true) {socket=ss.accept (); / / get the client IP address StringremoteIP=socket.getInetAddress (). GetHostAddress (); / / get the client connection port StringremotePort= ":" + socket.getLocalPort (); System.out.println ("Aclinetcomein connection IP:" + remoteIP+remotePort); / / read the client input in=newBufferedReader (newInputStreamReader (socket.getInputStream (); Stringline=in.readLine (); System.out.println ("Clientsendis:" + line) / send server-side information to client out=newPrintWriter (socket.getOutputStream (), true); out.println ("YourMessageReceived!"); out.close (); in.close (); socket.close ();}} catch (IOExceptionex) {System.out.println (ex.getCause ());}} publicstaticvoidmain (String [] args) {newServerSocketTest () }} after reading the above, do you have any further understanding of how to introduce the code of Java Socket communication client and server? 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report