This example for you to share a simple socket chat tool, I hope you like.
The code runs like a picture, it looks good.
Service side
Package Qiu;
Import Java.awt.BorderLayout;
Import java.awt.event.ActionEvent;
Import Java.awt.event.ActionListener;
Import java.awt.print.Printable;
Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import Java.io.PrintWriter;
Import Java.net.ServerSocket;
Import Java.net.Socket;
Import Java.util.Calendar;
Import Javax.swing.JButton;
Import Javax.swing.JFrame;
Import Javax.swing.JPanel;
Import Javax.swing.JScrollPane;
Import Javax.swing.JTextArea;
Import Javax.swing.JTextField; /** * Simple Chat Software server */public class MyServer extends JFrame implements actionlistener{JTextArea JTextArea =null;//used to display
A single-line area of plain text JTextField jtextfield=null;//can be used to edit single-line text JButton sendbutton=null;
JPanel Jpanel=null;
JScrollPane JScrollPane =null;
Send the information to the client object PrintWriter printwriter =null;
/** * Service-side main function */public static void main (string[] args) {//TODO auto-generated method Stub new MyServer ();}/** * Service-side constructor for initialization */public MyServer () {//ThisIs the initialization of the GUI JTextArea = new JTextArea ();
jtextfield= New JTextField (20);
sendbutton= New JButton ("send");
Sendbutton.addactionlistener (this);
Sendbutton.setactioncommand ("send");
Jscrollpane= new JScrollPane (JTextArea);
JPanel = new JPanel (); Jpanel.add (JTextField);//Add edit box Jpanel.add (Sendbutton);//Add button//Add layout to two panels this.add (jscrollpane,borderlayout.center
);
This.add (Jpanel,borderlayout.south);
This.setsize (400,300);
This.settitle ("chat server");
This.setdefaultcloseoperation (jframe.exit_on_close);/Set Exit button This.setvisible (TRUE);
This.setresizable (TRUE);
The following is the socket server's build try {//server monitor ServerSocket SS = new ServerSocket (9988);
Wait for client to connect socket socket = ss.accept ();
Gets the stream of data sent over by the client bufferedreader br = new BufferedReader (New InputStreamReader (Socket.getinputstream ()));
PrintWriter = new PrintWriter (Socket.getoutputstream (), true);
Read the information sent from the client while (true) {String info = br.readline ();
Jtextarea.append ("Client" +gettime () + "\ r \ n" +info+ "\ r \ n");
}catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
/** * Used to get current time * @return Current time */public String GetTime () {//Can be modified for each individual time domain Calendar c = calendar.getinstance ();
int hour = C.get (calendar.hour_of_day);//Get hours int minute = C.get (Calendar.minute);
int second = C.get (Calendar.second);
Return hour+ ":" +minute+ ":" +second;
/** * When button is clicked call/@Override public void actionperformed (ActionEvent e) {//TODO auto-generated stub Call if (E.getactioncommand (). Equals ("Send") when the button is pressed {//Send the server's JTextField write to client String info= Jtextfield.gettext ()
;
Jtextarea.append ("Server" +gettime () + "\ r \ n" +info+ "\ r \ n");
PRINTWRITER.PRINTLN (info);
Clearly send box content Jtextfield.settext ("");
}
}
}
Client
Package Qiu;
Import Java.awt.BorderLayout;
Import java.awt.event.ActionEvent;
Import Java.awt.event.ActionListener;
Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import Java.io.PrintWriter;
Import Java.net.Socket;
Import java.net.UnknownHostException;
Import Java.util.Calendar;
Import Javax.swing.JButton;
Import Javax.swing.JFrame;
Import Javax.swing.JPanel;
Import Javax.swing.JScrollPane;
Import Javax.swing.JTextArea;
Import Javax.swing.JTextField;
/** * Simple Chat Software Client * * */public class MyClient extends JFrame implements actionlistener{JTextArea Jtextarea=null;
JTextField Jtextfield=null;
JPanel Jpanel=null;
JScrollPane Jscrollpane=null;
JButton Sendbutton=null;
PrintWriter Printwriter=null;
/** * Client Main function */public static void main (string[] args) {//TODO auto-generated method Stub new MyClient ();}/**
* Client constructors are used to initialize */public myclient () {//gui Initialize jtextarea= new JTextArea ();
Jtextfield=new JTextField (20); Sendbutton= New JButton ("send");
Sendbutton.addactionlistener (this);
Sendbutton.setactioncommand ("send");
Jscrollpane=new JScrollPane (JTextArea);
Jpanel=new JPanel ();
Jpanel.add (JTextField);
Jpanel.add (Sendbutton);
This.add (Jscrollpane,borderlayout.center);
This.add (Jpanel,borderlayout.south);
This.setsize (400, 300);
This.settitle ("chat client");
This.setdefaultcloseoperation (Jframe.exit_on_close);
This.setvisible (TRUE);
This.setresizable (TRUE);
Socket communication code try {socket s= new socket ("127.0.0.1", 9988);
BufferedReader br = new BufferedReader (New InputStreamReader (S.getinputstream ()));
printwriter= New PrintWriter (S.getoutputstream (), true);
while (true) {//Non-stop reads the information sent over by the server String string=br.readline ();
Jtextarea.append ("Server" +gettime () + "\ r \ n" +string+ "\ r \ n");
The catch (Unknownhostexception e) {//TODO auto-generated catch block E.printstacktrace ();
catch (IOException e) {//TODO auto-generated catch block E.printstacktrace (); }}/** * used to obtainCurrent time * @return Current time */public String GetTime () {//Can be modified for each individual time field Calendar C = calendar.getinstance ();
int hour = C.get (calendar.hour_of_day);//Get hours int minute = C.get (Calendar.minute);
int second = C.get (Calendar.second);
Return hour+ ":" +minute+ ":" +second; /** * When button is clicked call/@Override public void actionperformed (ActionEvent e) {/TODO auto-generated stub I
F (Sendbutton.getactioncommand (). Equals ("send")) {String info= jtextfield.gettext ();
Sends the information sent by the client to the service side jtextarea.append ("Client" +gettime () + "\ r \ n" +info+ "\ r \ n");
PRINTWRITER.PRINTLN (info);
Jtextfield.settext ("");
}
}
}
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.