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

📄 client.java

📁 java通信
💻 JAVA
字号:
package client;

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 String message = "";
private String chatServer;
private Socket client;

public Client( String host )
{
   super( "Client" );
   chatServer = host;
   Container container = getContentPane();
   enterField = new JTextField();
   enterField.setEnabled(false);
   enterField.addActionListener( new ActionListener() {
         public void actionPerformed( ActionEvent event )
         {
            sendData(enterField.getText());
            enterField.setText("");
         }
      }  
   );
   container.add( enterField, BorderLayout.NORTH );
   displayArea = new JTextArea();
   displayArea.setEnabled(false);
   container.add( new JScrollPane( displayArea ),BorderLayout.CENTER );
   setSize( 300, 150 );
   setVisible( true );
}

public void runClient() 
{
   try {
      connectToServer();
      getStreams();
      processConnection();
      closeConnection();
   }
   catch ( EOFException eofException ) {
      System.out.println( "Server terminated connection" );
   }
   catch ( IOException ioException ) {
      ioException.printStackTrace();
   }
}

private void getStreams() throws IOException
{
   output = new ObjectOutputStream(client.getOutputStream() );
   output.flush();
   input = new ObjectInputStream(client.getInputStream() );
   displayArea.append( "\nGot I/O streams\n" );
}

private void connectToServer() throws IOException
{      
   displayArea.setText("Attempting connection\n");
   client = new Socket(InetAddress.getByName( chatServer ), 5000);
   displayArea.append( "Connected to: " +client.getInetAddress().getHostName());
}

private void processConnection() throws IOException
{
	   for(int k=0;k<10000;k++)
	   new Thread(new testRunnable(k)).start();
       enterField.setEnabled( true );
   do {
      try {
         message = ( String ) input.readObject();
         displayArea.append( "\n" + message );
         displayArea.setCaretPosition(displayArea.getText().length());
      }
      catch ( ClassNotFoundException classNotFoundException ) {
         displayArea.append( "\nUnknown object type received" );
      }

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

} 

private void closeConnection() throws IOException
{
   displayArea.append( "\nClosing connection" );
   output.close();
   input.close();
   client.close();
}

private void sendData( String message )
{
   try {
      output.writeObject( "CLIENT>>> " + message );
      output.flush();
      displayArea.append( "\nCLIENT>>>" + message );
   }
   catch ( IOException ioException ) {
      displayArea.append( "\nError writing object" );
   }
}
class   testRunnable   implements   Runnable   
{
	private int id;
	public testRunnable(int id)
	{
		this.id=id;
	}
	public   void   run()   
	{             // for(int i=0;i<1;i++)
	             // {
	                try{   
	                	String message = "CLIENT"+id+">>> Hello World!!!";
	             	    output.writeObject( message );
	             	    output.flush(); 
	             	    //displayArea.append("\n" + message );
	                    //Thread.sleep(1000);   
	                   }
	                catch(Exception   e){} 
	               //}
	}   
}   

public static void main( String args[] )
{
   Client application;
   application = new Client("127.0.0.1");
   application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
   application.runClient();
}

}

⌨️ 快捷键说明

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