Unicast of datagram programming using Java language

Source: Internet
Author: User

In fact, unicast is a message sent from the source host to another target host, with only one recipient.

First, write the transceiver class mongoramrecvsender_s. This class sends both the datagram and the datagram, sends all the images in a folder, one by one, and receives the data in one thread, each time a file is collected, an event is activated and the event processor processes the event.

Class list:

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 extends ramrecvsender_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 data packets.
*/
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 );
// Specify at least the datagram receiving port, which is the only requirement on the receiving end. Of course, if the host is a multi-access host, road = new DatagramSocket (sa) is required );
Road = new DatagramSocket (datagramrecvsender_s.w.ramport );
System. out. println (road. isBound () + ":" + road. getBroadcast ());
}
Catch (SocketException ex ){
Ex. printStackTrace ();
}
Catch (Exception ex1 ){
Ex1.printStackTrace ();
}
}
/**
* This constructor is used to send a datagram.
* @ 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 the datagram, You need to specify a port, otherwise you do not need 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 ();
// The data is self-contained and self-described. Its delivery is performed by the router. The router routing algorithm selects the appropriate route delivery packet based on the destination address of the packet,
// Specify the destination IP address and target host processing port in the datagram.
DatagramPacket dp = new DatagramPacket (B, 0, B. length, ia, datagramrecvsender_s.20.ramport );
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 ();
Required ramrecvsender_s dgrs = new required ramrecvsender_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]);
}
}
}

The following figure shows the acceptor interface.

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 ();
Required ramrecvsender_s dgrs = new required ramrecvsender_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! ");
}
}

List of DualWindow classes extended by testDual:

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;
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. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE );
Frame. setSize (300,200 );
Frame. validate ();
Frame. setVisible (true );
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.