實際上
首先
類清單為:
package recmail
import
import
import
import
import
import
import java
import java
import
import java
import java
public class DatagramRecvSender_s extends DataSwapListenerAdapter
implements Runnable {
public static final int DatagramPort =
private
private InetAddress ia;
DatagramPacket dp;
public static final int Datagram_Length =
private DataPacket cdp;
private boolean ishandling = true;
DataSwapEvent dsevent;
/**
* 這個構造器用於接收數據報
*/
public DatagramRecvSender_s() {
try {
System
ia = InetAddress
dsevent = new DataSwapEvent(this);
SocketAddress sa = new InetSocketAddress(ia
DatagramRecvSender_s
//至少要指定數據報接收端口
road = new DatagramSocket(DatagramRecvSender_s
System
}
catch (SocketException ex) {
ex
}
catch (Exception ex
ex
}
}
/**
* 這個構造器用於發送數據報
* @param send String
*/
public DatagramRecvSender_s(String send) {
try {
System
ia = InetAddress
dsevent = new DataSwapEvent(this);
SocketAddress sa = new InetSocketAddress(ia
DatagramRecvSender_s
//如果需要接收數據報
road = new DatagramSocket();
System
}
catch (SocketException ex) {
ex
}
catch (UnknownHostException ex
ex
}
}
public void sendFile(String file) {
cdp = new DataPacket(file);
System
Iterator it = cdp
byte[] b;
while (it
b = ( (DataEntry) it
//數據報是自包含的
//所以數據報中要指定目標IP地址和目標主機處理端口
DatagramPacket dp = new DatagramPacket(b
try {
Thread
if(dp==null)
continue;
road
}
catch (Exception ex) {
ex
}
}
}
public boolean isHandling() {
return ishandling;
}
public void isHandling(boolean ishand) {
ishandling = ishand;
}
public void run() {
byte[] buffer = new byte[DataPacket
DatagramPacket packet = new DatagramPacket(buffer
DataPacket dp = new DataPacket();
while (ishandling) {
packet
System
try {
road
dp
if (dp
dsevent
this
dp = new DataPacket();
}
}
catch (IOException ex) {
System
}
}
}
public static void main(String[] args) {
String file[];
ArrayList al = new ArrayList();
String path =
File f = new File(path);
file = f
DatagramRecvSender_s dgrs = new DatagramRecvSender_s(
for(int j=
for (int i =
try {
Thread
}
catch (InterruptedException ex) {
}
if (file[i]
dgrs
}
}
}
下面看接收端的界面
package recmail
import java
public class testDual
extends DualWindow implements DataSwapListener {
JImagePanel ip;
java
public testDual() {}
public void init() {
super
ImageShow is = new ImageShow();
is
Thread thread = new Thread(is
thread
}
public static void main(String[] args) {
testDual test = new testDual();
test
test
DatagramRecvSender_s dgrs = new DatagramRecvSender_s();
dgrs
Thread thread = new Thread(dgrs
thread
}
public void initmain() {
super
ip = new JImagePanel();
root
root
}
public void OnDataSendFinished(Object s
public void OnDataRecvFinished(Object s
this
ip
System
}
}
testDual擴展的DualWindow類清單:
package recmail
import javax
import javax
import javax
public class DualWindow
extends JApplet {
protected JPanel root;
private JFrame frame;
public DualWindow() {}
public void init() {
root = new JPanel();
this
}
public void initmain() {
root = new JPanel();
}
public static void main(String[] args) {
testDual test = new testDual();
test
test
test
}
public void go() {
frame = new JFrame();
frame
frame
frame
frame
frame
}
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/25587.html