java模拟一个简单的QQ

v 项目源码

  https://github.com/hjzgg/java_QQ

v 标题效果

 

 

      

package testFour;import java.awt.Color;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.BindException;
import java.net.ServerSocket;
import java.util.Iterator;import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;public class QQ {public static final int PICUTER = 1; public static final int FILE = 2; public static final int PARAGRAPH = 3; public static final int SEND = 1; public static final int RECEIVE = 2; public static final int FILEX = 3;public static int getUnusedPort() throws BindException{int port;for(port = 10000 ; port <= 65535; ++port){ServerSocket ss = null; try {ss = new ServerSocket(port);} catch (IOException e) {e.printStackTrace();}if(ss != null) break;}if(port > 65535)throw new BindException("没有可用的端口!");return port;}public synchronized static void setTextPane(JTextPane tp, byte[] bt, int len, int flag, int tag){ImageIcon myIcon = null;if(tag == SEND)myIcon = new ImageIcon("ff.jpg");else if(tag == RECEIVE)myIcon = new ImageIcon("gg.jpg");else if(tag == FILEX)myIcon = new ImageIcon("kk.jpg");SimpleAttributeSet set = new SimpleAttributeSet();Document doc = tp.getStyledDocument();if(tag == SEND || tag == RECEIVE){tp.setCaretPosition(doc.getLength());JLabel fileLabel = new JLabel("", myIcon, JLabel.CENTER);tp.insertComponent(fileLabel);}if(flag == PARAGRAPH){FontMetrics fm = tp.getFontMetrics(tp.getFont());int paneWidth = tp.getWidth();String text = new String(bt, 0, len);if( tag/3 == SEND ){StyleConstants.setForeground(set, new Color(0,255,0));//设置文字颜色StyleConstants.setFontSize(set, 15);//设置字体大小
             }else if( tag/3 == RECEIVE){StyleConstants.setForeground(set, new Color(0,0,0));//设置文字颜色StyleConstants.setFontSize(set, 15);//设置字体大小
             }try{for(int i = 0, cnt = 0; i < text.length(); ++i){if((cnt += fm.charWidth(text.charAt(i))) >= paneWidth){cnt = 0;doc.insertString(doc.getLength(), "\n", set);continue;}doc.insertString(doc.getLength(), String.valueOf(text.charAt(i)), set);}doc.insertString(doc.getLength(), "\n", set);tp.setCaretPosition(doc.getLength());//最简单的设置滚动条的位置到最后输出文本的地方//就是将JTextPane中的插入符的位置移动到文本的末端!
              }catch (BadLocationException e){}} else if(flag == PICUTER) {try{InputStream is = new ByteArrayInputStream(bt, 0, len);ImageInputStream iis = ImageIO.createImageInputStream(is);Iterator<ImageReader> itImage = ImageIO.getImageReaders(iis);if(itImage.hasNext()){ImageReader reader = itImage.next();ImageIcon ii = new ImageIcon( bt, reader.getFormatName() );tp.setCaretPosition( doc.getLength() );tp.insertComponent( new PicuterPanel(ii) );doc.insertString(doc.getLength(), "\n", set);}}catch(IOException ex){ex.printStackTrace();}catch(BadLocationException e1){}} else if(flag == FILE) {try{tp.setCaretPosition(doc.getLength());JLabel fileLabel = new JLabel(new String(bt, 0, len), myIcon, JLabel.LEFT);tp.insertComponent(fileLabel);doc.insertString(doc.getLength(), "\n", set);}catch (BadLocationException e) {e.printStackTrace();}}}
}class PicuterPanel extends JPanel{private ImageIcon ii;public PicuterPanel(ImageIcon ii){this.ii = ii;setPreferredSize(new Dimension(200, 300));setBackground(new Color(255, 255, 255));}protected void paintComponent(Graphics g) {super.paintComponent(g);g.drawImage(ii.getImage(), 0, 0, 200, 300, 0, 0, ii.getIconWidth(), ii.getIconHeight(), this);}
}
View Code
package testFour;import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;public class QQDialog extends Frame{private JPanel QQP = new JPanel();private JButton myFriend = new JButton("QQ好友>>");private JPanel funP = new JPanel();private JButton add = new JButton("添加好友");private JButton serach = new JButton("查询好友");private JPanel pp = new JPanel();private JPanel tmp = null;private final int QQPheight = 397;private JScrollPane jsp = new JScrollPane(QQP);private static Set<String>QQset;//IP的集合private static Map<String, QQFrame>QQmap;//IP到对通信窗口的映射private static Map<String, String>nameToIP;//姓名到IP的映射ImageIcon[] ii = new ImageIcon[3];public static Map<String, QQFrame> getMap(){return QQmap;}public static Set<String> getSet(){return QQset;}public static Map<String, String> getNameToIP(){return nameToIP;}public QQDialog(){QQset = new TreeSet<String>();QQmap = new TreeMap<String, QQFrame>();nameToIP = new TreeMap<String, String>();ii[0] = new ImageIcon("ff.jpg");ii[1] = new ImageIcon("gg.jpg");ii[2] = new ImageIcon("kk.jpg");setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));jsp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);jsp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);pp.add(jsp);pp.setLayout(new BoxLayout(pp, BoxLayout.Y_AXIS));QQP.setLayout(new BoxLayout(QQP, BoxLayout.Y_AXIS));JPanel pmyFriend = new JPanel();pmyFriend.setLayout(new BorderLayout());pmyFriend.add(myFriend, "Center");pmyFriend.setPreferredSize(new Dimension(300, 20));add(pmyFriend);pp.setPreferredSize(new Dimension(300, 400));add(pp);add(funP);JLabel myself = null;try {myself = new JLabel(InetAddress.getLocalHost().getHostName() + ":" + InetAddress.getLocalHost().getHostAddress());} catch (UnknownHostException e) {e.printStackTrace();}addLabelListener(myself);myself.setIcon(new ImageIcon("ff.jpg"));tmp = new JPanel();tmp.setLayout(new FlowLayout(FlowLayout.CENTER));tmp.setBackground(new Color(255, 0, 255));tmp.setPreferredSize(new Dimension(250, 60));tmp.add(myself);QQP.add(tmp);funP.setLayout(new FlowLayout(FlowLayout.CENTER));funP.add(add);funP.add(serach);myFriend.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {//将好友面板进行收缩的效果!if("QQ好友>>".equals(myFriend.getText())){QQP.setVisible(false);myFriend.setText("QQ好友<<");}else{QQP.setVisible(true);myFriend.setText("QQ好友>>");}}});add.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {InputDialog dlg = new InputDialog();if(dlg.key == InputDialog.CANCELBTN || dlg.key== 0) return;Random rd = new Random();int index = Math.abs(rd.nextInt()) % 3;JLabel ll = new JLabel(dlg.getNameText() + ":" + dlg.getIPText(), ii[index], JLabel.LEFT);QQset.add(dlg.getIPText());//将新增加的好友添加到集合中!
                        nameToIP.put(dlg.getNameText(), dlg.getIPText());tmp = new JPanel();tmp.setLayout(new FlowLayout(FlowLayout.CENTER));tmp.setBackground(new Color(255, 0, 255));/**  BoxLayout布局简直是蛋疼的要命,一个面板X是BoxLayout布局,如果该面板添加一个面板Y的话*  那么Y就会填充X面板!如果在添加一个面板Z, 那么Y, Z就会一起布满X面板!但是可以设置Y,Z面板*  的比例! 如果X添加的是一个按钮或者标签时,还不能控制其大小.....无语了!*  *  下面的我的做法将标签添加到面板tmp中,然后再将tmp添加中QQP面板中!这样就可以控制标签的大小了!*  再添加新的面板的时候,要设置一下之前面板的PreferredSize!保证每一个标签的距离适中!*  也就是保证所有的添加的面板的高度之和 == QQP.getHeight();* */int cnt = QQP.getComponentCount();//显示QQ好友的个数!if(cnt >= 1)QQP.getComponent(cnt-1).setPreferredSize(new Dimension(250, 60));int h = QQP.getHeight() - cnt*60;if(h < 0) h=60;tmp.setPreferredSize(new Dimension(250, h));tmp.add(ll);QQP.add(tmp);QQP.add(tmp);addLabelListener(ll);//给标签添加监听器JScrollBar jsb = jsp.getVerticalScrollBar();QQP.updateUI();//利用当前外观的值重置 UI 属性。 也可以保证滚动条随时的更新//终于搞好了,将垂直滚动条自动的移动到最低端jsp.getViewport().setViewPosition(new Point(0, jsp.getVerticalScrollBar().getMaximum()));}});serach.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {String name = JOptionPane.showInputDialog(null, "好友姓名", "好友查询", JOptionPane.OK_OPTION);if(name == null)  return ;String ip = nameToIP.get(name);if(ip == null)JOptionPane.showMessageDialog(null, "好友不存在!", "查询结果", JOptionPane.OK_OPTION);else{QQFrame fm = QQmap.get(ip);if(fm == null){try{String[] ipStr = ip.split("\\.");//将字符串中的数字分离byte[] ipBuf = new byte[4];//存储IP的byte数组for(int i = 0; i < 4; i++){ipBuf[i] = (byte)(Integer.parseInt(ipStr[i])&0xff);}fm = new QQFrame(name, ipBuf);}catch(RuntimeException ex){JOptionPane.showMessageDialog(null, ex.getMessage(), "Socket错误!", JOptionPane.OK_CANCEL_OPTION);return ;}QQmap.put(ip, fm);}else fm.requestFocus();}}});addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {if(qr == null) System.out.println("hehe");qr.setFlag();//udp服务线程停止ss.setFlag();//tcp服务线程停止System.exit(0);}});QQP.setBackground(new Color(255, 0, 255));funP.setBackground(new Color(255, 255, 0));setResizable(false);setSize(300, 500);setLocation(500, 200);setVisible(true);}public void addLabelListener(final JLabel lab){lab.setOpaque(true);lab.setBackground(new Color(255, 255, 255));lab.setPreferredSize(new Dimension(250, 55));String NameAndIP = lab.getText();String name = NameAndIP.substring(0, NameAndIP.indexOf(':'));String ip = NameAndIP.substring(NameAndIP.indexOf(':') + 1, NameAndIP.length());nameToIP.put(name, ip);QQset.add(ip);lab.addMouseListener(new MouseAdapter() {Color oldC = lab.getBackground();public void mouseEntered(MouseEvent e) {lab.setBackground(new Color(0, 255, 0));}public void mouseExited(MouseEvent e) {lab.setBackground(oldC);}public void mouseClicked(MouseEvent e) {//打开通信窗口if(e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() == 2){String NameAndIP = lab.getText();String name = NameAndIP.substring(0, NameAndIP.indexOf(':'));if(QQmap.get(nameToIP.get(name)) != null){//这个好友的窗口已经存在!
                                    QQmap.get(nameToIP.get(name)).requestFocus();return ;}QQFrame fm = null;try{String[] ipStr = nameToIP.get(name).split("\\.");//将字符串中的数字分离byte[] ipBuf = new byte[4];//存储IP的byte数组for(int i = 0; i < 4; i++){ipBuf[i] = (byte)(Integer.parseInt(ipStr[i])&0xff);}fm = new QQFrame(name, ipBuf);QQmap.put(nameToIP.get(name), fm);//name 和 窗口的映射!}catch(RuntimeException ex){JOptionPane.showMessageDialog(null, ex.getMessage(), "错误提示!", JOptionPane.OK_CANCEL_OPTION);return ;}}else if(e.getButton() == MouseEvent.BUTTON3){JPopupMenu pm = new JPopupMenu("胡峻峥");JMenuItem del = new JMenuItem("删除");del.setFont(new Font("华文行楷", Font.ITALIC, 20));del.setForeground(new Color(255, 0, 0));del.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {int choose = JOptionPane.showConfirmDialog(null, "确定删除!", "删除对话框", JOptionPane.YES_NO_OPTION);if(choose == JOptionPane.OK_OPTION){QQP.remove(lab.getParent());int cnt = QQP.getComponentCount();int h = QQP.getHeight() - (cnt-1)*60;if(h < 0) h=60;if(cnt >= 1)QQP.getComponent(cnt-1).setPreferredSize(new Dimension(250, h));cnt = QQP.getComponentCount();if(cnt*60 <= QQPheight)QQP.setPreferredSize(new Dimension(QQP.getWidth(), QQPheight));else QQP.setPreferredSize(new Dimension(QQP.getWidth(), cnt*60));QQP.updateUI();String NameAndIP = lab.getText();String ip = NameAndIP.substring(NameAndIP.indexOf(':') + 1);QQset.remove(ip);QQFrame fm = QQmap.get(ip);if(fm != null) fm.dispose();nameToIP.remove(NameAndIP.substring(0, NameAndIP.indexOf(':')));QQmap.remove(ip);}}});JMenuItem edit = new JMenuItem("编辑");edit.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {//得到之前的数据!String content = lab.getText();String oldName = content.substring(0, content.indexOf(':'));String oldIP = content.substring(content.indexOf(':')+1);//从新设置到文本框中InputDialog id = new InputDialog(oldIP, oldName, true);if(id.key == InputDialog.CANCELBTN || id.key== 0) return;lab.setText(id.NAME + ":" + id.IP);QQFrame fm = QQmap.get(oldIP);if(fm != null){try{fm.setSendIAD(id.IP);QQmap.put(id.IP, fm);fm.setTitle(id.NAME);QQmap.remove(oldIP);QQset.remove(oldIP);QQset.add(id.IP);nameToIP.remove(oldName);nameToIP.put(id.NAME, id.IP);}catch(RuntimeException ex){JOptionPane.showMessageDialog(null, ex.getMessage(), "修改之后的IP!", JOptionPane.OK_OPTION);}}}});edit.setFont(new Font("华文行楷", Font.ITALIC, 20));edit.setForeground(new Color(255, 0, 255));pm.setBorderPainted(true);pm.setBackground(new Color(125, 0, 125));pm.add(del);  pm.add(edit);pm.show(lab, e.getX(), e.getY());}}});}//内部类,用来访问Frame中的数据成员class InputDialog extends Dialog{private JLabel ipLabel = new JLabel("IP地址:");private JLabel nameLabel = new JLabel("姓名:");private JButton okBtn = new JButton("确定");private JButton cleBtn = new JButton("取消");private JTextField ipText = new JTextField(20);private JTextField nameText = new JTextField(20);private static final int OKBTN = 1;private static final int CANCELBTN = 2;private int key = 0;private String IP = null, NAME = null; private boolean flag = false;void initDialog(){JPanel p = null;setModal(true);setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));add(p = new JPanel());p.setLayout(new FlowLayout(FlowLayout.CENTER));ipLabel.setPreferredSize(new Dimension(50, 12));p.add(ipLabel); p.add(ipText);add(p = new JPanel());p.setLayout(new FlowLayout(FlowLayout.CENTER));nameLabel.setPreferredSize(new Dimension(50, 12));p.add(nameLabel); p.add(nameText);add(p = new JPanel());p.setLayout(new FlowLayout(FlowLayout.CENTER));p.add(okBtn); p.add(cleBtn);addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {key = 0;dispose();}});addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {requestFocus();}});okBtn.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {key = InputDialog.OKBTN;if("".equals(ipText.getText()) || "".equals(nameText.getText()) || !checkIP(ipText.getText()) )JOptionPane.showMessageDialog(null, "信息不全或者IP填写错误!");else{String ip = ipText.getText();if(!flag && QQset.contains(ip)){JOptionPane.showMessageDialog(null, "好友已存在!", "QQ好友", JOptionPane.OK_OPTION);return ;}IP = ipText.getText();NAME = nameText.getText();dispose();}}});cleBtn.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {key = InputDialog.CANCELBTN;dispose();}});setSize(300, 200);setLocation(200, 200);setVisible(true);}public InputDialog(String ip, String name){super(new Frame());ipText.setText(ip);nameText.setText(name);initDialog();}public InputDialog(String ip, String name, boolean flag){super(new Frame());ipText.setText(ip);nameText.setText(name);this.flag = flag;initDialog();}public InputDialog(){super(new Frame());initDialog();}public boolean checkIP(String ip){int i, begin = 0, end;for(i = 1; i <= 4; ++i){end = ip.indexOf('.', begin);if(end == -1) return false;int p = Integer.valueOf(ip.substring(begin, end));if(p < 0 || p > 255)  return false;}return true;}public String getIPText(){return IP;}public String getNameText(){return NAME;}}private static ServerSocketQQ ss = null;private static QQReceive qr = null;public static void main(String[] args){ss = new ServerSocketQQ();new Thread(ss).start();if(ServerSocketQQ.getPort() < 1) return;qr = new QQReceive();new Thread(qr).start();if(QQReceive.getPort() < 1)  return ;new QQDialog();}
}
View Code
package testFour;import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;
import java.util.Calendar;
import java.util.Iterator;
import java.util.Map;import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.filechooser.FileFilter;public class QQFrame extends Frame{private TextArea taSend = new TextArea();private JTextPane taReceive = new JTextPane();private JScrollPane p = new JScrollPane(taReceive);private JPanel pSend = new JPanel();private JPanel pReceive = new JPanel();private Label laSend = new Label("发送端.....");private Label laReceive = new Label("接收端.....");private JButton FileBtn = new JButton("传输文件");private JButton PicuterBtn = new JButton("发送图片");private InetAddress sendIAD = null;//当前窗口所对应的好友所在机器的IP对象private String QQname = null;//该对话框所对应的好友的姓名private Socket st =null;private String text;private DatagramSocket ds = null;public QQFrame(String name, byte[] ipBuf) throws RuntimeException{try {sendIAD = InetAddress.getByAddress(ipBuf);} catch (UnknownHostException e3) {throw new RuntimeException("IP错误(不存在或无发链接)!");}ds = QQReceive.getUdpSocket();if(ds == null)  throw new RuntimeException("udp Socket出错!");QQname = name;text = "";setSize(600, 600);setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));pSend.setLayout(new FlowLayout(FlowLayout.LEFT));pSend.add(laSend);pSend.add(FileBtn);pSend.add(PicuterBtn);pReceive.setLayout(new FlowLayout(FlowLayout.LEFT));pReceive.add(laReceive);taReceive.setForeground(new Color(255, 0, 255));add(pReceive);add(p);add(pSend);add(taSend);setTitle("我的好友 " + QQname);taSend.setPreferredSize(new Dimension(0, 200));taReceive.setPreferredSize(new Dimension(0, 400));taSend.setFont(new Font("仿宋", Font.PLAIN, 20));taReceive.setFont(new Font("黑体", Font.PLAIN, 25));taReceive.setEditable(false);//不能进行文本的编辑,但是可以进行访问
         taSend.addKeyListener(new KeyAdapter() {public void keyPressed(KeyEvent e) {if(e.getKeyCode() == KeyEvent.VK_ENTER){text = taSend.getText();if(text == null) return;text += "\n\n";byte[] bt = text.getBytes();DatagramPacket dp = null;try {//向指定的ip和端口发送数据~!//先说明一下数据是谁发送过来的!byte[] ip = InetAddress.getLocalHost().getHostAddress().getBytes();dp = new DatagramPacket(ip, ip.length, sendIAD, QQReceive.getPort());ds.send(dp);//这里主要是因为多可数据报包发送时会产生丢包的情况...所以暂停一段时间!try {Thread.sleep(100);} catch (InterruptedException e1) {}dp = new DatagramPacket("PARAGRAPH".getBytes(), "PARAGRAPH".getBytes().length, sendIAD, QQReceive.getPort());ds.send(dp);try {Thread.sleep(100);} catch (InterruptedException e1) {}dp = new DatagramPacket(bt, bt.length, sendIAD, QQReceive.getPort());ds.send(dp);} catch (IOException e1) {e1.printStackTrace();}synchronized(QQ.class){//发送端向接收窗口添加数据时 要 和 接收端向接收窗口添加数据时同步!byte[] x = null;try {x = new String(InetAddress.getLocalHost().getHostName() + " : ").getBytes();} catch (UnknownHostException e1) {e1.printStackTrace();}QQ.setTextPane(taReceive, x, x.length, QQ.PARAGRAPH, QQ.SEND);x = text.getBytes();QQ.setTextPane(taReceive, x, x.length, QQ.PARAGRAPH, QQ.SEND*3);taSend.requestFocus();}taSend.setText("");//发送端清空e.consume();//不让这个回车字符在输入端显示!return ;}}});addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {Map<String, QQFrame>mp = QQDialog.getMap();mp.remove(sendIAD.getHostAddress());dispose();}});FileBtn.addMouseListener(new MouseAdapter() {//文件传输public void mouseClicked(MouseEvent e) {JFileChooser jfc = new JFileChooser();jfc.showOpenDialog(null);File fl = jfc.getSelectedFile();if(fl == null) return ;try {st =  new Socket();//尝试连接对方st.connect(new InetSocketAddress(sendIAD, ServerSocketQQ.getPort()), 1000);} catch (IOException e2) {st = null;JOptionPane.showMessageDialog(null, "请求超时或连接错误!", "ServerSocket", JOptionPane.OK_CANCEL_OPTION);}if(st != null){try {byte[] bt = new byte[1024];InputStream is = st.getInputStream(); OutputStream os = st.getOutputStream();//先说明一下是谁发送过来的!byte[] ip = InetAddress.getLocalHost().getHostAddress().getBytes();os.write(ip);os.flush();try {Thread.sleep(100);} catch (InterruptedException e1) {}//向对方首先发送文件名, 然后发送文件内容!
                            os.write(fl.getName().getBytes());os.flush();try {Thread.sleep(100);} catch (InterruptedException e1) {}int len;InputStream fis = new FileInputStream(fl);while( (len = fis.read(bt)) != -1){os.write(bt, 0, len);os.flush();}bt = new String(Calendar.getInstance().getTime().toString() + ":文件已传输!").getBytes();QQ.setTextPane(taReceive, bt, bt.length, QQ.FILE, QQ.FILEX);st.shutdownOutput();//输出流结束,并标记一下,使服务端知道客户端输出已经结束了!
                            st.close();} catch (IOException e1) {e1.printStackTrace();}}}});PicuterBtn.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {JFileChooser jfc = new JFileChooser();jfc.setFileFilter(new PicuterFilter());//设置当前的文件过滤器!jfc.setAcceptAllFileFilterUsed(false);//设置所有文件过滤器不使用!//jfc.addChoosableFileFilter(new Filter());jfc.showOpenDialog(null);//将输入流按照下面方式处理, 根据Iterator<ImageReader> itImage是否能//成功的返回一个ImageReader对象确认该流文件是否是一个图片文件!//并ImageReader类中的getFormatName()得到文件的格式!//通过最后可以通过ImageIcon的byte[]构造函数建立ImageIcon对象!//最后将图片显示在面板上!File fl = jfc.getSelectedFile();if(fl == null) return ;try{InputStream is = new FileInputStream(fl);ImageInputStream iis = ImageIO.createImageInputStream(is);Iterator<ImageReader> itImage = ImageIO.getImageReaders(iis);if(itImage.hasNext()){ImageReader reader = itImage.next();byte[] imageByte = new byte[1024*64];int len = iis.read(imageByte);if(len > 64 * 1000){JOptionPane.showMessageDialog(new Frame(), "图片过大!请采用文件传输!");return ;}DatagramPacket dp = null;//先说明一下数据是谁发送过来的!byte[] ip = InetAddress.getLocalHost().getHostAddress().getBytes();dp = new DatagramPacket(ip, ip.length, sendIAD, QQReceive.getPort());ds.send(dp);try {Thread.sleep(100);} catch (InterruptedException e1) {}dp = new DatagramPacket("PICUTER".getBytes(), "PICUTER".getBytes().length, sendIAD, QQReceive.getPort());ds.send(dp);try {Thread.sleep(100);} catch (InterruptedException e1) {}dp = new DatagramPacket(imageByte, len, sendIAD, QQReceive.getPort());ds.send(dp);synchronized(QQ.class){byte[] name = null;name = new String(InetAddress.getLocalHost().getHostName() + " : ").getBytes();QQ.setTextPane(taReceive, name, name.length, QQ.PARAGRAPH, QQ.SEND);QQ.setTextPane(taReceive, imageByte, len, QQ.PICUTER, 0);}}else throw new NoPicuterException("不是一张图片!");}catch(IOException ex){ex.printStackTrace();}}});setVisible(true);}public void setSendIAD(String ip) throws RuntimeException{String[] ipStr = ip.split("\\.");//将字符串中的数字分离byte[] ipBuf = new byte[4];//存储IP的byte数组for(int i = 0; i < 4; i++){ipBuf[i] = (byte)(Integer.parseInt(ipStr[i])&0xff);}try {sendIAD = InetAddress.getByAddress(ipBuf);} catch (UnknownHostException e3) {throw new RuntimeException("IP错误(不存在或无发链接)!");}}public DatagramSocket getDs(){return ds;}public JTextPane getReceive(){return taReceive;}}class PicuterFilter extends FileFilter {public boolean accept(File file){return(file.isDirectory() || file.getName().endsWith(".gif") || file.getName().endsWith(".png") || file.getName().endsWith(".bmp")|| file.getName().endsWith(".jpg") );/* 返回要显示的文件类型 *//**   File.isDirectory()测试此抽象路径名表示的文件是否是一个目录*/}public String getDescription() {return("Picuter Files(*.gif, *.png, *.jpg, *.bmp)");                  //返回显示文件类型的描述
      }
}class NoPicuterException extends IOException{public NoPicuterException(String x){super(x);}
}
View Code
package testFour;import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import java.util.Map;
import java.util.Set;import javax.swing.JOptionPane;public class QQReceive implements Runnable{private QQFrame fm = null;private static DatagramSocket ds = null;private static int port = 10101;private boolean flag = true;public void setFlag(){if(ds != null)ds.close();flag = false;}public QQReceive(){try {//建立udp通信方式ds = new DatagramSocket(port);} catch (SocketException e1) {port = -1;JOptionPane.showMessageDialog(null, "DatagramSocket端口绑定错误!", "绑定错误", JOptionPane.OK_CANCEL_OPTION);}}public static DatagramSocket getUdpSocket(){return ds;} public static int getPort(){return port;}public DatagramSocket getDS(){return ds;}public void run(){if(ds == null) return ;byte[] bt = new byte[10024*10]; DatagramPacket dp = new DatagramPacket(bt, bt.length); while(flag){int flagx = 0;try {//ds.setReceiveBufferSize(size);
                    ds.receive(dp);String ip = new String(dp.getData(), 0, dp.getLength());ds.receive(dp);String tag = new String(dp.getData(), 0, dp.getLength());ds.receive(dp);Map<String, QQFrame>mp = QQDialog.getMap();Set<String>  set = QQDialog.getSet();fm = mp.get(ip);System.out.println(ip);if(set.contains(ip) && fm == null){//如果存在该好友,但是不存在对应的对话窗口//自动产生对话窗口Map<String, String> nameToIP = QQDialog.getNameToIP();String[] ipStr = ip.split("\\.");//将字符串中的数字分离byte[] ipBuf = new byte[4];//存储IP的byte数组for(int i = 0; i < 4; i++){ipBuf[i] = (byte)(Integer.parseInt(ipStr[i])&0xff);}String name = null;for(String ss : nameToIP.keySet())if( ip.equals(nameToIP.get(ss)) ){name = ss;break;}fm = new QQFrame(name, ipBuf);}else if(fm != null){fm.requestFocus();}if( tag.equals("FILE") )flagx = QQ.FILE;else if( tag.equals("PICUTER"))flagx = QQ.PICUTER;else if( tag.equals("PARAGRAPH"))flagx = QQ.PARAGRAPH;} catch (IOException e) {e.printStackTrace();}synchronized(QQ.class){if(fm == null) continue;byte[] x = new String(dp.getAddress().getHostName() + " : ").getBytes();QQ.setTextPane(fm.getReceive(), x, x.length, QQ.PARAGRAPH, QQ.RECEIVE);QQ.setTextPane(fm.getReceive(), dp.getData(), dp.getLength(), flagx, QQ.RECEIVE*3);}}}
}
View Code
package testFour;import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Calendar;
import java.util.Map;
import java.util.Set;import javax.swing.JOptionPane;public class ServerSocketQQ implements Runnable{private ServerSocket sst = null;private Socket st = null;private static int port = 10100;private boolean flag = true;public void setFlag(){if(sst != null)try {sst.close();} catch (IOException e) {e.printStackTrace();}flag = false;}public static int getPort(){return port;}public ServerSocketQQ(){//建立服务端try {sst = new ServerSocket(port);} catch (IOException e) {port = -1;JOptionPane.showMessageDialog(null, "ServerSocket端口绑定错误!", "绑定错误", JOptionPane.OK_CANCEL_OPTION);}}public void run(){byte[] bt = null;int len = 0;if(sst == null)  return ;while(true){try{//侦听并接受到此服务套接字的连接。此方法在进行连接之前一直阻塞。 创建新套接字 st = sst.accept();//得到客户端传输过来的流InputStream is = st.getInputStream();OutputStream os  = st.getOutputStream();bt = new byte[1024];len = is.read(bt);String name = new String(bt, 0, len);QQFrame fm = null;Map<String, QQFrame>mp = QQDialog.getMap();Set<String>  set = QQDialog.getSet();fm = mp.get(name);if(set.contains(name) && fm == null){//如果存在该好友,但是不存在对应的对话窗口//自动产生对话窗口Map<String, String> nameToIP = QQDialog.getNameToIP();fm = new QQFrame(name, nameToIP.get(name).getBytes());}if(fm == null){//对方不存在该好友!
                    st.close();continue;}len = is.read(bt);String fileName = new String(bt, 0, len);int choose = JOptionPane.showConfirmDialog(null, "接受或不接受", "文件传输提示", JOptionPane.YES_NO_OPTION);if(choose == JOptionPane.NO_OPTION){bt = new String(Calendar.getInstance().getTime().toString() + ":文件接受失败!").getBytes();QQ.setTextPane(fm.getReceive(), bt, bt.length, QQ.FILE, QQ.FILEX);st.close();continue;}FileOutputStream fos = new FileOutputStream( new File(fileName) );while( (len = is.read(bt)) != -1 ){//先将流文件变成byte[], 然后利用套接字的输出流发送给客户端
                     fos.write(bt);fos.flush();}bt = new String(Calendar.getInstance().getTime().toString() + ":文件接受成功!").getBytes();QQ.setTextPane(fm.getReceive(), bt, bt.length, QQ.FILE, QQ.FILEX);st.close();}catch(IOException e){e.printStackTrace();}}}}
