ChatServer.java~5~
资源名称:cs_chat.rar [点击查看]
上传用户:dst675
上传日期:2022-07-17
资源大小:25k
文件大小:4k
源码类别:
界面编程
开发平台:
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.*;
- import java.net.Socket;
- 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
- {
- toserver = new ServerSocket(4000,100);
- while(true)
- {
- wait4Connection();//等待连接
- getStreams();//获取输入输出流
- processConnection();//处理连接
- closeConnection();//关闭连接
- ++counter;
- }
- }
- catch(EOFException eofException)
- {//捕获异常
- System.out.println("客户端连接错位!");
- }
- catch(IOException ioException)
- {
- ioException.printStackTrace();
- }
- }
- private void wait4Connection() throws IOException
- {
- outFrame.setText("等待连接~~~n");
- connection = toserver.accept();//侦听并接受到此套接字的连接。此方法在进行连接之前一直阻塞。
- outFrame.append("连接"+counter+"来自:"+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 streamsn");
- }
- private void processConnection() throws IOException
- {//处理客户端连接,连接成功
- String message = "服务器端连接成功!";
- outputS.writeObject(message);//将指定的对象写入 ObjectOutputStream
- 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("接收到未知类型的信息!");
- }
- }while(!message.equals("客户端>>TERMINATE"));
- }
- private void closeConnection() throws IOException //关闭socket
- {
- outFrame.append("n用户 terminated 连接!");
- 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("n写信息发生错误!");
- }
- }
- public static void main(String args[])
- {
- ChatServer process = new ChatServer();
- process.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- process.connectServer();
- }
- }