to86293425 2015-01-04 08:45 采纳率: 0%
浏览 2410

Java 网络编程 聊天软件和发送文件

package ch09;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;

import javax.swing.*;

public class Server extends JFrame implements ActionListener,Runnable{
JTextArea showArea = new JTextArea(); //聊天区域
JLabel lb1 = new JLabel("连接的端口"); //端口栏便签
JTextField tf1 = new JTextField(); //端口栏
JTextField tf2 = new JTextField(); //发送栏
JButton link = new JButton("开启"); //连接按钮
JButton send = new JButton("发送");
JScrollPane JSPane = new JScrollPane(showArea);
JPanel pan1 = new JPanel();
JPanel pan2 = new JPanel();

JMenuBar menuBar = new JMenuBar();                  //菜单栏
JMenu menuFile = new JMenu("文件");                   //菜单
JMenuItem sentFile = new JMenuItem("发送文件");     //菜单项
JMenuItem select = new JMenuItem("历史记录");
JMenuItem exit = new JMenuItem("退出");               //菜单项

Thread thread = null;
Thread thread2 = null;
Thread thread3 = null;
ServerSocket serverSocket;
Socket connectToClient;
DataInputStream dis;
DataOutputStream dos;

public Server(){
    this.setTitle("聊天小工具");
    initialize();
    this.setBounds(100,100,650,350);
    this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public void initialize(){
    tf1.setText("5500");
    tf1.setEditable(false);

    tf1.setColumns(8);
    send.setEnabled(false);
    tf2.setEnabled(false);

    pan1.add(lb1);
    pan1.add(tf1);
    pan1.add(link);

    tf2.setColumns(40);
    pan2.add(tf2);
    pan2.add(send);

    menuFile.add(sentFile);                         //添加文件菜单项
    menuFile.add(select);
    menuFile.add(exit);                             //添加删除菜单项
    menuBar.add(menuFile);                          //添加菜单栏菜单
    this.setJMenuBar(menuBar);                      //窗框设置菜单栏

    this.add(pan1,BorderLayout.NORTH);
    this.add(JSPane,BorderLayout.CENTER);
    this.add(pan2,BorderLayout.SOUTH);


    tf2.addActionListener(this);
    exit.addActionListener(this);
    send.addActionListener(this);
    link.addActionListener(this);
    sentFile.addActionListener(this);

    thread3 = new Thread(this,"触发客户端");
    thread3.start();



}

public void actionPerformed(ActionEvent e){
    try{
        if(e.getSource()==sentFile){
            thread2 = new Thread(this,"文件");
            thread2.setPriority(Thread.MIN_PRIORITY);
            thread2.start();
        }
        else if(e.getSource()==link){
            thread = new Thread(this,"信息");
            thread.setPriority(Thread.MAX_PRIORITY);
            thread.start();
            tf2.setEnabled(true);
            send.setEnabled(true);
        }
        else if(e.getSource()==exit){
            System.exit(0);
        }
        else{
            String s = tf2.getText();
            if(s.length()>0){
                dos.writeUTF(s);

                showArea.append("我说:"+tf2.getText()+"\n");
                tf2.setText(null);
            }
        }
    }
    catch(IOException el){
        showArea.append("你的消息:"+tf2.getText()+"未能发送出去\n");
    }
}

public void run() {
    try{
        if(Thread.currentThread()==thread3){
            try{
                serverSocket = new ServerSocket(Integer.parseInt(tf1.getText()));
                showArea.append("正在等待对话请求\n");
                connectToClient = serverSocket.accept();
                showArea.append("连接成功,请说话\n");
                dis = new DataInputStream(connectToClient.getInputStream());
                dos = new DataOutputStream(connectToClient.getOutputStream());
            }
            catch(IOException el){
                showArea.append("对不起,不能创建服务器\n");
                tf2.setEnabled(false);
                send.setEnabled(false);
            }
        }
        else if(Thread.currentThread()==thread){
            while(true){
                showArea.append("对方说:"+dis.readUTF()+"\n");
                Thread.sleep(1000);
            }
        }
        else{
            JFileChooser chooser = new JFileChooser();
            String target;
            if(chooser.showOpenDialog(null)==JFileChooser.APPROVE_OPTION){
                target = chooser.getSelectedFile().toString();
                FileInputStream fis = new FileInputStream(target);
                byte[] buffer = new byte[1024];
                int len;
                while(((len=fis.read(buffer))>0)){
                    dos.write(buffer,0,len);
                }   
                fis.close();
            }
        }
    }
    catch(IOException el){}
    catch(InterruptedException e){}
}

public static void main(String[] args) {
    new Server();
}

}

客户端
package ch09;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;

import javax.swing.*;

public class Client extends JFrame implements Runnable,ActionListener{
String ip;
JTextArea showArea = new JTextArea(); //聊天区域
JLabel lb1 = new JLabel("连接的端口"); //端口栏便签
JLabel lb2 = new JLabel("连接的服务器IP");
JTextField tf1 = new JTextField(); //端口栏
JTextField tf2 = new JTextField();

JTextField tf3 = new JTextField(); //发送栏
JButton link = new JButton("连接"); //连接按钮
JButton send = new JButton("发送");
JScrollPane JSPane = new JScrollPane(showArea);
JPanel pan1 = new JPanel();
JPanel pan2 = new JPanel();

JMenuBar menuBar = new JMenuBar();                  //菜单栏
JMenu menuFile = new JMenu("文件");                   //菜单
JMenuItem sentFile = new JMenuItem("发送文件");     //菜单项
JMenuItem select = new JMenuItem("历史记录");
JMenuItem exit = new JMenuItem("退出");               //菜单项


Thread thread = null;
Thread thread2 = null;
Socket connectToServer;
DataInputStream dis;
DataOutputStream dos;

public Client(){

}

public Client(String IP){
    this.ip = IP;
    this.setTitle("客户端");
    initialize();
    this.setBounds(100,100,650,350);
    this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    try{
        connectToServer = new Socket(tf2.getText(),Integer.parseInt(tf1.getText()));
        dis = new DataInputStream(connectToServer.getInputStream());
        dos = new DataOutputStream(connectToServer.getOutputStream());
        showArea.append("连接成功,请说话\n");

    }
    catch(IOException el){
        showArea.append("对不起,未能连接服务器\n");
        tf3.setEnabled(false);
        send.setEnabled(false);
    }
}

public void initialize(){
    tf1.setText("5500");
    tf2.setText(ip);
    tf1.setEditable(false);
    tf2.setEditable(false);

    tf1.setColumns(8);
    tf2.setColumns(8);
    send.setEnabled(false);
    tf3.setEnabled(false);

    pan1.add(lb1);
    pan1.add(tf1);
    pan1.add(lb2);
    pan1.add(tf2);
    pan1.add(link);

    tf3.setColumns(40);
    pan2.add(tf3);
    pan2.add(send);

    menuFile.add(sentFile);                         //添加文件菜单项
    menuFile.add(select);
    menuFile.add(exit);                             //添加删除菜单项
    menuBar.add(menuFile);                          //添加菜单栏菜单
    this.setJMenuBar(menuBar);                      //窗框设置菜单栏

    this.add(pan1,BorderLayout.NORTH);
    this.add(JSPane,BorderLayout.CENTER);
    this.add(pan2,BorderLayout.SOUTH);

    tf3.addActionListener(this);
    exit.addActionListener(this);
    send.addActionListener(this);
    link.addActionListener(this);
    sentFile.addActionListener(this);
}

public void actionPerformed(ActionEvent e){
    try{
        if(e.getSource()==sentFile){                        
            thread2 = new Thread(this,"文件");
            thread2.setPriority(Thread.MIN_PRIORITY);
            thread2.start();
        }
        else if(e.getSource()==link){

            thread = new Thread(this,"信息");
            thread.setPriority(Thread.MIN_PRIORITY);
            thread.start();
            tf3.setEnabled(true);
            send.setEnabled(true);
        }   
        else if(e.getSource()==exit){
            System.exit(0);
        }
        else{
            String s = tf3.getText();
            if(s.length()>0){
                dos.writeUTF(s);
                dos.flush();
                showArea.append("我说:"+tf3.getText()+"\n");
                tf3.setText(null);
            }
        }
    }
    catch(IOException el){
        showArea.append("你的消息:"+tf3.getText()+"未能发送出去\n");
    }
}

public void run() {
    try{
        if(Thread.currentThread()==thread){
            while(true){
                showArea.append("对方说:"+dis.readUTF()+"\n");
                Thread.sleep(1000);
            }
        }
        else{
            JFileChooser chooser = new JFileChooser();
            String source;
            if(chooser.showSaveDialog(null)==JFileChooser.APPROVE_OPTION){
                source = chooser.getSelectedFile().toString();
                FileOutputStream fos = new FileOutputStream(source);
                byte[] buffer = new byte[1024];
                int len;
                while(((len=dis.read(buffer))>0)){
                    fos.write(buffer,0,len);
                }

// dis.close();
fos.flush();
fos.close();
}
}

    }
    catch(IOException el){}
    catch(InterruptedException e){}
}

}

问题一 为什么当我发送消息后,就不能发送文件了
问题二 为什么只能发送一次文件,当我再次发送就会变成0字节

  • 写回答

2条回答 默认 最新

  • oyljerry 2015-01-04 11:08
    关注

    多加点log,分析一下,看代码什么地方处理不对

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题