udt传输文件
⑴ 谁有基于UDT传输文件的例子(java)
UDP传输吧。
如果是UDP的话,需要传输结束符。
----------------------------------------------------------------------
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Client extends JFrame implements ActionListener {
private String file = "D:\\send.doc";
private boolean isSend = false;
public Client() {
getContentPane().setLayout(null);
JLabel lblNewLabel = new JLabel("Send File : " + file);
lblNewLabel.setBounds(12, 10, 202, 22);
getContentPane().add(lblNewLabel);
JButton btnNewButton = new JButton("Send");
btnNewButton.setBounds(12, 63, 93, 23);
btnNewButton.addActionListener(this);
getContentPane().add(btnNewButton);
new SendThread().start();
setSize(300, 150);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
}
public static void main(String[] args) {
new Client();
}
public void actionPerformed(ActionEvent actionevent) {
if (isSend) {
return;
}
isSend = true;
}
class SendThread extends Thread {
public void run() {
try {
DatagramSocket client = new DatagramSocket();
InetAddress addr = InetAddress.getByName("127.0.0.1");
int port = 5050;
DatagramPacket sendPacket = new DatagramPacket("".getBytes(),
0, addr, port);
while (true) {
try {
if (isSend) {
FileInputStream fin = new FileInputStream(file);
int len = 0;
byte[] buff = new byte[4096];
while ((len = fin.read(buff)) != -1) {
sendPacket.setData(buff);
sendPacket.setLength(len);
client.send(sendPacket);
}
sendPacket.setData("end".getBytes());
sendPacket.setLength("end".getBytes().length);
client.send(sendPacket);
isSend = false;
}
sleep(500);
} catch (Exception e) {
System.out.println("ignore");
}
}
} catch (Exception e) {
System.out.println("client init error");
System.exit(0);
}
}
}
}
------------------------------------------------------------------
import java.awt.event.ActionEvent;
public class Server extends JFrame implements ActionListener {
private String file = "D:\\receive.doc";
private JLabel mesg = null;
private boolean isReceive = false;
public Server() {
getContentPane().setLayout(null);
mesg = new JLabel("");
mesg.setBounds(12, 10, 202, 22);
getContentPane().add(mesg);
JLabel label = new JLabel("Receive File : D:\\receive.doc");
label.setBounds(12, 43, 202, 22);
getContentPane().add(label);
JButton btnNewButton = new JButton("Receive");
btnNewButton.setBounds(12, 75, 93, 23);
btnNewButton.addActionListener(this);
getContentPane().add(btnNewButton);
new ReceiveThread().start();
setSize(300, 150);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
}
public static void main(String[] args) {
new Server();
}
public void actionPerformed(ActionEvent actionevent) {
if (isReceive) {
return;
}
isReceive = true;
}
class ReceiveThread extends Thread {
public void run() {
try {
DatagramSocket server = new DatagramSocket(5050);
byte[] recvBuf = new byte[4096];
DatagramPacket recvPacket = new DatagramPacket(recvBuf,
recvBuf.length);
while (true) {
try {
System.out.println(server.getSendBufferSize());
if (server.getSendBufferSize() > 0) {
mesg.setText("client Send file!");
}
if (isReceive) {
FileOutputStream out = new FileOutputStream(file);
while (server.getSendBufferSize() > 0) {
server.receive(recvPacket);
if("end".equals(new String(recvPacket.getData()))){
break;
}
out.write(recvPacket.getData(), 0, recvPacket
.getLength());
}
out.close();
isReceive = false;
}
sleep(500);
} catch (Exception e) {
System.out.println("ignore");
}
}
} catch (Exception e) {
System.out.println("Server init error");
System.exit(0);
}
}
}
}