基于Java Socket实现文件传输

2024-11-22 10:23:06
推荐回答(1个)
回答1:

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

public class Client1 extends JFrame
{
private JTextField enter,Iptext;
private JTextArea display;
private JLabel Iplabel;
private JPanel upPanel;
JButton clientMessageButton,connectButton,DisConnectButton;
ObjectOutputStream output;
ObjectInputStream input;
Socket socket1;
String message=" ";
public Client1()
{
super("客户端");
Container c=getContentPane();
clientMessageButton=new JButton("发送");
c.add(clientMessageButton,BorderLayout.EAST);
clientMessageButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{sendData(enter.getText());
}});
DisConnectButton=new JButton("断开连接");
DisConnectButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{DisConnect();
}});

Iptext=new JTextField(10);
Iptext.setText("");
Iptext.setEnabled(true);
Iptext.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{connect();
}});
Iplabel=new JLabel("服务器IP:");
connectButton=new JButton("连接");
connectButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{connect();
}});
upPanel=new JPanel();
upPanel.add(connectButton);
upPanel.add(DisConnectButton);
upPanel.add(Iplabel);
upPanel.add(Iptext);

enter=new JTextField();
enter.setEnabled(true);
enter.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{sendData(enter.getText());
}});
c.add(enter,BorderLayout.SOUTH);
c.add(upPanel,BorderLayout.NORTH);
display=new JTextArea();
c.add(new JScrollPane(display),BorderLayout.CENTER);
setSize(300,450);
show();
}
public void connect()
{
try
{
display.setText("准备连接...\n");
String s=Iptext.getText();
socket1=new Socket(InetAddress.getByName(s),8888);
display.append("连接到: "+socket1.getInetAddress().getHostName());
display.append("\n主机IP为:"+socket1.getInetAddress().toString());
output=new ObjectOutputStream(socket1.getOutputStream());
output.flush();

do{
try{
message=(String)input.readObject();
display.append("\n"+message);
}
catch(IOException e)
{
display.append("\n无法获得信息");
}
catch(ClassNotFoundException e)
{}
}while(!message.equals("end"));
display.append("\n关闭连接");
output.close();
input.close();
socket1.close();
}
catch(EOFException eof)
{
System.out.println(eof);
}
catch(IOException e)
{
e.printStackTrace();
}
}

public void DisConnect()
{
try
{
display.append("\n关闭连接");
output.close();
input.close();
socket1.close();
}
catch(Exception e)
{}
}

private void sendData(String s)
{
try
{
message=s;
output.writeObject("客户消息: "+s);
display.append("\n客户:"+s);
output.flush();
enter.setText(" ");
}
catch(Exception e)
{
display.append("\n发生错误");
}
}
public static void main(String args[])//throws NullPointerException
{
Client1 app=new Client1();
app.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{System.exit(0);}});
//app.connect();
}
}
//服务端 2007-12-30 17:33
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class Server1 extends JFrame
{
private JTextArea display;
private JTextField enter;
JButton sysMessageButton;
JButton stopMessageButton,startServerButton;
JPanel upPanel;
ServerSocket server;
Socket socket1;
ObjectOutputStream output;
ObjectInputStream input;

public Server1()
{
super("服务器端");
Container c=getContentPane();
enter=new JTextField();
setSize(50,20);
upPanel=new JPanel();
sysMessageButton=new JButton("发送");
c.add(sysMessageButton,BorderLayout.EAST);
setSize(50,20);
stopMessageButton=new JButton("断开连接");
c.add(stopMessageButton,BorderLayout.WEST);
stopMessageButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
stopService();}});