I. Overview
1. Objective: To establish a TCP protocol-based chat system
2. Idea: Programming with Java sockets
Second, the Code
1.chatserver.java
import Java.io.datainputstream;import Java.io.eofexception;import Java.io.ioexception;import Java.net.serversocket;import Java.net.socket;import java.net.UnknownHostException; public class Chatserver{public static void Main (string[] args) {DataInputStream dis = null; Socket s = null; ServerSocket SS = Null;boolean started =false;try {ss = new ServerSocket (8888);} catch (IOException E1) {E1.printstacktrac E ();} try {started = True;while (started) {Boolean bconnect = false;//!!!!! Note that accept () is a blocking method that is not executed when it is blocked by readUTF () s = ss.accept (); SYSTEM.OUT.PRINTLN ("A Client connect------" +s);d is = new DataInputStream (S.getinputstream ()); bconnect = True;while ( Bconnect) {//!!!!! Note that readUTF () is a blocking method System.out.println (Dis.readutf ());}}} catch (Eofexception e) {System.out.println ("client is OFF!) ");} catch (IOException e) {e.printstacktrace ();} finally{try {if (dis! = null) dis.close (); if (s! = null) s.close (); if (ss! = null) Ss.close ();} catch (Exception e) {E.pri Ntstacktrace ();}}}
2.chatclient.java
Import Java.awt.borderlayout;import java.awt.button;import Java.awt.frame;import Java.awt.textarea;import Java.awt.textfield;import Java.awt.event.actionevent;import Java.awt.event.actionlistener;import Java.awt.event.keyevent;import Java.awt.event.windowadapter;import Java.awt.event.windowevent;import Java.io.dataoutputstream;import Java.io.ioexception;import Java.io.outputstream;import Java.net.Socket;import Java.net.unknownhostexception;public class ChatClient extends Frame{private TextField tftext;private TextArea Tacontent;//private Button btnsend;private Socket socket;private dataoutputstream dos;public void Launchframe () { Addwindowlistener (New Windowadapter () {public void windowclosing (WindowEvent e) {disconnect (); System.exit (0); }}); tftext = new TextField (); tacontent = new TextArea ();//btnsend = New button ("send");////btnsend.addactionlistener (new ActionListener () {//@Override//public void actionperformed (ActionEvent e) {////tacontent.settext (Tacontent.gettext ( ) + "\n\r" +tftext.gEttext ());////tftext.settext ("");//try {//dataoutputstream dos = new DataOutputStream (Socket.getoutputstream ());// Dos.writeutf (Tftext.gettext ());//dos.close ();//socket.close ();/} catch (IOException E1) {//e1.printstacktrace (); /}//}//}); Tftext.addactionlistener (new Tflistener ()); Add (Tacontent,borderlayout.north); Add (Tftext, Borderlayout.center);//add (Btnsend,borderlayout.south); setlocation (+);p ack (); setvisible (true); Connect (" localhost ", 8888);} When this method is called, the member variable is automatically given to the socket connection on serverpublic void Connect (String address, int port) {try {socket = new socket (address, port); DOS = new DataOutputStream (Socket.getoutputstream ());} catch (Unknownhostexception e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ()}} Release resources on exit public void disconnect () {try {if (dos = null) dos.close (); if (socket! = NULL) Socket.close ();} catch (Ioexceptio n e) {e.printstacktrace ();}} Class Tflistener implements actionlistener{@Overridepublic void actionperformed (ActionEvent e) {Tacontent.settext ( TacontEnt.gettext (). Trim () + "\n\r" +tftext.gettext ()), try {Dos.writeutf (Tftext.gettext ());d Os.flush (), Tftext.settext ("" );//dos.close ();//socket.close ();} catch (IOException E1) {e1.printstacktrace ();}}} public static void Main (string[] args) {new ChatClient (). Launchframe ();}}
Iii. Results of operation
java-Online chat system-Non-threading