一个简单的聊天程序
来源:互联网 发布:浙江手工艺品网络 编辑:程序博客网 时间:2024/05/13 05:58
一个简单的聊天程序(含服务器端和客户端),支持键盘(ENTER)按下发送信息。
服务器端代码:
- package myclass.cart;
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import javax.swing.border.*;
- import java.io.ObjectOutputStream;
- import java.io.*;
- /**
- * <p>Title: 服务器端</p>
- * <p>Description:服务器端程序 </p>
- * <p>Copyright: Copyright (c) 2010</p>
- * <p>Company: </p>
- * @author not attributable
- * @version 1.0
- */
- public class ServerFrame extends JFrame {
- JPanel contentPane;
- TitledBorder titledBorder1;
- TitledBorder titledBorder2;
- JScrollPane jScrollPane1 = new JScrollPane();
- JTextField jTextField1 = new JTextField();
- JButton jButton1 = new JButton();
- JEditorPane jEditorPane1 = new JEditorPane();
- TitledBorder titledBorder3;
- private ObjectOutputStream out;
- //Construct the frame
- public ServerFrame() {
- enableEvents(AWTEvent.WINDOW_EVENT_MASK);
- try {
- jbInit();
- }
- catch(Exception e) {
- e.printStackTrace();
- }
- }
- //Component initialization
- private void jbInit() throws Exception {
- contentPane = (JPanel) this.getContentPane();
- titledBorder1 = new TitledBorder("");
- titledBorder2 = new TitledBorder("");
- titledBorder3 = new TitledBorder("");
- contentPane.setLayout(null);
- this.setSize(new Dimension(410, 340));
- this.setTitle("服务器");
- this.setResizable(false);
- this.addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e) {
- if(out!=null){
- try {
- out.writeObject(Constant.CONNECT_QUIT);
- out.flush();
- }
- catch (IOException ex) {
- }
- }
- }
- });
- jScrollPane1.setBorder(BorderFactory.createEtchedBorder());
- jScrollPane1.setBounds(new Rectangle(2, 2, 405, 252));
- jTextField1.setBorder(titledBorder2);
- jTextField1.setText("");
- jTextField1.setBounds(new Rectangle(5, 270, 309, 25));
- jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
- public void keyPressed(KeyEvent e) {
- jTextField1_keyPressed(e);
- }
- });
- jButton1.setBounds(new Rectangle(323, 266, 77, 34));
- jButton1.setFont(new java.awt.Font("Dialog", 0, 15));
- jButton1.setForeground(Color.blue);
- jButton1.setText("发送");
- jButton1.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(ActionEvent e) {
- jButton1_actionPerformed(e);
- }
- });
- jEditorPane1.setText("");
- contentPane.add(jScrollPane1, null);
- contentPane.add(jButton1, null);
- contentPane.add(jTextField1, null);
- jScrollPane1.getViewport().add(jEditorPane1, null);
- }
- //Overridden so we can exit when window is closed
- protected void processWindowEvent(WindowEvent e) {
- super.processWindowEvent(e);
- if (e.getID() == WindowEvent.WINDOW_CLOSING) {
- System.exit(0);
- }
- }
- public void showMessage(String mess){
- jEditorPane1.setText(jEditorPane1.getText()+mess+"/n");
- jEditorPane1.setCaretPosition(jEditorPane1.getText().length());
- }
- public void setOut(ObjectOutputStream out){
- this.out=out;
- }
- void jButton1_actionPerformed(ActionEvent e) {
- try {
- out.writeObject("服务器>>" + jTextField1.getText());
- out.flush();
- jTextField1.setText("");
- }
- catch (IOException ex) {
- }
- }
- void jTextField1_keyPressed(KeyEvent e) {
- if(e.getKeyCode()==KeyEvent.VK_ENTER){
- try {
- out.writeObject("服务器>>" + jTextField1.getText());
- out.flush();
- jTextField1.setText("");
- }
- catch (IOException ex) {
- }
- }
- }
- }
- package myclass.cart;
- import javax.swing.UIManager;
- import java.awt.*;
- import java.net.Socket;
- import java.net.ServerSocket;
- import java.io.*;
- import javax.swing.JOptionPane;
- /**
- * <p>Title: </p>
- * <p>Description: </p>
- * <p>Copyright: Copyright (c) 2010</p>
- * <p>Company: </p>
- * @author not attributable
- * @version 1.0
- */
- public class Server {
- public ServerFrame frame;
- boolean packFrame = false;
- private ServerSocket serverSocket;
- private Socket socket;
- private ObjectOutputStream out;
- private ObjectInputStream in;
- //Construct the application
- public Server() {
- frame = new ServerFrame();
- //Validate frames that have preset sizes
- //Pack frames that have useful preferred size info, e.g. from their layout
- if (packFrame) {
- frame.pack();
- }
- else {
- frame.validate();
- }
- //Center the window
- Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
- Dimension frameSize = frame.getSize();
- if (frameSize.height > screenSize.height) {
- frameSize.height = screenSize.height;
- }
- if (frameSize.width > screenSize.width) {
- frameSize.width = screenSize.width;
- }
- frame.setLocation( (screenSize.width - frameSize.width) / 2,
- (screenSize.height - frameSize.height) / 2);
- frame.setVisible(true);
- //启动服务器
- startServer();
- }
- public void startServer() {
- try {
- frame.showMessage("服务器开始启动,等待连接...");
- serverSocket = new ServerSocket(Constant.LISTEN_PORT);
- socket = serverSocket.accept();
- //建立连接
- frame.showMessage("已与客户端" + socket.getInetAddress().getHostName() +
- "建立连接!");
- frame.showMessage("----------------------------------------------------");
- while (true) {
- //ServerThread st = new ServerThread(socket);
- //st.start();
- try {
- out = new ObjectOutputStream(socket.getOutputStream());
- frame.setOut(out);
- out.flush();
- //保持通信
- in=new ObjectInputStream(socket.getInputStream());
- String message="";
- while(true){
- try {
- message = (String) in.readObject();
- if(message.equals(Constant.CONNECT_QUIT)){
- frame.setOut(null);
- break;
- }
- // JOptionPane.showMessageDialog(null,"server");
- frame.showMessage(message);
- }
- catch (ClassNotFoundException ex1) {
- }
- catch (IOException ex1) {
- }
- }
- frame.showMessage("客户端"+socket.getInetAddress().getHostName()+"中断了连接!");
- in.close();
- out.close();
- socket.close();
- }
- catch (SecurityException ex) {
- }
- catch (IOException ex) {
- }
- }
- }
- catch (IOException ex) {
- }
- }
- //Main method
- public static void main(String[] args) {
- try {
- UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
- // UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- new Server();
- }
- //使用线程来处理有点问题
- /* class ServerThread extends Thread{
- Socket c;
- ServerThread(Socket t){
- c=t;
- }
- public void run(){
- try {
- ObjectOutputStream out = new ObjectOutputStream(c.getOutputStream());
- frame.setOut(out);
- out.flush();
- //保持通信
- ObjectInputStream in=new ObjectInputStream(c.getInputStream());
- String message="";
- while(true){
- try {
- message = (String) in.readObject();
- if(message.equals(Constant.CONNECT_QUIT)){
- frame.setOut(null);
- break;
- }
- frame.showMessage(message);
- }
- catch (ClassNotFoundException ex1) {
- }
- catch (IOException ex1) {
- }
- }
- frame.showMessage("客户端"+c.getInetAddress().getHostName()+"中断了连接!");
- in.close();
- out.close();
- socket.close();
- }
- catch (SecurityException ex) {
- }
- catch (IOException ex) {
- }
- }
- }
- */
- }
客户端代码:
- package myclass.cart;
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import javax.swing.border.*;
- import java.io.ObjectOutputStream;
- import java.io.*;
- /**
- * <p>Title: 客户端</p>
- * <p>Description:客户端程序 </p>
- * <p>Copyright: Copyright (c) 2010</p>
- * <p>Company: </p>
- * @author not attributable
- * @version 1.0
- */
- public class ClientFrame extends JFrame {
- JPanel contentPane;
- TitledBorder titledBorder1;
- TitledBorder titledBorder2;
- JScrollPane jScrollPane1 = new JScrollPane();
- JTextField jTextField1 = new JTextField();
- JButton jButton1 = new JButton();
- JEditorPane jEditorPane1 = new JEditorPane();
- TitledBorder titledBorder3;
- private ObjectOutputStream out;
- //Construct the frame
- public ClientFrame() {
- enableEvents(AWTEvent.WINDOW_EVENT_MASK);
- try {
- jbInit();
- }
- catch(Exception e) {
- e.printStackTrace();
- }
- }
- //Component initialization
- private void jbInit() throws Exception {
- contentPane = (JPanel) this.getContentPane();
- titledBorder1 = new TitledBorder("");
- titledBorder2 = new TitledBorder("");
- titledBorder3 = new TitledBorder("");
- contentPane.setLayout(null);
- this.setSize(new Dimension(410, 340));
- this.setTitle("客户端");
- this.setResizable(false);
- this.addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e) {
- if(out!=null){
- try {
- out.writeObject(Constant.CONNECT_QUIT);
- out.flush();
- }
- catch (IOException ex) {
- }
- }
- }
- });
- jScrollPane1.setBorder(BorderFactory.createEtchedBorder());
- jScrollPane1.setBounds(new Rectangle(2, 2, 405, 252));
- jTextField1.setBorder(titledBorder2);
- jTextField1.setText("");
- jTextField1.setBounds(new Rectangle(5, 270, 309, 25));
- jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
- public void keyPressed(KeyEvent e) {
- jTextField1_keyPressed(e);
- }
- });
- jButton1.setBounds(new Rectangle(323, 266, 77, 34));
- jButton1.setFont(new java.awt.Font("Dialog", 0, 15));
- jButton1.setForeground(Color.blue);
- jButton1.setText("发送");
- jButton1.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(ActionEvent e) {
- jButton1_actionPerformed(e);
- }
- });
- jEditorPane1.setText("");
- contentPane.add(jScrollPane1, null);
- contentPane.add(jButton1, null);
- contentPane.add(jTextField1, null);
- jScrollPane1.getViewport().add(jEditorPane1, null);
- }
- //Overridden so we can exit when window is closed
- protected void processWindowEvent(WindowEvent e) {
- super.processWindowEvent(e);
- if (e.getID() == WindowEvent.WINDOW_CLOSING) {
- System.exit(0);
- }
- }
- public void showMessage(String mess){
- jEditorPane1.setText(jEditorPane1.getText()+mess+"/n");
- jEditorPane1.setCaretPosition(jEditorPane1.getText().length());
- }
- public void setOut(ObjectOutputStream out){
- this.out=out;
- }
- void jButton1_actionPerformed(ActionEvent e) {
- try {
- out.writeObject("客户端>>" + jTextField1.getText());
- out.flush();
- jTextField1.setText("");
- }
- catch (IOException ex) {
- }
- }
- void jTextField1_keyPressed(KeyEvent e) {
- if(e.getKeyCode()==KeyEvent.VK_ENTER){
- try {
- out.writeObject("服务器>>" + jTextField1.getText());
- out.flush();
- jTextField1.setText("");
- }
- catch (IOException ex) {
- }
- }
- }
- }
- package myclass.cart;
- import javax.swing.UIManager;
- import java.awt.*;
- import java.io.ObjectOutputStream;
- import java.io.ObjectInputStream;
- import java.net.Socket;
- import java.io.*;
- import javax.swing.JOptionPane;
- /**
- * <p>Title: </p>
- * <p>Description: </p>
- * <p>Copyright: Copyright (c) 2010</p>
- * <p>Company: </p>
- * @author not attributable
- * @version 1.0
- */
- public class Cilent {
- boolean packFrame = false;
- private ClientFrame frame;
- private ObjectOutputStream out;
- private ObjectInputStream in;
- private Socket socket;
- //Construct the application
- public Cilent() {
- frame = new ClientFrame();
- //Validate frames that have preset sizes
- //Pack frames that have useful preferred size info, e.g. from their layout
- if (packFrame) {
- frame.pack();
- }
- else {
- frame.validate();
- }
- //Center the window
- Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
- Dimension frameSize = frame.getSize();
- if (frameSize.height > screenSize.height) {
- frameSize.height = screenSize.height;
- }
- if (frameSize.width > screenSize.width) {
- frameSize.width = screenSize.width;
- }
- frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
- frame.setVisible(true);
- startClient();
- }
- public void startClient(){
- try {
- frame.showMessage("连接中...");
- socket = new Socket(Constant.SERVER_HOST, Constant.LISTEN_PORT);
- //建立连接
- frame.showMessage("连接至" + socket.getInetAddress().getHostName());
- frame.showMessage("----------------------------------------------------");
- out = new ObjectOutputStream(socket.getOutputStream());
- frame.setOut(out);
- //保持通信
- in=new ObjectInputStream(socket.getInputStream());
- String message="";
- while(true){
- try {
- message = (String) in.readObject();
- if(message.equals(Constant.CONNECT_QUIT)){
- frame.setOut(null);
- break;
- }
- frame.showMessage(message);
- }
- catch (ClassNotFoundException ex1) {
- }
- catch (IOException ex1) {
- }
- }
- //结束连接
- frame.showMessage("服务器已经断开!");
- out.close();
- in.close();
- socket.close();
- }
- catch (IOException ex) {
- }
- }
- //Main method
- public static void main(String[] args) {
- try {
- UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
- }
- catch(Exception e) {
- e.printStackTrace();
- }
- new Cilent();
- }
- }
一些公用的参数代码:
- package myclass.cart;
- /**
- * <p>Title: 服务器与客户端使用常数</p>
- * <p>Description: </p>
- * <p>Copyright: Copyright (c) 2010</p>
- * <p>Company: </p>
- * @author not attributable
- * @version 1.0
- */
- public class Constant {
- public static final String SERVER_HOST = "127.0.0.1"; //服务器主机
- public static final int LISTEN_PORT = 8000; //监听端口号
- public static final String CONNECT_QUIT = "quit"; //退出连接标志
- }
- 一个简单的聊天程序
- 一个最简单的聊天程序2
- 一个简单的socket通信聊天程序
- 一个超级简单的tcp聊天程序
- 一个简单的命令行聊天程序
- 一个简单的局域网Socket聊天程序
- 一个最简单聊天程序
- 一个简单地聊天程序
- 简单的聊天程序
- 我写的一个简单的聊天程序(bcb)
- 一个简单的手机蓝牙聊天程序的源码
- 自己写的一个简单的蓝牙聊天程序
- 一个用Java写的简单的TCP聊天程序
- 记录自己做的一个简单的局域网聊天程序
- 一个用Java写的简单的TCP聊天程序
- 一个简单的基于udp协议的聊天程序
- C#多线程编程---一个简单的聊天程序(Server)
- C#多线程编程---一个简单的聊天程序(Client)
- 给我分啊~
- 9.14
- js与c#交互
- win7装oracle
- 抽象类和接口的异同
- 一个简单的聊天程序
- 只为兴趣
- 实训体念
- 刚开始Oracle的学习,感触不少
- 文件上传及最近问题
- I/O
- 和自己赛跑
- 关于C#开发
- 微软面试题