62,621
社区成员
发帖
与我相关
我的任务
分享

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
public class UDPDemo {
public static void main(final String[] s) throws IOException, InterruptedException {
new Thread(new ChannelReader()).start();
DatagramChannel channel = DatagramChannel.open();
channel.connect(new InetSocketAddress("localhost", 9999));
ByteBuffer buffer = ByteBuffer.allocate(50);
int i = 0;
while (true) {
buffer.putInt(i++);
buffer.flip();
channel.write(buffer);
buffer.clear();
Thread.sleep(1000);
}
}
private static class ChannelReader implements Runnable {
@Override
public void run() {
try {
DatagramChannel channel = DatagramChannel.open();
channel.bind(new InetSocketAddress("localhost", 9999));
ByteBuffer buffer = ByteBuffer.allocate(50);
while (true) {
channel.receive(buffer);
System.out.println(buffer.getInt(0));
buffer.clear();
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}
package fileTransport;
import java.awt.FileDialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
public class GetFile {
/**
* @param args
* @throws UnknownHostException
*/
public static void main(String[] args) throws UnknownHostException {
GetFile g;
try {
g = new GetFile(InetAddress.getLocalHost(),11111,"download");
g.getFile();
} catch (UnknownHostException e1) {
e1.printStackTrace();
}
}
private DatagramSocket socket = null;
private String name="";
private InetAddress ip = null;
private int port = 0 ;
private String saveName=null;
public GetFile(InetAddress ip ,int port,String name){
this.ip = ip ;
this.port = port ;
this.name = name;
try {
this.socket = new DatagramSocket();
} catch (SocketException e) {
e.printStackTrace();
}
}
public String chooseSavePath(){
try {
if (System.getProperty("os.name").toUpperCase().indexOf("WINDOWS") != -1){
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
} catch (Exception e1) {
System.out.println("设置界面感官异常!");
e1.printStackTrace();
}
JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogType(JFileChooser.SAVE_DIALOG);
fileChooser.setSelectedFile(new File(name));
int i = fileChooser.showSaveDialog(null);
System.out.println(i);
if(i == JFileChooser.APPROVE_OPTION){
if(fileChooser.getSelectedFile().exists()){
JOptionPane.showConfirmDialog(null, "文件已存在,请重新命名");
return chooseSavePath();
}
return fileChooser.getSelectedFile().getAbsolutePath();
}
return null;
}
public void getFile(){
String path = chooseSavePath();
System.out.println(path);
if(path!=null){
byte[] b = "ok".getBytes();
DatagramPacket p = new DatagramPacket(b,b.length);
p.setPort(port);
p.setAddress(ip);
try {
socket.send(p);
} catch (IOException e) {
e.printStackTrace();
}
boolean end = false;
while(!end){
byte[] data = new byte[1024];
DatagramPacket p1 = new DatagramPacket(data,data.length);
try {
socket.receive(p1);
} catch (IOException e) {
e.printStackTrace();
}
String msg = new String(p1.getData()).trim();
System.out.println(msg);
if(msg.equals("over")){
end = true;
System.out.println(end);
}
else{
String type = msg.substring(0, 4);
String newName = path+msg.substring(msg.indexOf(name)+name.length());
if(type.equals("path")){
File fpath = new File(newName);
if(fpath.exists()){
JOptionPane jop = new JOptionPane();
JButton[] options = new JButton[3];
options[0]= new JButton("覆盖");
options[1] = new JButton("保存为:"+this.saveName+"(new)");
options[2] = new JButton("取消");
jop.setOptions(options);
jop.setVisible(true);
}
else{
fpath.mkdirs();
}
byte[] reb = "path".getBytes();
DatagramPacket respons = new DatagramPacket(reb,reb.length);
respons.setAddress(ip);
respons.setPort(port);
try {
socket.send(respons);
} catch (IOException e) {
e.printStackTrace();
}
}
else if(type.equals("file")){
File file = new File(newName.substring(0, newName.indexOf("length")));
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
byte[] reb = "file".getBytes();
DatagramPacket respons = new DatagramPacket(reb,reb.length);
respons.setAddress(ip);
respons.setPort(port);
try {
socket.send(respons);
} catch (IOException e) {
e.printStackTrace();
}
String s1=newName.replaceAll(".*[^\\d](?=(\\d+))","");
long size = Long.valueOf(s1);
long sum = 0;
byte[] buffer = new byte[10001];
DatagramPacket pk = new DatagramPacket(buffer,10001);
int k = 0 ;
byte[] bn = new byte[1];
for(long j = 0 ; j<size/10000 ; j++){
try {
sum+=10000;
socket.receive(pk);
Integer num = new Integer(buffer[10000]);
if(num==k){
fos.write(buffer, 0, 10000);
fos.flush();
if(j!=size/10000-1){
k++;
k%=100;
bn[0] = Integer.valueOf(k).byteValue();
System.out.println(bn[0]);
respons.setData(bn);
socket.send(respons);
}
else{
if(size%10000==0){
bn[0] = Integer.valueOf(-1).byteValue();
System.out.println(bn[0]);
respons.setData(bn);
socket.send(respons);
}
}
}
else{
bn[0] = Integer.valueOf(k).byteValue();
System.out.println(bn[0]);
respons.setData(bn);
socket.send(respons);
j--;
}
} catch (IOException e) {
e.printStackTrace();
}
}
if(size%10000!=0){
if(size>10000){
k++;
k%=100;
bn[0] = Integer.valueOf(k).byteValue();
System.out.println(bn[0]);
respons.setData(bn);
try {
socket.send(respons);
} catch (IOException e1) {
e1.printStackTrace();
}
}
try {
socket.receive(pk);
bn[0] = Integer.valueOf(-1).byteValue();
respons.setData(bn);
socket.send(respons);
} catch (IOException e) {
e.printStackTrace();
}
try {
fos.write(buffer, 0, (int)(size%10000));
fos.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
else{
System.out.println("文件传输失败");
socket.close();
System.exit(0);
}
}
}
}
else{
return;
}
}
}
package fileTransport;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
public class SentFile {
/**
* @param args
*/
private InetAddress ip = null;
private int port = 0;
private DatagramSocket socket ;
private InetAddress toIp = null;
private int toPort = 0;
public static void main(String[] args) throws UnknownHostException {
SentFile s = new SentFile(InetAddress.getLocalHost(),11111);
File file = new File("g:\\download");
s.sentFile(file);
}
public SentFile(InetAddress ip ,int port){
this.ip = ip ;
this.port = port;
try {
socket = new DatagramSocket(port , ip);
} catch (SocketException e) {
e.printStackTrace();
}
}
public int sentFile(final File file){
Thread th = new Thread(){
public void run(){
byte[] b = new byte[1000];
DatagramPacket p = new DatagramPacket(b,b.length);
try {
socket.receive(p);
byte[] date = p.getData();
System.out.println(new String(date));
} catch (IOException e) {
e.printStackTrace();
}
toIp = p.getAddress();
toPort = p.getPort();
send(file,"");
byte[] over = "over".getBytes();
DatagramPacket p1 = new DatagramPacket(over,over.length);
p1.setAddress(toIp);
p1.setPort(toPort);
try {
socket.send(p1);
} catch (IOException e) {
e.printStackTrace();
}
}
};
th.start();
return 1;
}
private void send(File file,String name){
String name1 = name+"\\"+file.getName();
if(file.isDirectory()){
String s = "path"+name1;
byte[] b = s.getBytes();
DatagramPacket p = new DatagramPacket(b,b.length);
p.setAddress(toIp);
p.setPort(toPort);
try {
socket.send(p);
} catch (IOException e) {
e.printStackTrace();
}
try {
System.out.println("recieve");
socket.receive(p);
System.out.println("recieved");
} catch (IOException e) {
e.printStackTrace();
}
for(int i = 0 ; i<file.listFiles().length; i++){
send(file.listFiles()[i],name1);
}
}
else{
String s = "file"+name1+"length"+file.length();
byte[] b = s.getBytes();
DatagramPacket p = new DatagramPacket(b,b.length);
p.setAddress(toIp);
p.setPort(toPort);
try {
socket.send(p);
} catch (IOException e) {
e.printStackTrace();
}
try {
socket.receive(p);
} catch (IOException e) {
e.printStackTrace();
}
byte[] buffer = new byte[10000];
byte[] sendbuffer = new byte[10001];
DatagramPacket pk = new DatagramPacket(sendbuffer,10001);
pk.setAddress(toIp);
pk.setPort(toPort);
long sum = 0;
FileInputStream fis = null;
BufferedInputStream bis = null;
try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
bis.mark(10000);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
int read = 0;
int num= 0;
try {
socket.setSoTimeout(3000);
boolean re = false;
while(-1!=(read=bis.read(buffer))){
sum+=read;
System.arraycopy(buffer, 0, sendbuffer, 0, read);
sendbuffer[10000] = Integer.valueOf(num).byteValue();
pk.setData(sendbuffer);
socket.send(pk);
System.out.println(sum);
while(!re){
try{
socket.receive(p);
re = true;
}catch(SocketTimeoutException timeout){
socket.send(pk);
}
}
re = false;
Integer bn = new Integer(p.getData()[0]);
System.out.println(bn+","+num+","+read);
if(bn==-1){
break;
}
if(bn!=(num+1)%100){
bis.reset();
}
else{
bis.mark(10000);
num++;
num%=100;
}
}
} catch (IOException e) {
e.printStackTrace();
}
try {
bis.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}