View Code

 

转载于:https://www.cnblogs.com/hujunzheng/p/4004368.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/531620.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

修改Linux启动后的默认颜色,更改linux目录的默认颜色(我选择了Yellow)

在控制台下&#xff0c;用ls&#xff0c;就会发现&#xff0c;shell将不同类型的文件项目显示为不同的颜色。者可以提高效率&#xff0c;不用ls -l便能大概的把各个文件的类型情况了解一下。你有没有想过更改这个着色配置呢&#xff1f;其 实&#xff0c;在/etc下有一个DIR_COL…

AC_Dream 1216 G - Beautiful People

题意&#xff1a;有n个人每人有一个力气值Si,美丽值Bi&#xff0c;满足Bi>Bj&&Si>Sj 或者 Bi<Bj&&Si<Sj 的人可以一起参见晚会&#xff0c;问最多有多少人可以一起参见晚会。思路&#xff1a; 我们根据S从小到大将所有人排序&#xff0c;然后看B最…

云主机用linux还是winows,云服务器一般使用什么系统?Linux还是Windows?

云服务器一般使用什么系统?最常用的就是Linux以及Windows系统&#xff0c;两大系统各有不同优势&#xff0c;大家选择上也是存在差异的&#xff0c;接下来跟着小编来了解一下吧。Windows系统&#xff1a;一般情况来说&#xff0c;Windows系统常用的是Server 2003和Server 2008…

