?? chatclient.java
字號:
/* 聊天程序之客戶端ChatClient.java
* 建立客戶端以獲取服務器端發送信息并顯示
*/
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ChatClient extends JFrame {
private JTextField inputBox;
private JTextArea outFrame;
private ObjectOutputStream outputS;
private ObjectInputStream inputS;
private String message = "";
private String chatServer;
private Socket toclient;
// 初始化聊天服務
public ChatClient( String srvhost )
{
super( "Client" );
chatServer = srvhost;
// 設置客戶端連接的服務器
Container container = getContentPane();
inputBox = new JTextField();
// 建立輸入框
inputBox.setEnabled( false );
inputBox.addActionListener(
//監聽
new ActionListener() {
public void actionPerformed( ActionEvent event )
{
sendMsg( event.getActionCommand() );
}
// 向服務器端發送消息
}
);
container.add( inputBox, BorderLayout.NORTH );
outFrame = new JTextArea();
container.add( new JScrollPane( outFrame ),
BorderLayout.CENTER );
setSize( 280, 160 );
setVisible( true );
// 輸出輸出框
}
// 連接服務器端
public void connectClient()
{
try {
// 創建用于連接的Socket
connect2Server();
// 得到輸入輸出流
getStreams();
// 建立連接
processConnection();
// 關閉連接
closeConnection();
}
catch ( EOFException eofException ) {
System.out.println( "Server terminated connection" );
}
catch ( IOException ioException ) {
ioException.printStackTrace();
}
//捕獲異常
}
// 獲取輸入輸出流
private void getStreams() throws IOException
{
// 輸出
outputS = new ObjectOutputStream(
toclient.getOutputStream() );
outputS.flush();
// 輸入
inputS = new ObjectInputStream(
toclient.getInputStream() );
outFrame.append( "\nGet I/O streams\n" );
}
// 連接服務器端
private void connect2Server() throws IOException
{
outFrame.setText( "連接中……\n" );
toclient = new Socket(
InetAddress.getByName( chatServer ), 4000 );
// 連接信息顯示
outFrame.append( "連接至: " +
toclient.getInetAddress().getHostName() );
}
private void processConnection() throws IOException
{
// 輸出框
inputBox.setEnabled( true );
do {
// 讀入信息并輸出
try {
message = ( String ) inputS.readObject();
outFrame.append( "\n" + message );
outFrame.setCaretPosition(
outFrame.getText().length() );
}
catch ( ClassNotFoundException classNotFoundException ) {
outFrame.append( "\nUnknown object type received" );
}
} while ( !message.equals( "服務器端>> TERMINATE" ) );
}
//關閉輸入輸出流、關閉連接,注意順序
private void closeConnection() throws IOException
{
outFrame.append( "\n關閉連接" );
outputS.close();
inputS.close();
toclient.close();
}
// 給服務器端發消息
private void sendMsg( String message )
{
try {
outputS.writeObject( "客戶端>> " + message );
outputS.flush();
outFrame.append( "\n客戶端>>" + message );
}
catch ( IOException ioException ) {
outFrame.append( "\nError writing object" );
}
}
//main()方法
public static void main( String args[] )
{
ChatClient beginning;
if ( args.length == 0 )
beginning = new ChatClient( "127.0.0.1" );
else
beginning = new ChatClient( args[ 0 ] );
beginning.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE );
beginning.connectClient();
}
} // end class ChatClient
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -