版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Java 多人聊天源码1. ChatApplet.java 类import java.awt.*;import java.awt.event.*;import java.applet.*;import javax.swing.*;import com.borland.jbcl.layout.*;import javax.swing.border.*;import java.io.*;import .*;public class ChatApplet extends JApplet boolean isStandalone = false;BorderLayout borderLayout1 =
2、 new BorderLayout(); Border border1;JPanel jPanel2 = new JPanel();Border border2;BorderLayout borderLayout2 = new BorderLayout();JPanel jPanel1 = new JPanel();JButton buttonSend = new JButton();BorderLayout borderLayout3 = new BorderLayout(); JPanel jPanel3 = new JPanel();Border border3;BorderLayout
3、 borderLayout4 = new BorderLayout();JTextField textTalk = new JTextField();JPanel jPanel4 = new JPanel();Border border4;BorderLayout borderLayout5 = new BorderLayout();JScrollPane jScrollPane1 = new JScrollPane();JTextArea textMessages = new JTextArea();PrintWriter out = null;/*Get a parameter value
4、*/public String getParameter(String key, String def) return isStandalone ? System.getProperty(key, def) : (getParameter(key) != null ? getParameter(key) : def);/*Construct the applet*/public ChatApplet() /*Initialize the applet*/public void init() try jbInit();Client client = new Client(this);if (cl
5、ient.isConnected()out = client.getOutputStream();elseappendMessage(大大的错误! );catch(Exception e) e.printStackTrace();/*Component initialization*/private void jbInit() throws Exception border1 = BorderFactory.createEmptyBorder(9,9,9,9); border2 = BorderFactory.createEmptyBorder(9,9,9,9); border3 = Bord
6、erFactory.createEmptyBorder(2,0,2,5); border4 = BorderFactory.createEmptyBorder(0,0,5,0); this.setSize(new Dimension(400,300); this.getContentPane().setLayout(borderLayout1); jPanel2.setBorder(border2); jPanel2.setLayout(borderLayout2); buttonSend.setFocusPainted(false); buttonSend.setText( 发 送 ); b
7、uttonSend.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(ActionEvent e) buttonSend_actionPerformed(e););jPanel1.setLayout(borderLayout3); jPanel3.setBorder(border3);jPanel3.setLayout(borderLayout4); jPanel4.setBorder(border4);jPanel4.setLayout(borderLayout5); textT
8、alk.addKeyListener(new java.awt.event.KeyAdapter() public void keyPressed(KeyEvent e) textTalk_keyPressed(e););this.getContentPane().add(jPanel2, BorderLayout.CENTER); jPanel2.add(jPanel1, BorderLayout.SOUTH); jPanel1.add(buttonSend, BorderLayout.EAST); jPanel1.add(jPanel3, BorderLayout.CENTER); jPa
9、nel3.add(textTalk, BorderLayout.CENTER); jPanel2.add(jPanel4, BorderLayout.CENTER); jPanel4.add(jScrollPane1, BorderLayout.CENTER); jScrollPane1.getViewport().add(textMessages, null);/*Get Applet information*/ public String getAppletInfo() return Applet Information;/*Get parameter info*/public Strin
10、g getParameterInfo() return null;/*Main method*/public static void main(String args) ChatApplet applet = new ChatApplet(); applet.isStandalone = true; JFrame frame = new JFrame(); /EXIT_ON_CLOSE = 3 frame.setDefaultCloseOperation(3); frame.setTitle(Applet Frame); frame.getContentPane().add(applet, B
11、orderLayout.CENTER); applet.init();applet.start();frame.setSize(400,320);Dimension d = Toolkit.getDefaultToolkit().getScreenSize();frame.setLocation(d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);frame.setVisible(true);/static initializer for setting look & feelstatic
12、 try UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName();/UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClass Name(); catch(Exception e) public void appendMessage(String message) textMessages.setText(message + textMessages.getText();void buttonSend_actionPerformed(Acti
13、onEvent e) String msg;msg = textTalk.getText().trim();if (msg.equals() | msg = null) return;out.println(textTalk.getText();textTalk.setText();void textTalk_keyPressed(KeyEvent e) if (e.getKeyChar() != n) return; out.println(textTalk.getText(); textTalk.setText();2. ChatServlet.javaimport javax.servl
14、et.*;import javax.servlet.http.*;import java.io.*;import java.util.*;public class ChatServlet extends HttpServlet private static final String CONTENT_TYPE = text/html;/*Initialize global variables*/public void init(ServletConfig config) throws ServletException super.init(config);try new Server();cat
15、ch (IOException ex) System.err.println(IO错误: );ex.printStackTrace(System.err);destroy();/*Process the HTTP Get request*/ public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException response.setContentType(CONTENT_TYPE); PrintWriter out = response.g
16、etWriter(); out.println(); out.println(ChatServlet); out.println();out.println(The servlet has received a GET. This is the reply.);out.println();/*Clean up resources*/public void destroy() 3. Client.javaimport java.io.*;import .*;public class Client Socket socket = null;private String host;private b
17、oolean connected = false;public boolean isConnected() return connected; public Client(ChatApplet applet) try host = applet.getDocumentBase().getHost();/host = 46;socket = new Socket(host, Server.port);connected = true;new ClientRecieveThread(socket, applet).start(); catch (Exception ex) a
18、pplet.appendMessage(ex.getMessage(); ex.printStackTrace(System.err);public PrintWriter getOutputStream() throws IOException return new PrintWriter(socket.getOutputStream(), true);4. ClientRecieveThread.javaimport java.io.*;import .*;public class ClientRecieveThread extends Thread private ChatApplet
19、applet = null;private Socket socket = null;public ClientRecieveThread(Socket socket, ChatApplet applet) this.socket = socket; this.applet = applet;public void run() BufferedReader in = null;String inputLine;try in = new BufferedReader( new InputStreamReader( socket.getInputStream();while (inputLine
20、= in.readLine() != null) /if (inputLine.equalsIgnoreCase(quit) break; applet.appendMessage(inputLine + n);catch (Exception ex) ex.printStackTrace(System.err);finally Close();void Close() try socket.close();catch (Exception ex) ex.printStackTrace(System.err);5. Protocol.java public class Protocol pri
21、vate String userid;public Protocol(String userid) this.userid = userid;public String processInput(String input) return (userid + : + input);6. Server.javaimport java.io.*;import .*;import java.util.Vector;public class Server private ServerSocket serverSocket = null;public static int port = 4444;priv
22、ate boolean listening = true;Vector clientSockets = new Vector(10);public Server() throws IOException try serverSocket = new ServerSocket(port);catch (Exception ex) + port);System.err.println( 不能监听端口: ex.printStackTrace(System.err); System.exit(-1);System.out.println( 成功监听端口: + port);while (listenin
23、g) addClient(serverSocket.accept();serverSocket.close();来到人的离人public void addClient(Socket socket) throws IOException new ServerThread(socket, this).start(); clientSockets.add(socket);send(欢迎 + socket.getInetAddress().getHostName() + 这里! );System.out.println( 聊天室共有 + clientSockets.size() + );public
24、void removeClient(Socket socket) throws IOException send(欢送 + socket.getInetAddress().getHostName() + 去);clientSockets.remove(socket);System.out.println( 聊天室共有 + clientSockets.size() + );public void send(String msg) throws IOException Socket socket = null;for (int I = 0; I clientSockets.size(); I+)
25、socket = (Socket)(clientSockets.get(I);PrintWriterout = newPrintWriter(socket.getOutputStream(),true);out.println(msg);public static void main(String args) throws IOException new Server(); 7. ServerThread.javapublic class ServerThread extends Thread private Server server = null; private Socket socket = null; private Protocol j
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年施工项目部《春节节后复工复产》工作专项方案 (3份)
- 小学数学四年级上册《你知道吗-加法交换律和交换律》知识要点
- 小学四年级数学上册全册错题集练习试题第三单元 混合运算
- 小学数学二年级加减法练习题
- 扬州会议高考语文阅读理解
- 高考语文试题分类汇编语句衔接
- 人力资源管理在酒店行业的应用
- 金融投资行业顾问心得分享
- 在变化中寻找机遇的方法计划
- 班主任工作培训总结加强教学管理及学科指导
- 中国铝业股份有限公司河南分公司巩义市山川铝土矿矿山地质环境保护与土地复垦方案
- 工商企业管理毕业论文范文六篇
- 二十五项反措检查表优质资料
- 保密办主任工作总结保密办主任工作总结八篇
- 新生儿沐浴及抚触护理
- 机械原理课程设计-压床机构的设计
- 教学案例 英语教学案例 市赛一等奖
- 四川省2023职教高考英语试题
- JJG 913-2015浮标式氧气吸入器
- GB/T 12190-2006电磁屏蔽室屏蔽效能的测量方法
- 2020年贵州专升本高等数学真题及答案
评论
0/150
提交评论