c语言程序中return的作用,单片机C语言程序中return dat 什么意思

/* 打开 ISP,IAP 功能 */void ISP_IAP_enable(void){EA 0; /* 关中断 */ISP_CONTR ISP_CONTR & 0x18; /* 0001,1000 */ISP_CONTR ISP_CONTR | WaitTime; /* 写入硬件延时 */ISP_CONTR ISP_CONTR | 0x80; /* ISPEN1 */}/* 关闭 ISP,IAP 功能 *…

java中DatagramSocket连续发送多个数据报包时产生丢包现象解决方案

1 try {2 //向指定的ip和端口发送数据~&#xff01;3 //先说明一下数据是谁发送过来的&#xff01;4 byte[] ip InetAddress.getLocalHost().getHostAddress().getBytes();5 …

二级c语言程序设计bug,《C语言及程序设计》实践项目——发现Bug

返回&#xff1a;贺老师课程教学链接【项目1-sin泰勒展式中的错误】下面是sin函数的泰勒展式&#xff1a;(注&#xff1a;x取弧度值&#xff0c;而非角度值)编写了double mysin(double x)用于求sin值&#xff0c;却“死”在了123上。剧透一下&#xff0c;循环没有问题(当然问题…

AC_Dream 1224 Robbers(贪心)

题意&#xff1a;n个抢劫犯分别抢到的金钱是k1, k2, k3,...&#xff0c;一共得到的金钱是m&#xff0c; 但是在分钱的时候是按照x1/y, x2/y, x3/y,....的比例进行分配的&#xff01;这样的话 一些抢劫犯就会觉得不公平&#xff0c;不公平度为|xi/y - ki/m|(浮点运算)&#xff0…

C语言编程出图形,C语言画出各种图形

矩形&#xff1a;(里面是空的)******** ** ** ********Program ended with exit code: 0for (int i 0; i < 5; i ) {for (int j 0; j < 7; j ) {//用条件判断打出*号if (i 0 || i 4 || j 0 || j 6 ) {printf("*");}else{printf(" "…

AC_Dream 1211 Reactor Cooling

1 /*2 题意&#xff1a;无源无汇&#xff0c;并且每条边的容量有上下界限的网络流问题&#xff01;既然无源无汇&#xff0c;那么素有的节点都应该满足“入流出流”&#xff01;3 输出每一条边的流量&#xff0c;使得满足上面的条件。&#xff08;如果u->v有流…

c语言中const对于define优点,为什么大多数C开发人员使用define而不是const?

这有一个非常可靠的原因&#xff1a;C中的const并不意味着一些常量。 这只是意味着一个variables是只读的。在编译器需要一个常量的地方(例如非VLA数组的数组大小)&#xff0c;使用constvariables(如fieldWidth是不可能的。他们不一样const只是一个限定符&#xff0c;它表示一个…

c语言程序设计期末试卷A,《C语言程序设计》期末试卷(A)..doc

《C语言程序设计》期末试卷(A).2011-12-1学期《C语言程序设计》期末试卷(A)班级____________姓名____________学号________________大题号一二三四总分得 分判卷 /核分人“一、选择题”使用答题卡选择。“二、看程序写运行结果”答题处&#xff1a;题号答 案二、1二、2二、3“三…

codeforces B. Strongly Connected City(dfs水过)

题意&#xff1a;有横向和纵向的街道&#xff0c;每个街道只有一个方向&#xff0c;垂直的街道相交会产生一个节点&#xff0c;这样每个节点都有两个方向&#xff0c; 问是否每一个节点都可以由其他的节点到达.... 思路&#xff1a;规律没有想到&#xff0c;直接爆搜&#xff0…

c语言数组两个值交换,如可交换两个数组中的元素?

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼#include #include #include int main(void){int a[]{1,2,3,4,5,6,7,8};int b[]{9,10,11,12,13,15};int lena,lenb,randa,randb,randtimes;int i,temp;srand((unsigned)time(NULL));lena sizeof(a)/sizeof(int);lenb sizeof(b)/s…

Uvaoj 11248 Frequency Hopping(Dinic求最小割)

题意&#xff1a;1到n节点&#xff08;节点之间有一定的容量&#xff09;&#xff0c;需要流过C的流量&#xff0c;问是否可以&#xff1f;如果可以输出possible&#xff0c; 否则如果可以扩大任意一条边的容量 可以达到目的&#xff0c;那么输出possible option&#xff1a;接…

随机数归并排序c语言,用C语言实现归并排序

#include#include#include#include#define random(i) (rand()%i)#define N 12#define INFINITY 99999999//要排序的数存放在a数组汇总&#xff0c;p,q,r是数组下标void Merge(int *a,int p,int q,int r){int n1q-p1;int n2r-q;int *L(int *)malloc(sizeof(int)*n1);int *R(int …

UVAoj 11324 - The Largest Clique(tarjan + dp)

题意&#xff1a;给定一个有向图&#xff0c;寻找一个点数最大集合&#xff0c;使得这个集合中的任意两个点 u,v, 都有u->v 或者 v->u 或者u<>v 思路&#xff1a;首先将强连通分量通过tarjan算法求出来&#xff0c;然后进行缩点&#xff0c;也就是每一个缩点 所组成…

android开发蓝牙自动连接电脑上,Android蓝牙开发之自动连接设备

自动连接使用的是SharedPreferences这个来解决。private void Automaticconnection() {SharedPreferences sp getSharedPreferences("Dizhi", MODE_PRIVATE);String address sp.getString("address", "");if (!address.equals("")) …

hdu 2014鞍山赛区 5073 Galaxy

题意&#xff1a;就是给你 n 个数&#xff0c;代表n个星球的位置&#xff0c;每一个星球的重量都为 1 &#xff01; 开始的时候每一个星球都绕着质心转动&#xff0c;那么质心的位置就是所有的星球的位置之和 / 星球的个数 现在让你移动 k 个星球到任意位置&#xff08;多个星球…

android onitemclicklistener 参数,android – 对listview中的项使用setOnItemClickListener

大家好,有一个应用程序,可以在SD卡上保存音频.我创建了一个listview,它从sdcard中检索文件名.我正在尝试设置一个监听器,所以当单击文件名时,我可以启动另一个播放该文件的意图.当我尝试设置监听器并传入一个新的OnItemClickListener()时,eclipse是红色的下划线.我知道我必须实…

DRF之请求与响应

目录 一、模块与包回顾 二、反序列化校验源码分析(了解) 三、断言 四、drf之请求 【1】源码分析 【2】配置视图类能处理的编码格式 五、drf之响应 【1】源码 【2】响应编码格式 一、模块与包回顾 模块与包 什么是模块&#xff1f; 一个py文件&#xff0c;被别的py文件…