Socket-based simple instant messaging program written in Java

xiaoxiao2021-04-07  372

/ ** /

/* *Server */

Package

edu.jlu.fuliang;

Import

Java.io.

*

;

Import

Java.net.

*

;

Import

Java.awt.event.Actionevent;

Import

Java.awt.event.actionListener;

Import

Java.awt.event.keyadapter;

Import

Java.awt.event.keyevent;

Import

Javax.swing.

*

;

public

Class

Server

Extends

JFrame

IMPLEMENTS

Runnable

...

{Private ServerSocket server; private Socket connection; private OutputStream output; private InputStream input; private Thread outThread; private JTextArea display; private JTextField text1; private JButton startButton; public static void main (String args []) ... {Server s = New Server (); s.setdefaultcloseOperation (jframe.exit_on_close);} public server () ... {Super ("Server"); startButton = New JButton ("start the server"); text1 = new jtextfield (20); display = new JTextArea (7, 30); display.setEditable (false); Container container = getContentPane (); container.setLayout (new BorderLayout ()); container.add (startButton, BorderLayout.NORTH); container.add (new JScrollpane (Display), BorderLayout.center; Container.Add (Text1, BorderLayout.South); / ** // * * Add a listener to the start button. * / StartButton.addActionListener (New ActionListener () ... {PUBLIC VOID ActionPerformed (ActionEvent E) ... {Display.Settext ("Start Server ..."); StartButton.seTenabled (false); try ... { // port is set to 5000, the maximum connection request is 100 server = new serversocket (5000, 100); connection = server.accept (); output = connection.getOutputStream (); input = connection.getinputStream (); Output.write ("Connection is successful!" .Getbytes ()); Outthread = New Thread (server.this); Outthread.Start ();} catch (ooexception ee); / ** // * / * Add a keyboard listener to the text domain, press Enter to send information.

* / Text1.addKeylistener (new keyadapter () ... {public void keypressed (keyevent ke) ... {if (ke.getKeycode () == keyevent.vk_enter) ... {byte writebytes [] = new byte [ 50]; string s = "server:" text1.getText () "" "; text1.setText (" "); writebytes = s.getbytes (); display.Append (S " "); try ... { Output.write (Writebytes);} catch (ioException ee) ... {} if (s.trim (). Equals ("server: exit")) ... {Outthread.stop (); quit ();} }}}); Setsize (300, 400); setVisible (true);} public void run () ... {while ... {byte readbytes [] = new byte [50] ; Try ... {INPUT.READ (ReadBytes); // Read the message sent by the other party} catch (ooException e) ... {} string s = new string (readbytes); Display.Append (s "); IF (S.trim (). Equals ("client: exit")) Break;} quit ();} public void quit () ... {Try ... {Output.close (); Input.close () ; * /

/ * * Client * /

Package

edu.jlu.fuliang;

Import

Java.io.

*

;

Import

Java.net.

*

;

Import

Java.awt.event.Actionevent;

Import

Java.awt.event.actionListener;

Import

Java.awt.event.keyadapter;

Import

Java.awt.event.keyevent;

Import

Javax.swing.

*

;

public

Class

Client

Extends

JFrame

IMPLEMENTS

Runnable

...

{Private Socket client; private OutputStream output; private InputStream input; private Thread outThread; private JTextArea display; private JTextField text1; private JButton startButton; private JMenu loginMenu = new JMenu ( "login"); private JMenuItem register = new JMenuItem ( "Registration "); private JMenuItem login = new JMenuItem (" Login "); private JMenuBar bar = new JMenuBar (); private Register registerDlg; private Login loginDlg; private RandomAccessFile file; public static void main (String args []) ... { Client C = new client (); C.SETDEFAULTCLOSEOPERATION (JFrame.exit_on_close);} public client () ... {Super ("client"); startButton = New JButton ("Connect to Server"); Text1 = New JtextField 20); Display = New JtexTarea (7, 30); Display.setedItable (false); loginmenu.add (register); LoginMenu.Add (login); bar.add (loginmenu); setjmenubar (bar); container container = getContentPane (); Container.SetLayout (New BorderLayout ()); Container.Add (StartButton, BorderLayout.North); Container.Add (New JSC Rollpane (Display), BorderLayout.center; Container.Add (Text1, BorderLayout.South); try ... {file = new randomaccessfile (New File ("E: //login.txt"), "RW"); } Catch (ioException E1) ... {E1.PrintStackTrace (); registerdlg = new register (this); logindlg = new login (this); startButton.AddActionListener (new actionListener () ... {public Void ActionPerformed (ActionEvent E) ... {Display.Settext ("Connecting Server ..."); StartButton.senabled (false); try ... {client = new socket ("127.0.0.1", 5000); Output = Client.getOutputStream (); Input = Client.getInputStream (); OUTTHREAD =

New thread (client.this); outthread.start ();} catch (ioException ee); Text1.addKeylistener (new keyadapter () ... {public void keypressed (KeyEvent Ke) .. . {If (ke.getKeyCode () == keyevent.vk_enter) ... {byte writebytes [] = new byte [50]; string s = logindlg.getloginname () ":" text1.getText () " "; Text1.settext (" "); writebytes = s.getbytes (); display.Append (s " "); try ... {output.write (Writebytes);} catch (ooExcelt ee) ... {} IF (s.trim (). Equals (logindlg.getloginname () ": exit")) ... {OutthRead.stop (); quit ();}}}}; register.addActionListener (New ActionListener () ... {Public Void ActionPerformed (ActionEvent E) ... {registerdlg.setvisible (true);}}); login.addactionListener (new actionListener () ... {public void actionPerformed (ActionEvent E) ... {logindlg .setvisible (TRUE); Setsize (300, 400); setResizable (false); setvisible (true);} public void run () ... {while (true) ... {byte ReadBytes [] = new byte [1024]; Try ... {INPUT.READ (ReadBytes);} catch (ooException e) ... {} string s = new string (readbytes); Display.Append (S "") ; If (s.trim (). Equals ("Server: exit")) Break;} quit ();} public void quit () ... {Try ... {Output.close (); Input.Close ); Client.close (); Catch (ooException e) ... {} startbutton.setenabled (true);}} package

edu.jlu.fuliang;

Import

Java.aw.

*

;

Import

Java.awt.event.

*

;

Import

Java.io.

*

;

Import

Javax.swing.

*

;

public

Class

Login

Extends

JDIALOG

...

{Private JTextField textField; private JButton loginButton; private RandomAccessFile file; // save the registration information file private String loginName = "guest"; // save the name of the registrant for the landing for the guest; public Login (JFrame f, RandomAccessFile file) ... {Super (F, "Landing", False; this.file = file; jPanel panel = new jPanel (); panel.add ("Nickname:")); TextField = New JtextField (10) Panel.add (TextField); Container Container = getContentPane (); Container.SetLayout (New BorderLayout ()); Container.Add (Panel, BorderLayout.North); LoginButton = New JButton ("Landing"); Container.Add ( loginButton, BorderLayout.SOUTH); setVisible (false); setBounds (100,200,200,200); loginButton.addActionListener (new LoginListener ());} public String getLoginName () ... {return loginName;} / ** // * * Login listening When you click the login button, trigger the event * read from the file and find if you register, if you don't find *, you will pop up an unregistered warning.

Otherwise, Popked Welcome Dialog Form * Welcome * / Private Class LoginListener IMPLEments ActionListener ... {public void ActionPerformed (ActionEvent E) ... {Boolean Flag = False; try ... {string name = textfield.getText (). Trim (); TextField.Settext (""); file.seek (0); while (file.getfilepointer ()

edu.jlu.fuliang;

Import

Java.aw.

*

;

Import

Java.awt.event.

*

;

Import

Java.io.

*

;

Import

Javax.swing.

*

;

public

Class

Register

Extends

JDIALOG

...

转载请注明原文地址:https://www.9cbs.com/read-132503.html

New Post(0)