基于Java Socket实现文件传输
1)编写服务器和客户端程序实现文件传输功能。2)服务器端监听客户端连接,一旦监听到客户端连接,则发送指定文件到客户端。3)要有窗口...
1)编写服务器和客户端程序实现文件传输功能。
2)服务器端监听客户端连接,一旦监听到客户端连接,则发送指定文件到客户端。
3)要有窗口 展开
2)服务器端监听客户端连接,一旦监听到客户端连接,则发送指定文件到客户端。
3)要有窗口 展开
2013-08-10
展开全部
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());<br/>}});
DisConnectButton=new JButton("断开连接");
DisConnectButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{DisConnect();<br/>}});
Iptext=new JTextField(10);
Iptext.setText("");
Iptext.setEnabled(true);
Iptext.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{connect();<br/>}});
Iplabel=new JLabel("服务器IP:");
connectButton=new JButton("连接");
connectButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{connect();<br/>}});
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());<br/>}});
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();}});
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());<br/>}});
DisConnectButton=new JButton("断开连接");
DisConnectButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{DisConnect();<br/>}});
Iptext=new JTextField(10);
Iptext.setText("");
Iptext.setEnabled(true);
Iptext.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{connect();<br/>}});
Iplabel=new JLabel("服务器IP:");
connectButton=new JButton("连接");
connectButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{connect();<br/>}});
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());<br/>}});
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();}});
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询