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

📄 client2.java

📁 C-S模式下提供主机间相互通信的功能
💻 JAVA
字号:

import java.io.*; 
import java.net.*; 
import java.awt.*; 
import java.awt.event.*; 

// Java extension packages 
import javax.swing.*; 

public class Client2 extends JFrame { 
private JTextField enterField; 
private JTextArea displayArea; 
private ObjectOutputStream output; 
private ObjectInputStream input; 
private String message = ""; 
private String chatServer; 
private Socket client; 

// initialize chatServer and set up GUI 
public Client2( String host ) 
{ 
super( "Client" ); 

// set server to which this client connects 
chatServer = host; 

Container container = getContentPane(); 

// create enterField and register listener 
enterField = new JTextField(); 
enterField.setEnabled( false ); 
//添加指定的操作侦听器以从此文本字段接收操作事件。
enterField.addActionListener( 

new ActionListener() { 

// send message to server 
public void actionPerformed( ActionEvent event ) 
{ 
sendData( event.getActionCommand() ); 

} 

} // end anonymous inner class 

); // end call to addActionListener 

container.add( enterField, BorderLayout.NORTH ); 

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

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

// connect to server and process messages from server 
public void runClient() 
{ 
// connect to server, get streams, process connection 
try { 

// Step 1: Create a Socket to make connection 
connectToServer(); 

// Step 2: Get the input and output streams 
getStreams(); 

// Step 3: Process connection 
processConnection(); 

// Step 4: Close connection 
closeConnection(); 
} 

// server closed connection 
catch ( EOFException eofException ) { 
System.out.println( "Server terminated connection" ); 
} 

// process problems communicating with server 
catch ( IOException ioException ) { 
ioException.printStackTrace(); 
} 
} 

// get streams to send and receive data 
private void getStreams() throws IOException 
{ 
// set up output stream for objects 
output = new ObjectOutputStream( 
client.getOutputStream() ); 

// flush output buffer to send header information 
output.flush(); 

// set up input stream for objects 
input = new ObjectInputStream( 
client.getInputStream() ); 

displayArea.append( "\nGot I/O streams\n" ); 
} 

// connect to server 
private void connectToServer() throws IOException 
{ 
displayArea.setText( "Attempting connection\n" ); 

// create Socket to make connection to server 
client = new Socket( 
InetAddress.getByName( chatServer ), 5000 ); 

// display connection information 
displayArea.append( "Connected to: " + 
client.getInetAddress().getHostName() ); 
} 

// process connection with server 
private void processConnection() throws IOException 
{ 
// enable enterField so client user can send messages 
enterField.setEnabled( true ); 

// process messages sent from server 
do { 

// read message and display it 
try { 
message = ( String ) input.readObject(); 
displayArea.append( "\n" + message ); 
displayArea.setCaretPosition( 
displayArea.getText().length() ); 
} 

// catch problems reading from server 
catch ( ClassNotFoundException classNotFoundException ) { 
displayArea.append( "\nUnknown object type received" ); 
} 

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

} // end method process connection 

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

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

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

// execute application 
public static void main( String args[] ) 
{ 
Client2 application; 

if ( args.length == 0 ) 
application = new Client2( "127.0.0.1" ); 
else 
application = new Client2( args[ 0 ] ); 

application.setDefaultCloseOperation( 
JFrame.EXIT_ON_CLOSE ); 

application.runClient(); 
} 

} 

⌨️ 快捷键说明

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