ChatClient.java~2~
资源名称:cs_chat.rar [点击查看]
上传用户:dst675
上传日期:2022-07-17
资源大小:25k
文件大小:3k
源码类别:
界面编程
开发平台:
Java
- package room;
- /**
- * <p>Title: </p>
- * <p>Description: </p>
- * <p>Copyright: Copyright (c) 2010</p>
- * <p>Company: </p>
- * @author not attributable
- * @version 1.0
- */
- 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
- {
- connect2Server();//创建用于连接的Socket
- 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 streamsn");
- }
- 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("接收到未知类型的信息!");
- }
- }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("n写信息发生错误!");
- }
- }
- public static void main(String args[])
- {
- ChatClient beginning;
- if(args.length==0)
- beginning = new ChatClient("10.0.1.112");
- else
- beginning= new ChatClient(args[0]);
- beginning.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- beginning.connectClient();
- }
- }