Programming | data
In fact, unicast is a message sent from the source host to another target host, receiving only one target.
First of all, write transceiver Class datagramrecvsender_s, this class is not only send datagrams and receive datagrams, send a folder of all the pictures, a piece of the carry out, receive is in a thread, every file, the activation of an event, event handler to handle this event.
The Class list is:
Package recmail.multiservice;
Import Java.net.DatagramPacket;
Import Java.net.DatagramSocket;
Import java.net.SocketException;
Import java.net.SocketAddress;
Import java.net.UnknownHostException;
Import java.net.InetSocketAddress;
Import java.io.IOException;
Import java.util.ArrayList;
Import java.net.InetAddress;
Import Java.io.File;
Import Java.util.Iterator;
public class datagramrecvsender_s extends Dataswaplisteneradapter
Implements Runnable {
public static final int datagramport = 10698;
Private Java.net.DatagramSocket Road;
Private InetAddress IA;
Datagrampacket DP;
public static final int datagram_length = 8192;
Private Datapacket CDP;
Private Boolean ishandling = true;
Dataswapevent dsevent;
/**
* This constructor is used to receive datagrams.
*/
Public datagramrecvsender_s () {
try {
System.out.println ("recv ...");
ia = inetaddress.getbyname ("localhost");//192.168.0.17 "127.0.0.1"
Dsevent = new Dataswapevent (this);
SocketAddress sa = new Inetsocketaddress (IA,
Datagramrecvsender_s.datagramport);
At a minimum, you specify the datagram receive port, which is the only requirement on the receiving end. Of course, if the host is a multiple access host, you need to road = new Datagramsocket (SA);
Road = new Datagramsocket (datagramrecvsender_s.datagramport);
System.out.println (Road.isbound () + "::" +road.getbroadcast ());
}
catch (SocketException ex) {
Ex.printstacktrace ();
}
catch (Exception Ex1) {
Ex1.printstacktrace ();
}
}
/**
* This constructor is used to send datagrams.
* @param send String
*/
Public datagramrecvsender_s (String send) {
try {
System.out.println ("Start sending ...");
ia = Inetaddress.getbyname ("192.168.0.4");//"255.255.255.255" "192.168.0.4" "127.0.0.1"
Dsevent = new Dataswapevent (this);
SocketAddress sa = new Inetsocketaddress (IA,
Datagramrecvsender_s.datagramport);
If you need to receive datagrams, you need to specify a port, otherwise you do not have to specify. Road = new Datagramsocket (datagramrecvsender_s.datagramport-1);
Road = new Datagramsocket ();
System.out.println ("isbound--" +road.isbound () + ": getbroadcast--" +road.getbroadcast ());
}
catch (SocketException ex) {
Ex.printstacktrace ();
}
catch (Unknownhostexception Ex1) {
Ex1.printstacktrace ();
}
}
public void SendFile (String file) {
CDP = new Datapacket (file);
System.out.println (file);
Iterator it = cdp.getdatapackets (). iterator ();
Byte[] B;
while (It.hasnext ()) {
B = ((DataEntry) It.next ()). GetByte ();
Datagram is self-contained, self-describing data, its delivery is carried out by the router, router routing algorithm based on the destination address of the message to select the appropriate route delivery message,
Therefore, the destination IP address and target host processing port are specified in the datagram.
Datagrampacket DP = new Datagrampacket (b, 0, b.length, ia,datagramrecvsender_s.datagramport);
try {
Thread.Sleep (100);
if (dp==null)
Continue
Road.send (DP);
}
catch (Exception ex) {
Ex.printstacktrace ();
}
}
}
public Boolean ishandling () {
return ishandling;
}
public void Ishandling (Boolean ishand) {
ishandling = Ishand;
}
public void Run () {
byte[] buffer = new Byte[datapacket.dataswapsize];
Datagrampacket packet = new Datagrampacket (buffer, buffer.length);
Datapacket DP = new Datapacket ();
while (ishandling) {
Packet.setlength (buffer.length);
System.out.println ("wait;");
try {
Road.receive (packet);
Dp. ADD (Packet.getdata ());
if (Dp.isfull ()) {
Dsevent.setimage (DP. Gereratedata ());
This.processrecvfinishedevent (dsevent);
DP = new Datapacket ();
}
}
catch (IOException ex) {
System.out.println (ex);
}
}
}
public static void Main (string[] args) {
String file[];
ArrayList al = new ArrayList ();
String Path = "e:\\nature\\";
File F = new file (path);
File = F.list ();
datagramrecvsender_s Dgrs = new datagramrecvsender_s ("");
for (int j=0;j<1000;j++)
for (int i = 0; i < file.length; i++) {
try {
Thread.Sleep (2000);
}
catch (Interruptedexception ex) {
}
if (File[i].endswith ("jpg") | | file[i].endswith ("BMP")
Dgrs.sendfile (path + file[i]);
}
}
}
Look at the interface of the receiving end below.
Package recmail.multiservice;
Import Java.awt.BorderLayout;
public class Testdual
Extends Dualwindow implements Dataswaplistener {
Jimagepanel IP;
Java.awt.Image bi;
Public testdual () {
}
public void init () {
Super.init ();
Imageshow is = new Imageshow ();
Is.adddataswaplistener (this);
Thread thread = new Thread (IS, "test");
Thread.Start ();
}
public static void Main (string[] args) {
Testdual test = new testdual ();
Test.initmain ();
Test.go ();
datagramrecvsender_s Dgrs = new datagramrecvsender_s ();
Dgrs.adddataswaplistener (test);
Thread thread = new Thread (DGRS, "test");
Thread.Start ();
}
public void Initmain () {
Super.initmain ();
ip = new Jimagepanel ();
Root.setlayout (New BorderLayout (5, 5));
Root.add (IP, borderlayout.center);
}
public void ondatasendfinished (Object s, dataswapevent e) {
}
public void ondatarecvfinished (Object s, dataswapevent e) {
This.bi = E.getimage ();
Ip.setimage (BI);
System.out.println ("recv finished!");
}
}
Testdual extended List of Dualwindow classes:
Package recmail.multiservice;
Import Javax.swing.JPanel;
Import Javax.swing.JFrame;
Import Javax.swing.JApplet;
public class Dualwindow
Extends JApplet {
protected JPanel root;
Private JFrame frame;
Public Dualwindow () {
}
public void init () {
root = new JPanel ();
This.setcontentpane (root);
}
public void Initmain () {
root = new JPanel ();
}
public static void Main (string[] args) {
Testdual test = new testdual ();
Test.initmain ();
Test.start ();
Test.go ();
}
public void Go () {
frame = new JFrame ();
Frame.setcontentpane (root);
Frame.setdefaultcloseoperation (Jframe.exit_on_close);
Frame.setsize (300, 200);
Frame.validate ();
Frame.setvisible (TRUE);
}
}
The next article introduces the implementation of the broadcast.