- 客户端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();
}
}
- 聊天程序之服务器端ChatServer.java: 建立服务器端,接收客户端连接,发送信息、关闭连接。
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ChatServer extends JFrame {
private JTextField inputBox;
private JTextArea outFrame;
private ObjectOutputStream outputS;
private ObjectInputStream inputS;
private ServerSocket toserver;
private Socket connection;
private int counter = 1;
public ChatServer()
{
super( "Server" );
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 connectServer()
{
try {
// 创建一个ServerSocket.
toserver = new ServerSocket( 4000, 100 );
while ( true ) {
// 等待连接
wait4Connection();
// 获取输入输出流
getStreams();
// 处理连接
processConnection();
// 关闭连接
closeConnection();
++counter;
}
}
catch ( EOFException eofException ) {
System.out.println( "Client terminated connection" );
}
//捕获异常
catch ( IOException ioException ) {
ioException.printStackTrace();
}
}
private void wait4Connection() throws IOException
{
outFrame.setText( "等待连接……\n" );
connection = toserver.accept();
outFrame.append( "Connection " + counter +
"from: " +
connection.getInetAddress().getHostName()
);
}
private void getStreams() throws IOException
{
// 设置输出流
outputS = new ObjectOutputStream(
connection.getOutputStream()
);
outputS.flush();
// 设置输入流
inputS = new ObjectInputStream(
connection.getInputStream()
);
outFrame.append( "\nGet I/O streams\n" );
}
//处理客户端连接
private void processConnection() throws IOException
{
// 连接成功
String message = "服务器端>> 连接成功";
outputS.writeObject( message );
outputS.flush();
// 输入框
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" ) );
}
// 关闭socket
private void closeConnection() throws IOException
{
outFrame.append( "\nUser terminated connection" );
inputBox.setEnabled( false );
outputS.close();
inputS.close();
connection.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[] )
{
ChatServer process = new ChatServer();
process.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE );
process.connectServer();
}
} // end class ChatServer
|