⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 client.java

📁 一个小型的聊天软件源码,用JAVA编译,方便朋友们更了解JAVA语言
💻 JAVA
字号:
// Fig. 24.7: Client.java
// Client that reads and displays information sent from a Server.
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Client extends JFrame 
{
   private JTextField enterField; 
   private JTextArea displayArea; 
   //private ObjectOutputStream output;
   //private ObjectInputStream input;
   private BufferedReader input;
   private PrintWriter output;
   private String message = "";
   private Socket client;

   public Client()
   {
      super( "Client" );

      enterField = new JTextField(); 
      enterField.setEditable( false );
      enterField.addActionListener(
         new ActionListener() {
            public void actionPerformed( ActionEvent event )
            {
               sendData( event.getActionCommand() );
               enterField.setText( "" );
            }
         }
      );

      add( enterField, BorderLayout.NORTH );

      displayArea = new JTextArea();
      add( new JScrollPane( displayArea ), BorderLayout.CENTER );

      setSize( 300, 450 );
      setLocation(400,0);
      setVisible( true ); 
   }

   // connect to server and process messages from server
   public void runClient() 
   {
      try 
      {
         //connectToServer; 
         displayArea.append( "Attempting connection\n" );
         client = new Socket( "localhost", 12345 );
         displayArea.append( "Connected to: " +  client.getInetAddress().getHostName() );
         
         //getStreams;
         //output = new ObjectOutputStream( client.getOutputStream() );      
         //output.flush(); 
         //input = new ObjectInputStream( client.getInputStream() );
         displayArea.append( "\nGot I/O streams\n" );
         
         input = new BufferedReader(new InputStreamReader(client.getInputStream()));
         output = new PrintWriter(client.getOutputStream());
         
         processConnection();
      }
      catch ( EOFException eofException ) 
      {
         displayArea.append( "\nClient terminated connection" );
      } 
      catch ( IOException ioException ) 
      {
         ioException.printStackTrace();
      } 
      finally 
      {
         closeConnection();
      }
   }

   // process connection with server
   private void processConnection() throws IOException
   {
      enterField.setEditable( true );

      do
      { 
         try 
         {
            //message = ( String ) input.readObject(); 
            message = input.readLine();
            displayArea.append( "\n" + message );
         }
         catch ( IOException ex ) 
         {
            displayArea.append( "\nUnknown object type received" );
         }

      } while ( !message.equals( "SERVER>>> END" ) );
   }

   // close streams and socket
   private void closeConnection() 
   {
      displayArea.append( "\nClosing connection" );
      enterField.setEditable( false ); 

      try 
      {
         input.close();
         output.close();
         client.close();
      }
      catch ( IOException ioException ) 
      {
         ioException.printStackTrace();
      }
   }

   // send message to server
   private void sendData( String message )
   {
      try
      {
         //output.writeObject( "CLIENT>>> " + message );
         output.println("CLIENT>>> " + message);
         output.flush();
         displayArea.append( "\nCLIENT>>> " + message );
      }
      catch ( Exception ioException )
      {
         displayArea.append( "\nError writing object" );
      }
   } 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -