简易的聊天软件代码

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

GetMessage.java服务端:importjava.io.BufferedReader;importjava.io.FileReader;importjava.io.IOException;importjava.io.InputStreamReader;publicclassGetMessageextendsThread{privateinti;Stringv;JLabellabel=null;privateJTextAreatext;publicGetMessage(inti,JTextAreatext){this.i=i;this.text=text;}publicvoidrun(){try{ServerSocketso=newServerSocket(i);Sockets=so.accept();while(true){InputStreamReaderi=newInputStreamReader(s.getInputStream());BufferedReaderb=newBufferedReader(i);v=b.readLine();text.append(对方说+v+\n);}}catch(IOExceptione){//label.setText(对方已经下线);text.append();}}}SendMessage.java客户端:importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStreamReader;importjava.io.PrintStream;importjava.net.Socket;publicclassSendMessageextendsThread{privateStringip;privateinti;Sockets=null;JLabellabel=null;JTextFieldtext;JTextAreatext1;publicSendMessage(Stringip,inti,JTextAreatext1){//TODOAuto-generatedconstructorstubthis.ip=ip;this.i=i;this.text1=text1;}publicvoidrun(){while(true){try{s=newSocket(ip,i);text1.setText(连接成功+\n);break;}catch(Exceptione){try{Thread.sleep(1000);}catch(InterruptedExceptione1){System.out.println(出错了。。。。);}}}}publicvoidsend(Stringmessage){try{PrintStreamp=newPrintStream(s.getOutputStream());p.println(message);}catch(Exceptione1){System.out.println(异常+e1.getMessage());}}}Test.java简单的界面和测试类importjava.awt.*;importjava.awt.event.*;importjava.io.ByteArrayInputStream;importjava.io.InputStream;importjava.util.*;importJTextAreatext1;JTextAreatext2;JButtonbutton1,button2,button3;SendMessaget2;GetMessaget1;JLabellable1,lable2;JTextFieldtext;WindowTextArea(){this.s=s;lable1=newJLabel(对方ip);text=newJTextField(20);text1=newJTextArea(6,18);text2=newJTextArea(6,18);text2.setEditable(false);button1=newJButton(发送);button2=newJButton(关闭);button3=newJButton(确定ip);setBounds(100,100,450,300);setVisible(true);Containercon=getContentPane();con.setLayout(newFlowLayout());con.add(lable1);con.add(text);con.add(button3);con.add(newJScrollPane(text1));con.add(newJScrollPane(text2));con.add(button1);con.add(button2);button1.addActionListener(this);button2.addActionListener(this);button3.addActionListener(this);con.validate();setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}publicvoidrunthread(Stringip){t1=newGetMessage(4066,text2);t1.start();t2=newSendMessage(ip,4066,text2);t2.start();}publicvoidactionPerformed(ActionEvente){//TODOAuto-generatedmethodstubif(e.getSource()==button2){System.exit(0);}text2.append(text1.getText()+\n);t2.send(text1.getText());text1.setText();}if(e.getSource()==button3){s=text.getText();runthread(s);/***@paramargs*/publicstaticvoidmain(String[]args){newWindowTextArea();}}newWindowTextArea();}importjava.io.DataInputStream;importjava.io.DataOutputStream;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;publicclassTest{/***@paramargs*@throwsException*/publicstaticvoidmain(String[]args)throwsException{FileOutputStreamfout=newFileOutputStream(e:/heihei.RMVB);DataOutputStreamdout=newDataOutputStream(fout);longstart=System.currentTimeMillis();//读取原文件的输入流FileInputStreamf=newFileInputStream(d:/heihei.RMVB);DataInputStreamd=newDataInputStream(f);//-创建一个byte类型数组,定义数据包的大小为2048(2kb)byteb[]=newbyte[2048];i=d.read(b);}f.close();d.close();fout.close();dout.close();longend=System.currentTimeMillis();System.out.println(用时:+(end-start)+毫秒);}}

1 / 13
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功