java 简单的qq聊天源码.docx
《java 简单的qq聊天源码.docx》由会员分享,可在线阅读,更多相关《java 简单的qq聊天源码.docx(9页珍藏版)》请在冰豆网上搜索。
java简单的qq聊天源码
GetMessage.java服务端:
importjava.io.BufferedReader;
importjava.io.FileReader;
importjava.io.IOException;
importjava.io.InputStreamReader;
import.ServerSocket;
import.Socket;
importjavax.swing.JLabel;
importjavax.swing.JTextArea;
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;
import.Socket;
import.UnknownHostException;
importjavax.swing.JLabel;
importjavax.swing.JTextArea;
importjavax.swing.JTextField;
publicclassSendMessageextendsThread{
privateStringip;
privateinti;
Sockets=null;
JLabellabel=null;
JTextFieldtext;
JTextAreatext1;
publicSendMessage(Stringip,inti,JTextAreatext1){
//TODOAuto-generatedconstructorstub
this.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.*;importjavax.swing.*;
importjavax.swing.event.*;
classWindowTextAreaextendsJFrameimplementsActionListener
{
Strings;
JTextAreatext1;
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-generatedmethodstub
if(e.getSource()==button2)
{
System.exit(0);
}
if(e.getSource()==button1)
{
text2.append(text1.getText()+"\n");
t2.send(text1.getText());
text1.setText("");
}
if(e.getSource()==button3)
{
s=text.getText();
runthread(s);
}
}
}
publicclassTest{
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
newWindowTextArea();
}
}
newWindowTextArea();
}
}
向对方发送信息之前必须输入对方ip和点击确定ip按钮,文本区显示“连接成功”
然后可以发送信息
作者:
曾建路
packagecom.neusoft.test.对文件的内容操作.字节流.每次读取固定大小的文件内容;
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];
inti=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();
longend=System.currentTimeMillis();
System.out.println("用时:
"+(end-start)+"毫秒");
}
}