Java.net drill

xiaoxiao2021-03-06  16

A very simple LAN chat tool server side

/ *

* Created on 2005-3-18 * * Todo to change the Template for this generated file go to * window - preferences - java - code style - code templates * / package netchat;

Import javax.swing. *; import java.io. *; import java.Net. *; import java.awt. *; import java.awt.event. *;

/ ** * @author AppleTian * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates * / public class server extends JFrame {private JTextField messageBox; private JTextArea infBox; private ObjectInputStream input; private ObjectOutputStream output; private serverSocket server; private Socket serverSocket; public server () {super ( "server"); Container c = getContentPane (); messageBox = new JTextField ( ""); messageBox.setEnabled (false); messageBox.addActionListener (new ActionListener () {public void actionPerformed (ActionEvent e) {sendData (e.getActionCommand ());}}); c.add (messageBox, BorderLayout.NORTH); infBox = new JTextArea (); infBox. SetFont (New Font ("Serif", Font.Bold, 12)); C.Add (New Jscrollpane (Infbox), BorderLayout.Center); SetSize (500, 400); setvisible (TRUE);}

/ ** * @param actionCommand * / protected void sendData (String actionCommand) {try {output.writeObject (actionCommand); output.flush (); infBox.append ( "/ nsend <<" actionCommand ">> to client. ../n ");} catch (ioException e) {infbox.append (" / nexceptionioInsenddatafunction ... ");}} public void ruinserver ()} public void ruinserver ()} {Server = new Serversocket (5000, 100); while (True ) {waitForConnection (); getStreams (); processConnection (); closeConnection ();}} catch (EOFException e) {infBox.append ( "/ nError in runServerFuction");} catch (IOException e) {e.printStackTrace () }} / ** * * / private void closeConnection () throws oException {infbox.append ("/ nclose connection ..."); messagebox.setenabled (false); output.close (); input.close (); ServerSocket.close ();

/ ** * * / private void processConnection () throws IOException {String mg = "/ nconnection succeful"; messageBox.setEnabled (true); output.writeObject (mg); output.flush (); do {try {mg = ( Input.readObject (); infbox.append ("/ nsend to client:" mg); infbox.setcaretPosition (Infbox.getText (). Length ());} catch (classnotfoundexcection e) {infbox.Append (" / n unkown class received ... ");}} while (mg! =" end ");

/ ** * * / private void getStreams () throws IOException {output = new ObjectOutputStream (serverSocket.getOutputStream ()); output.flush (); input = new ObjectInputStream (serverSocket.getInputStream ()); infBox.append ( "/ Nget streams ... ");

/ ** * * / private void waitForConnection () throws IOException {infBox.append ( "wait for a Client ..."); serverSocket = server.accept (); infBox.append ( "connected by" serverSocket.getInetAddress ( ) .gethostname ());} public static void main (String [] args) {server my = new server (); my.setdefaultcloseoperation (jframe.exit_on_close); my.runserver ();}}

Client

/ * * Created on 2005-3-18 * * Todo to change the Template for this generated file go to * window - preferences - java - code style - code templates * / package netchat; import javax.swing. *;

Import java.io. *; import java.Net. *; import java.awt. *; import java.awt.event. *;

/ ** * @author AppleTian * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates * / public class client extends JFrame {private JTextField messageBox; private JTextArea infBox; private ObjectInputStream input; private ObjectOutputStream output; private String mg = ""; private String chatServer; private Socket client; public client (String host) {super ( "client ...."); chatServer = host; Container c = getContentPane () MessageBox = new jtextfield (); messagebox.setenabled (false); messagebox.addActionListener (new actionListener () {public void actionPerformed (ActionEvent E) {sendData (E.GetActionCommand ());}

}); C.ADD (MessageBox, BorderLayout.North); Infbox = New jtextarea (); C.Add (Infbox, BorderLayout.Center); Setsize (500, 400); setvisible (TRUE);}

/ ** * @param actionCommand * / protected void sendData (String actionCommand) {try {mg = messageBox.getText (); output.writeObject (mg); output.flush (); infBox.append ( "/ nsend to server: " mg);} catch (ioException e) {infbox.append (" / nioException in senddata ..);}} public void runclient ()}} (); getStreams (); processconnection (); closeconnection (); CloseConnection );} Catch (ioException e) {infbox.append (e.tostring ());}} / ** * * / private void closeconnection () THROWS IOEXCEPTION {// Todo auto-generated method stub infbo} ("/ Nclose ... "); Output.close (); Input.close (); client.close ();

}

/ ** * * / private void processConnection () throws IOException {// TODO Auto-generated method stub messageBox.setEnabled (true); do {try {mg = (String) input.readObject (); infBox.append ( "/ NFROM Server: " mg); Infbox.SetcaretPosition (InfboX.GetText (). Length ());} catch (classnotfoundexcection e) {infbox.append (" classnotfound ... / n ");}} while (mg! = "END");

/ ** * * / private void getStreams () throws IOException {// TODO Auto-generated method stub output = new ObjectOutputStream (client.getOutputStream ()); output.flush (); input = new ObjectInputStream (client.getInputStream () ); Infbox.append ("Get the IO stream ../ n");

/ ** * * / private void connectToServer () throws IOException {// TODO Auto-generated method stub infBox.append ( "try connecting to server ... / n"); client = new Socket (InetAddress.getByName (chatServer) , 5000); Infbox.Append ("Connect to Server:" Client.Getinetaddress (). Gethostname () "/ n");}

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

New Post(0)