java UDP 一对一文件传输

原创
2012/11/15 21:02
阅读数 1.6K

学习了基于java 的网络编程,关于udp 的网络编程,这方面的东西前几天难住了哥,现在解决了,做个笔记。同时分享一下给以后的同学。

 UDP是并不是可靠的传输方式,字节序列并没有得到校验,所以传输文件时出现错误也是难免的.因为,txt,doc和jpg本身即使出错也不会影响打开,顶多出现个别字符或是个别像素错误不会影响整体,而exe则是cpu的指令执行序列你错一个就将会导致程序无法执行。

对于文件传输还是建议使用tcp,点对点的传输。

下面是我的代码,希望对各位用。

客服端:

import java.io.FileInputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/**
 * udp 文件传输,客户端 
 * @author jun
 * @date: 2012-11-15
 */
public class Client {

	public static void main(String[] args) {
		
		FileInputStream fis = null;					//file
		DatagramSocket sendSocket= null;		//socket
		DatagramPacket sendPacket =null;		//send packet
		DatagramPacket getPacket = null;		//get packet
		InetAddress ip = null;							//ip
		
		try {
			sendSocket = new DatagramSocket();
			int port = 8888;
			ip = InetAddress.getLocalHost();
			fis = new FileInputStream("E:\\test.mp3");
			byte[] buf =new byte[1024];
			int i;
			
			while((i = fis.read(buf, 0, 1024) )==1024){
				System.out.println(i);
				sendPacket = new DatagramPacket(buf, buf.length, ip,  port);
				sendSocket.send(sendPacket);
				Thread.sleep(1);	//简单的防止丢包现象
			}
			System.out.println(i);
			sendPacket = new DatagramPacket(buf, 0,i, ip,  port);
			sendSocket.send(sendPacket);
			Thread.sleep(1);		//简单的防止丢包现象
			
			System.out.println("client send done.");
			
			byte[] getBuf = new byte[1024];
			getPacket = new DatagramPacket(getBuf, getBuf.length);
			sendSocket.receive(getPacket);
			//get feedback message
			String backMes = new String(getBuf, 0, getPacket.getLength());
			System.out.println("feefBack:"+backMes);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				if(fis !=null){
					fis.close();
				}
				if(sendSocket != null){
					sendSocket.close();
				}
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}

}
服务端:

import java.io.FileOutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketAddress;

/**
 * udp 文件传输,服务端
 * @author jun
 * @date: 2012-11-15
 */
public class Server {

	public static void main(String[] args) {
		DatagramSocket serverSocket = null;					//socket 
		InetAddress ip = null;											//ip
		DatagramPacket getPacket = null; 						//get packet
		DatagramPacket sendPacket = null;						//send packet
		FileOutputStream fos = null;								//file
		int port  = 8888;												//port
		//InetAddress sendIP= null;								//send ip
		//int sendPort = -1;											//send port
		try {
			ip = InetAddress.getLocalHost();
			serverSocket = new DatagramSocket(port ,  ip);
			fos = new FileOutputStream("D:\\test.mp3");
			byte[] buf = new byte[1024];
			int i=0;
			while(true){
				getPacket = new DatagramPacket(buf,0,buf.length); 
				serverSocket.receive(getPacket);
				i = getPacket.getLength();
				fos.write(buf,0,i);
				if(i<1024){
					break;
				}
			}
			
			//System.out.println("server saved!");
			//sendIP = getPacket.getAddress(); 
			//sendPort = getPacket.getPort();
			SocketAddress sendAddress = getPacket.getSocketAddress(); 
			String feedback = "received successfully!";
			byte[] backBuf = feedback.getBytes(); 
			sendPacket = new DatagramPacket(backBuf,  backBuf.length, sendAddress);
			serverSocket.send(sendPacket);
			//System.out.println(feedback);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				serverSocket.close();
				fos.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}

}


展开阅读全文
打赏
0
2 收藏
分享
加载中
更多评论
打赏
0 评论
2 收藏
0
分享
返回顶部
顶部