您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 广告经营 > java_模拟qq聊天的源代码
客户端:packagecom.insigma.commutil;importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStreamReader;importjava.io.PrintWriter;importjava.net.Socket;importjavax.swing.JTextArea;publicclassClienThreadextendsThread{privateSocketsocket=null;privatejavax.swing.JTextAreajTextArea1;publicClienThread(JTextAreajTextArea1){this.jTextArea1=jTextArea1;}publicvoidrun(){try{//创建连接socket=newSocket(,12344);Stringmessage=null;while(true){//接受服务端请求message=receive();//显示到消息接收框if(message!=null&&!message.equals()){if(this.jTextArea1.getText()==null||this.jTextArea1.getText().equals()){this.jTextArea1.setText(服务端:+message);}else{this.jTextArea1.setText(this.jTextArea1.getText()+\n服务端:+message);}}else{message=服务端以断开连接或未连接!;if(this.jTextArea1.getText()==null||this.jTextArea1.getText().equals()){this.jTextArea1.setText(服务端:+message);}else{this.jTextArea1.setText(this.jTextArea1.getText()+\n服务端:+message);}break;}}}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}/***向服务端发送消息*/publicvoidsend(Stringmessage){try{PrintWriterprintWriter=newPrintWriter(socket.getOutputStream());printWriter.write(message+\n);printWriter.flush();}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}/***接受服务端消息*/publicStringreceive(){Stringmessage=;try{BufferedReaderbufferedReader=newBufferedReader(newInputStreamReader(socket.getInputStream()));message=bufferedReader.readLine();}catch(IOExceptione){System.out.println(服务端已断开或未连接!);}returnmessage;}}服务端:packagecom.insigma.commutil;importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStreamReader;importjava.io.PrintWriter;importjava.net.ServerSocket;importjava.net.Socket;importjavax.swing.JTextArea;publicclassServerThreadextendsThread{privateSocketsocket=null;privateServerSocketserverSocket=null;privatejavax.swing.JTextAreajTextArea1;publicServerThread(JTextAreajTextArea1){this.jTextArea1=jTextArea1;}publicvoidrun(){try{//创建连接serverSocket=newServerSocket(12344);//监听客户连接socket=serverSocket.accept();Stringmessage=null;while(true){//接受客户请求message=receive(socket);if(message!=null&&!message.equals()){//显示到消息接收框if(this.jTextArea1.getText()==null||this.jTextArea1.getText().equals()){this.jTextArea1.setText(客户端:+message);}else{this.jTextArea1.setText(this.jTextArea1.getText()+\n客户端:+message);}}else{message=客户端以断开连接或未连接!;if(this.jTextArea1.getText()==null||this.jTextArea1.getText().equals()){this.jTextArea1.setText(客户端:+message);}else{this.jTextArea1.setText(this.jTextArea1.getText()+\n客户端:+message);}break;}}}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}/***向客户端发送消息*/publicvoidsend(Stringmessage){try{PrintWriterprintWriter=newPrintWriter(socket.getOutputStream());printWriter.write(message+\n);printWriter.flush();}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}/***接受客户端消息*/publicStringreceive(Socketsocket){Stringmessage=;try{BufferedReaderbufferedReader=newBufferedReader(newInputStreamReader(socket.getInputStream()));message=bufferedReader.readLine();}catch(IOExceptione){System.out.println(服务端已断开或未连接!);}returnmessage;}}服务端界面:/**SereveFrame.java**Createdon__DATE__,__TIME__*/packagecom.insigma.frame;importcom.insigma.commutil.ServerThread;/****@author__USER__*/publicclassServerFrameextendsjavax.swing.JFrame{privateServerThreadserverThread;/**CreatesnewformSereveFrame*/publicServerFrame(){initComponents();serverThread=newServerThread(this.jTextArea1);serverThread.start();}/***Thismethodiscalledfromwithintheconstructortoinitializetheform.*WARNING:DoNOTmodifythiscode.Thecontentofthismethodisalways*regeneratedbytheFormEditor.*///GEN-BEGIN:initComponents//editor-folddefaultstate=collapseddesc=GeneratedCodeprivatevoidinitComponents(){jScrollPane1=newjavax.swing.JScrollPane();jTextArea1=newjavax.swing.JTextArea();jScrollPane2=newjavax.swing.JScrollPane();jTextArea2=newjavax.swing.JTextArea();jButton1=newjavax.swing.JButton();jButton2=newjavax.swing.JButton();setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);setTitle(\u670d\u52a1\u7aef);setBackground(newjava.awt.Color(153,153,255));jTextArea1.setColumns(20);jTextArea1.setRows(5);jScrollPane1.setViewportView(jTextArea1);jTextArea2.setColumns(20);jTextArea2.setRows(5);jScrollPane2.setViewportView(jTextArea2);jButton1.setText(\u53d6\u6d88);jButton1.addActionListener(newjava.awt.event.ActionListener(){publicvoidactionPerformed(java.awt.event.ActionEventevt){jButton1ActionPerformed(evt);}});jButton2.setText(\u53d1\u9001);jButton2.addActionListener(newjava.awt.event.ActionListener(){publicvoidactionPerformed(java.awt.event.ActionEventevt){jButton2ActionPerformed(evt);}});javax.swing.GroupLayoutlayout=newjavax.swing.GroupLayout(getContentPane());getContentPane().setLayout(layout);layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(javax.swing.GroupLay
本文标题:java_模拟qq聊天的源代码
链接地址:https://www.777doc.com/doc-4249169 .html