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

📄 server.java

📁 this file contains java source code to connect two computers in a network
💻 JAVA
字号:
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Server extends JFrame {
   private JTextField enterField;
   private JTextArea displayArea;
   private ObjectOutputStream output;
   private ObjectInputStream input;
   private ServerSocket server;
   private Socket connection;
   private int counter = 1;

   // set up GUI
   public Server(){
      super( "Server" );
      Container container = getContentPane();

      // create enterField and register listener
      enterField = new JTextField();
      enterField.setEditable(false);
      enterField.addActionListener(
         new ActionListener() 
         { 
      // send message to client
            public void actionPerformed(ActionEvent event)
            {
               sendData( event.getActionCommand() );
               enterField.setText( "" );
            }
         }  
      ); 

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

      setSize( 300, 150 );
      setVisible( true );

   } 
   // set up and run server 
   public void runServer()
   {
           try{

    server = new ServerSocket( 12345, 100 );

   while ( true ) {

            try {
               waitForConnection(); 
               getStreams();        
               processConnection(); 
            }

            // process EOFException when client closes connection 
            catch ( EOFException eofException ){
               System.err.println( "Server terminated connection" );
            }

            finally {
               closeConnection();   
               ++counter;
            }

         } 

      } 

      // process problems with I/O
      catch ( IOException ioException ) 
      {
         ioException.printStackTrace();
      }

   }

   // wait for connection to arrive, then display connection information
   private void waitForConnection() throws IOException
   {
      displayMessage( "Waiting for connection\n" );
      connection = server.accept(); // allow server to accept connection            
      displayMessage( "Connection " + counter + " received from: " +
     connection.getInetAddress().getHostName() );
   }

   // get streams to send and receive data
   private void getStreams() throws IOException
   {
      output = new ObjectOutputStream( connection.getOutputStream() );
     
      input = new ObjectInputStream( connection.getInputStream() );

  
   }

   // process connection with client
   private void processConnection() throws IOException
   {
      String message = "Connection successful";
      sendData( message );

      setTextFieldEditable( true );

      do
      { 

         try
          {
            message = ( String ) input.readObject();
            displayMessage( "\n" + message );
         }
         catch ( ClassNotFoundException classNotFoundException ) 
         {
            displayMessage( "\nUnknown object type received" );
         }

      } while ( !message.equals( "CLIENT>>> TERMINATE" ) );

   } 

   // close streams and socket
   private void closeConnection() 
   {
      displayMessage( "\nTerminating connection\n" );
      setTextFieldEditable( false ); // disable enterField

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

   // send message to client
   private void sendData( String message )
   {
      try
       {
         output.writeObject( "SERVER>>> " + message );
         
         displayMessage( "\nSERVER>>> " + message );
      }

      catch ( IOException ioException ) 
      {
         displayArea.append( "\nError writing object" );
      }
   }

   // utility method called from other threads to manipulate 
   // displayArea in the event-dispatch thread
   private void displayMessage( final String messageToDisplay )
   {
      SwingUtilities.invokeLater
      (
         new Runnable() 
         {  

            public void run() 
            {
               displayArea.append( messageToDisplay );
               displayArea.setCaretPosition( 
               displayArea.getText().length() );
            }

         } 

      ); 
   }

   // utility method called from other threads to manipulate 
   // enterField in the event-dispatch thread
   private void setTextFieldEditable( final boolean editable )
   {
     
      SwingUtilities.invokeLater
      (
         new Runnable() 
         {  

            public void run()  
            {
               enterField.setEditable( editable );
            }

         } 

      ); 
   }

   public static void main( String args[] )
   {
      Server application = new Server();
      application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
      application.runServer();
   }
}  

⌨️ 快捷键说明

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