public class jieshou { /**
* @param args
* @throws SocketException
* @throws IOException
*/
public static void main(String[] args) throws SocketException, Exception {
// TODO Auto-generated method stub
DatagramSocket socket;
socket = new DatagramSocket(10000);
byte[] buf = new byte[108421];
OutputStream fos = new BufferedOutputStream(new FileOutputStream("C:\\Users\\Administrator\\Desktop\\jieshou.jpg"));
while(true)
{
DatagramPacket packet = new DatagramPacket(buf,108421);
try{
socket.receive(packet);
fos.write(packet.getData(), 0, packet.getLength());
}
catch (Exception e1)
{
try{
socket.close();
fos.flush();
fos.close();
}catch(Exception iee){System.out.println("close :"+e1);}
}
break;
}
System.out.println("server_finish");
}
}
* @param args
* @throws SocketException
* @throws IOException
*/
public static void main(String[] args) throws SocketException, Exception {
// TODO Auto-generated method stub
DatagramSocket socket;
socket = new DatagramSocket(10000);
byte[] buf = new byte[108421];
OutputStream fos = new BufferedOutputStream(new FileOutputStream("C:\\Users\\Administrator\\Desktop\\jieshou.jpg"));
while(true)
{
DatagramPacket packet = new DatagramPacket(buf,108421);
try{
socket.receive(packet);
fos.write(packet.getData(), 0, packet.getLength());
}
catch (Exception e1)
{
try{
socket.close();
fos.flush();
fos.close();
}catch(Exception iee){System.out.println("close :"+e1);}
}
break;
}
System.out.println("server_finish");
}
}
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;public class Test {
public static void main(String[] args) throws SocketException, Exception {
// TODO Auto-generated method stub
DatagramSocket socket;
socket = new DatagramSocket(10000);
byte[] buf = new byte[108421];
OutputStream fos = new BufferedOutputStream(new FileOutputStream(
"f:/temp/jieshou.jpg"));
while (true) {
DatagramPacket packet = new DatagramPacket(buf, 108421); try {
socket.receive(packet);
fos.write(packet.getData(), 0, packet.getLength());
} catch (Exception e1) {
try {
socket.close();
fos.flush();
fos.close();
} catch (Exception iee) {
System.out.println("close :" + e1);
}
}
break; }
System.out.println("server_finish");
}
}package com.ssj.test;import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketException;public class Client {
public static void main(String[] args) throws Exception {
try {
DatagramSocket soc = new DatagramSocket();
InputStream is = new FileInputStream("F:/temp/wmm.jpg");
byte[] data = new byte[is.available()];
is.read(data);
DatagramPacket dp = new DatagramPacket(data, data.length,new InetSocketAddress("127.0.0.1",10000));
soc.send(dp);
soc.close();
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
程序没问题
shaosijun2004的方法可以传输小图片但传输200k的图片就会捕捉出下面的错误:
The message is larger than the maximum supported by the underlying transport: Datagram send failed
tcp/ip协议为64k 你一下传200K还不挂?
>64K要分包的嘛,我了个去