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字节