Java多人聊天源码_第1页
Java多人聊天源码_第2页
Java多人聊天源码_第3页
Java多人聊天源码_第4页
Java多人聊天源码_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论