java简单的qq聊天源码

上传人:壹****1 文档编号:512561135 上传时间:2022-09-16 格式:DOC 页数:16 大小:76.50KB
返回 下载 相关 举报
java简单的qq聊天源码_第1页
第1页 / 共16页
java简单的qq聊天源码_第2页
第2页 / 共16页
java简单的qq聊天源码_第3页
第3页 / 共16页
java简单的qq聊天源码_第4页
第4页 / 共16页
java简单的qq聊天源码_第5页
第5页 / 共16页
点击查看更多>>
资源描述

《java简单的qq聊天源码》由会员分享,可在线阅读,更多相关《java简单的qq聊天源码(16页珍藏版)》请在金锄头文库上搜索。

1、Threadtext)GetMessage.javs服务端:importjava.io.BufferedReader;importjava.io.FileReader;importjava.io.IOException;importjava.io.InputStreamReader;.ServerSocket;.Socket;importjavax.swing.JLabel;importjavax.swing.JTextArea;public class GetMessage extendsprivateint i ;Stringv;JLabellabel =null ;privateJTex

2、tAreatextpublic GetMessage( int i,JTextArea this . i =i;this . text =text;public void run()try ServerSocket so =newServerSocket( i );Socket s = so.accept();while ( true )InputStreamReader i =newInputStreamReader(s.getInputStream();BufferedReader b =newBufferedReader(i);v= b.readLine();text .append(

3、对方说 + v +n ); catch (IOException e) /label.setText( 对方已经下线 );text .append( 对方下线了。 );Sen dMessage.java客户端:import import import import import importjava.io.BufferedReader;java.io.IOException;java.io.InputStreamReader;java.io.PrintStream;.Socket;.UnknownHostException;import import importjavax.swing.JLa

4、bel; javax.swing.JTextArea; javax.swing.JTextField;publicclass SendMessage extends Threadprivate String ip ;private int i ;Socket s = null ;JLabel label =null ;JTextField text ;JTextArea text1 ;public SendMessage(String ip,inti,JTextArea text1) / TODOAuto-generated constructor stubthis . ip =ip; thi

5、s . i =i;this . text1 =text1;publicvoid run()while( true )trys = new Socket( ip , i ); text1 .setText( 连接成功 +n);break ; catch (Exception e) try Thread. sleep (1000);e1)。 catch (InterruptedExceptionSystem. out .println( 出错了);public void send(String message)try PrintStream p =newPrintStream(s .getOutp

6、utStream();p.println(message); 异常 catch (Exception e1) System. out .println( +e1.getMessage();Test.java 简单的界面和测试类import java.awt.*; import java.awt.event.*;import java.io.ByteArrayInputStream;importjava.i o.ln putStream;import java.util.*;importjavax.swi ng.*;import javax.sw in g.eve nt.*;JFrameclas

7、s Win dowTextAreaexte ndsimpleme ntsActio nListe nerStrings;JTextAreatext1 ;JTextAreatext2JButtonbutt on1,button2Sen dMessaget2 ;GetMessaget1 ;JLabellable1 , lable2 ;JTextFieldtext7butt on3Win dowTextArea() this . s=s;Iable1 =new JLabel(对方 ip); text =new JTextField(20);textl =new JTextArea(6,18);tex

8、t2 =new JTextArea(6,18);text2 .setEditable(false );butt on1 =new JButt on(butt on2 =new JButt on(butt on3 =new JButt on(发送);关闭”);确定ip);setBou nds(100,100,450,300);setVisible(true );Container con=getC onten tPa ne();con. setLayout(new FlowLayout();con. add(lable1);con. add(text );con. add(butt on3);c

9、on. add(new JScrollPa ne(text1 );con. add(new JScrollPa ne(text2 );con. add(butto n1);con. add(butto n2);this );this );this );EXIT_Otext2 );butt on1.addActi on Liste ner(butt on2 .addActi on Liste ner(butt on3 .addActi on Liste ner(con. validate。; setDefaultCloseOperati on( JFrame.N_CLOSE);public vo

10、id run thread(Stri ng ip) t1 = new GetMessage(4066,t1 .start();t2 =newSen dMessage (i p,4066,text2 );t2 .start();public void actio nPeformed(Actio nEve nt e) / TODOAuto-ge nerated method stub if (e.getSource()=button2 )System. exit (0);if (e.getSource()= button1 )n);ext2 .appe nd( text1 .getText()+t

11、2 .send( text1 .getText();text1 .setText( );if (e.getSource()= button3 )s =text .getText();runthread( s);public class Test /* param args*/args)public static void main(Stringnew WindowTextArea();new WindowTextArea();ip 和点击确定 ip向对方发送信息之前必须输入对方 按钮,文本区显示“连接成功” 然后可以发送信息作者:曾建路package com.neusoft.test对文件的内

12、容操作.字节流.每次读取固定大 小的文件内容 ;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;public class Test /* param args* throws Exception*/public static void main(String args) throws Exception FileOutp

13、utStreamfout=newFileOutputStream(e:/heihei.RMVB);DataOutputStream dout = new DataOutputStream(fout);long start = System.currentTimeMillis();/读取原文件的输入流FileInputStream f = new FileInputStream(d:/heihei.RMVB); DataInputStream d = new DataInputStream(f);-创建一个byte类型数组,定义数据包的大小为2048 (2kb)byte b = new byte2048;int i = d.read(b);/读取文件的内容返回值是本次读取到的字节的长度while(i != -1)dout.write(b,0,i);dout.flush();i = d.read(b);f.close();d.close();fout.close();dout.close();long end = System.currentTimeMil

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 资格认证/考试 > 自考

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号