为了正常的体验网站,请在浏览器设置里面开启Javascript功能!

JAVA聊天程序源代码

2011-06-01 14页 doc 67KB 170阅读

用户头像

is_230842

暂无简介

举报
JAVA聊天程序源代码.java文件以下是本文件的内容,总共有MyWindow.java PPfriend.java QQDialog.java receiveSMG.java Recevie.java RecevieToOne.java SendFile.java SendToOne.java MyWindow.java package hf; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.WindowAdapter; import java...
JAVA聊天程序源代码
.java文件以下是本文件的,总共有MyWindow.java PPfriend.java QQDialog.java receiveSMG.java Recevie.java RecevieToOne.java SendFile.java SendToOne.java MyWindow.java package hf; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; class APanel extends Panel { TextArea chat_txt; APanel2 apanel2; public APanel() { chat_txt = new TextArea(40,40); chat_txt.setEditable(false); apanel2 = new APanel2(); setLayout(new BorderLayout()); add("West",chat_txt); add("East",apanel2); } } class APanel2 extends Panel { java.awt.List list; APanel2(){ try{ list = new java.awt.List(25,false); } catch(NullPointerException e){} setLayout(new BorderLayout()); add("Center",list); add("North", new Label("当前聊天者列:")); add("East",new Label()); add("South",new Label("双击一个人的昵称可以进行交谈")); } } class BPanel extends Panel { TextField msg_txt; Button button1,button2,button3,button4,button5; //button1 button2用做 public BPanel() { msg_txt = new TextField(30); button1 = new Button("发送"); button2 = new Button("刷新谈话区"); button3 = new Button("刷新在线用户"); setLayout(new FlowLayout(FlowLayout.LEFT)); add(new Label("发送的话")); add(msg_txt); add(button1); add(button2); add(button3); } } class MyWindow extends Frame { Label person; APanel pa; BPanel pb; public MyWindow() { super(".."); person = new Label("聊天:"); pa = new APanel(); pb = new BPanel(); setLayout(new BorderLayout()); setBackground(new Color(206,113,150)); setSize(550,450); setLocation(450,100); setResizable(false); add("North",person); add("Center",pa); add("South",pb); addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(1); } }); setVisible(true); } } PPfriend.java package hf; import java.applet.Applet; import java.applet.AudioClip; import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; import java.util.*; import javax.swing.*; public class PPfriend extends JFrame implements ActionListener{ public String myAddress; public String myIP; Button button1,button2,button3; Vector clients = new Vector(); public MyWindow myw ; QQDialog qq ; String sendIp; BroadcastThread broadcast; receiveSMG rm; String filepath=null; String savepath=null; RecevieToOne recevietoone; public static void main(String []str){ new PPfriend(); } public PPfriend(){ try{ ppInit(); }catch(Exception e){ e.printStackTrace(); } serverListen(); } void ppInit(){ myw = new MyWindow(); qq = new QQDialog("QQ"); qq.setVisible(false); qq.refile.setEnabled(false); recevietoone = new RecevieToOne(this); recevietoone.start(); myw.pa.apanel2.list.addActionListener(this); myw.pb.button1.addActionListener(this); myw.pb.button2.addActionListener(this); myw.pb.button3.addActionListener(this); qq.fileBut.addActionListener(this); qq.sendBut.addActionListener(this); qq.refile.addActionListener(this); rm = new receiveSMG(this); rm.start(); } public void serverListen(){ broadcast = new BroadcastThread(); broadcast.start(); } public void actionPerformed(ActionEvent e){ if(e.getSource() == myw.pb.button1 ){ myw.pa.chat_txt.append(myIP+": "+myw.pb.msg_txt.getText()+"\n"); for(int i=0;i < clients.size();i++){ if(!clients.elementAt(i).toString().equals(myIP)) { try{ String message = myIP+": "+myw.pb.msg_txt.getText().toString(); InetAddress ia = InetAddress.getByName(clients.elementAt(i).toString()); byte[] bs = message.getBytes(); DatagramPacket sendmessage = new DatagramPacket(bs, bs.length,ia, 8005); DatagramSocket socket = new DatagramSocket(); socket.send(sendmessage); socket.close(); qq.chat_txt.setText(null); qq.chat_txt.requestFocus(); }catch(Exception ee){ ee.printStackTrace(); } } } myw.pb.msg_txt.setText(null); myw.pb.msg_txt.requestFocus(); } else if(e.getSource() == myw.pb.button2){ myw.pa.chat_txt.setText(""); } else if(e.getSource() == myw.pb.button3){ broadcast.yield(); broadcast=null; clients.clear(); myw.pa.apanel2.list.removeAll(); myw.pa.apanel2.list.repaint(); serverListen(); } else if(e.getSource()==myw.pa.apanel2.list){ qq.setVisible(true); sendIp = myw.pa.apanel2.list.getSelectedItem().toString(); } else if(e.getSource() == qq.fileBut){ //发送文件 JFileChooser chooser = new JFileChooser(); int returnVal=chooser.showDialog(null, "send"); if(returnVal==chooser.APPROVE_OPTION) { filepath=chooser.getSelectedFile().getAbsolutePath(); SendFile mysf=new SendFile(this,filepath); mysf.start(); } } else if(e.getSource()==qq.refile){ { JFileChooser mychooser = new JFileChooser(); int myreturnVal=mychooser.showDialog(null, "save"); if(myreturnVal==mychooser.APPROVE_OPTION){ savepath=mychooser.getSelectedFile().getAbsolutePath(); Recevie myrecevie=new Recevie(this,savepath); myrecevie.start(); } } } else if(e.getSource() == qq.sendBut){ try{ String message = myIP+": "+qq.chat_txt.getText().toString(); myw.pa.chat_txt.append(message+"\n"); InetAddress ia = InetAddress.getByName(sendIp.toString()); byte[] bs = message.getBytes(); DatagramPacket sendmessage = new DatagramPacket(bs, bs.length,ia, 8005); DatagramSocket socket1 = new DatagramSocket(); socket1.send(sendmessage); qq.chat_txt.setText(null); qq.chat_txt.requestFocus(); socket1.close(); }catch(Exception ee){ ee.printStackTrace(); } } } class BroadcastThread extends Thread{ public void getmyAddress(){ try{ InetAddress addr = InetAddress.getLocalHost(); myAddress= addr.getAddress().toString();//获得本机名称 myIP=addr.getHostAddress();//获得本机IP }catch(Exception e1){ e1.printStackTrace(); } } public void run(){ try{ MulticastSocket multicastSocket = new MulticastSocket(7777); InetAddress inetAddress = InetAddress.getByName("230.0.0.1"); multicastSocket.joinGroup(inetAddress); byte [] receiveIp = new byte [12]; getmyAddress(); while (true){ DatagramPacket sendIp = new DatagramPacket(myAddress.getBytes(),myAddress.length(),inetAddress,7777); multicastSocket.send(sendIp); DatagramPacket datagramPacket = new DatagramPacket(receiveIp, receiveIp.length,inetAddress,7777); multicastSocket.receive(datagramPacket); String ip = datagramPacket.getAddress().getHostAddress().toString(); if((!clients.contains(ip))) clients.add(ip); myrepaint(); sleep(1000); } } catch (Exception exception) { exception.printStackTrace(); } } void myrepaint(){ myw.pa.apanel2.list.removeAll(); for(int i = 0;i < clients.size();i++) { myw.pa.apanel2.list.add(clients.elementAt(i).toString()); } } } } QQDialog.java package hf; import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.*; public class QQDialog extends JFrame{ TextArea chat_txt; Button sendBut,fileBut,refile; QQDialog(String name){ super(name); Label title = new Label("密函:"); chat_txt = new TextArea(20,20); chat_txt.setEditable(true); sendBut = new Button("发送"); fileBut = new Button("发送文件"); refile = new Button("接受文件"); setLayout(new BorderLayout()); Panel p = new Panel(new FlowLayout(FlowLayout.LEFT)); p.add(sendBut); p.add(fileBut); p.add(refile); add("North",title); add("Center",chat_txt); add("South",p); setSize(400,200); setLocation(400,400); addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ e.getWindow().dispose(); } }); setVisible(true); } } receiveSMG.java package hf; import java.applet.Applet; import java.applet.AudioClip; import java.io.BufferedReader; import java.io.File; import java.io.InputStreamReader; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.ServerSocket; import java.net.Socket; public class receiveSMG extends Thread{ public String msg; PPfriend friend; receiveSMG(PPfriend friend){ this.friend =friend; } public void run(){ try{ File music = new File("remsg.wav"); AudioClip ac = Applet.newAudioClip(music.toURL()); while(true){ byte[] inbuf = new byte[2048]; // 默认的数据缓冲大小 DatagramSocket socket = new DatagramSocket(8005); DatagramPacket packet = new DatagramPacket(inbuf, inbuf.length); socket.receive(packet);// 接受数据 msg = new String(packet.getData()); if(msg!=null) { friend.myw.pa.chat_txt.append(msg); friend.myw.pa.chat_txt.append("\n"); ac.play(); } socket.close(); sleep(1000); } }catch(Exception eee){ eee.printStackTrace(); } } } Recevie.java package hf; import java.net.*; import java.io.*; import javax.swing.JFileChooser; import javax.swing.JOptionPane; public class Recevie extends Thread{ private String filepath=null; PPfriend friend=null; InetAddress addr; public Recevie(PPfriend friend,String savepath) { this.friend = friend; filepath=savepath; } public void doclient() throws Exception{ int length; File file=new File(filepath); FileOutputStream outputb=new FileOutputStream(file); if(friend.recevietoone.getip!=null) { addr=InetAddress.getByName(friend.recevietoone.getip); } Socket socket=new Socket(addr,8888); try{ //System.out.println("socket= "+socket); PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true); InputStream netIn=socket.getInputStream(); InputStream in=new DataInputStream(new BufferedInputStream(netIn)); OutputStream netOut=socket.getOutputStream(); OutputStream doc=new DataOutputStream(new BufferedOutputStream(netOut)); byte []buf=new byte[2048]; length=in.read(buf); while(length!=(-1)){ //是否读完所有数据 outputb.write(buf,0,length);//将数据写往文件 length=in.read(buf);//继续从网络中读取文件 } in.close(); outputb.close(); }finally{ JOptionPane.showMessageDialog(null, "文件传输成功!", null,JOptionPane.OK_OPTION); socket.close(); } } public void run(){ try { this.doclient(); } catch (Exception e) { e.printStackTrace(); } } } RecevieToOne.java package hf; import java.applet.Applet; import java.applet.AudioClip; import java.io.File; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; public class RecevieToOne extends Thread{ DatagramSocket datasocket; public String getip =null; PPfriend friend; public RecevieToOne(PPfriend friend) { super("kk"); this.friend = friend; } public void run() { try { byte[] inbuf = new byte[256]; // 默认的数据缓冲大小 DatagramSocket socket = new DatagramSocket(8889); // 等待数据 DatagramPacket packet = new DatagramPacket(inbuf, inbuf.length); socket.receive(packet); getip = packet.getAddress().getHostAddress().toString(); if(getip !=null){ friend.qq.refile.setEnabled(true); File music = new File("refile.wav"); AudioClip ac = Applet.newAudioClip(music.toURL()); ac.play(); friend.qq.setVisible(true); return; } } catch (Exception e) { e.printStackTrace(); } } } SendFile.java package hf; import java.io.*; import java.net.*; import javax.swing.JFileChooser; import javax.swing.JOptionPane; public class SendFile extends Thread { public static final int PORT=8888; PPfriend friend; private String filepath=null; SendToOne sendtoone; public SendFile(PPfriend friend,String filepath2) { this.friend = friend; filepath=filepath2; } public void doserver() throws IOException, URISyntaxException{ int length; sendtoone = new SendToOne(friend); File file=new File(filepath); FileInputStream fos=new FileInputStream(file); ServerSocket s=new ServerSocket(PORT); try{ Socket socket=s.accept(); try{ InputStream netIn=socket.getInputStream(); InputStream in=new DataInputStream(new BufferedInputStream(netIn)); OutputStream netOut=socket.getOutputStream(); OutputStream doc=new DataOutputStream(new BufferedOutputStream(netOut)); file.getAbsolutePath(); byte[] buf=new byte[2048]; length=fos.read(buf); while(length!=(-1)){ //是否读完文件 doc.write(buf,0,length);//把文件数据写出网络缓冲区 doc.flush();//刷新缓冲区把数据写往客户端 length=fos.read(buf);//继续从文件中读取数据 } fos.close(); doc.close(); }finally{ JOptionPane.showMessageDialog(null, "文件传输成功!", null,JOptionPane.OK_OPTION); socket.close(); } }finally{ s.close(); } } public void run() { try { this.doserver(); } catch (IOException e) { e.printStackTrace(); } catch (URISyntaxException e) { e.printStackTrace(); } } } SendToOne.java package hf; import java.net.*; public class SendToOne { DatagramSocket datasocket; InetAddress ia; PPfriend friend; public SendToOne(PPfriend friend){ this.friend= friend; try { Thread.sleep(1000); // 休眠1秒,等待接收服务启动 ia = InetAddress.getByName(friend.sendIp); byte[] bs = "net".getBytes(); DatagramPacket request = new DatagramPacket(bs, bs.length, ia, 8889); DatagramSocket socket = new DatagramSocket(); socket.send(request); } catch (Exception e) { e.printStackTrace(); } } public void run() { } }
/
本文档为【JAVA聊天程序源代码】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
热门搜索

历史搜索

    清空历史搜索