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)05/31 Report--
This article mainly introduces the relevant knowledge of "how to use java socket to achieve LAN chat". The editor shows you the operation process through an actual case. The operation method is simple and fast, and it is practical. I hope this article "how to use java socket to achieve LAN chat" can help you solve the problem.
The code is simply divided into two classes, a server class and a client, and the runtime starts two threads, one responsible for receiving and the other responsible for sending.
The whole process is that two machines start the program respectively, one chooses to connect actively and the other chooses to accept passively, which can achieve the chat effect similar to qq. Two people can send messages at the same time.
Note that do not start both on the same computer at the same time. This will cause the ip of both the server and the client to be the same. It is very likely that the messages you send will be received by yourself, which can be carried out on both computers or virtual machines.
Server:
Package com.server; import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.net.ServerSocket;import java.net.Socket; import com.client.RunClient; public class RunServer {public static Socket socket=null; / * establish a server passive connection * @ throws IOException * / public void startServer () throws IOException {/ / the server receives data ServerSocket serverSocekt=new ServerSocket (8888) System.out.println ("server starts, waiting for connection..."); / / create client thread RunClient rc=new RunClient (); Thread rct=new Thread (rc); rct.start (); / / block thread listening port to establish socket session RunServer.socket=serverSocekt.accept () / / get input InputStream is=socket.getInputStream (); / / convert byte input stream to character input stream InputStreamReader isr=new InputStreamReader (is); / / load character input stream into buffered input stream BufferedReader br=new BufferedReader (isr); String str=null While ((str=br.readLine ())! = null) {System.out.print ("output:" + str);} socket.shutdownInput (); serverSocekt.close ();}}
Client:
Package com.client; import java.io.BufferedWriter;import java.io.OutputStream;import java.io.OutputStreamWriter;import java.net.Socket;import java.net.UnknownHostException;import java.util.Scanner; import com.server.RunServer; public class RunClient implements Runnable {@ Override public void run () {/ / TODO Auto-generated method stub Scanner input = new Scanner (System.in); System.out.println ("1, active connection\ N2, passive waiting") String sign=input.next (); switch (sign) {case "1": try {System.out.println ("input server IP:"); startClient (input.next ());} catch (Exception e) {e.printStackTrace ();} break Case "2": try {startClient ();} catch (Exception e) {e.printStackTrace ();} break; default: break }} / * reverse connection is made by the established socket * @ throws Exception * @ throws UnknownHostException * / public void startClient () throws Exception {while (true) {try {if (RunServer.socketcards null) {break } Thread.sleep (1000);} catch (InterruptedException e) {e.printStackTrace ();}} / / establish a connection with the server Socket socket=new Socket (RunServer.socket.getInetAddress (). GetHostAddress (), 8888) System.out.println ("connection established successfully..."); / / write stream OutputStream os=socket.getOutputStream () to the server; BufferedWriter bw=new BufferedWriter (new OutputStreamWriter (os)); Scanner input=new Scanner (System.in); while (true) {System.out.println ("input Information:"); bw.write (input.next ()) Bw.newLine (); bw.flush ();}} / * establish a connection to the server through IP * @ throws Exception * @ throws UnknownHostException * / public void startClient (String ip) throws Exception {/ / establish a connection to the server Socket socket=new Socket (ip, 8888) System.out.println ("connection established successfully..."); / / write stream OutputStream os=socket.getOutputStream () to server; BufferedWriter bw=new BufferedWriter (new OutputStreamWriter (os)); Scanner input=new Scanner (System.in); while (true) {bw.write (input.next ()); bw.newLine () Bw.flush ();}
Main:
Package com.start; import com.server.RunServer; public class Go {public static void main (String [] args) throws Exception {/ / TODO Auto-generated method stub RunServer rs=new RunServer (); rs.startServer ();}}
Effect: I use a virtual machine plus a local machine.
This is the end of the content about "how to use java socket to achieve LAN chat". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
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.