I mentioned in the previous article how to do it when I receive a broadcast package.
Because you do not know when to send broadcast packets
So we also need to use a thread to constantly listen to broadcast ports.
After receiving a request packet, send your information to the sender.
Now let's talk about how users send broadcast packets.
I have defined a class for sending messages (sendmes)
This class has two constructor Methods
The default constructor sends a broadcast package.
Parameter construction method sendmes (byte [] IP, byte data [])
IP indicates the recipient's IP address, and data indicates the data to be sent.
The source code is as follows:
// Used to send Short Messages
// Port 9001 is the information sending Port
// Port 9000 is the information receiving port
// Port 8888 is the broadcast sending Port
// Port 8889 is the broadcast receiving port
// The default constructor is to send a broadcast package.
Import java.net .*;
Import java. Io .*;
Class sendmes {
Datagramsocket trans;
Datagrampacket udpmess;
Inetaddress adsid;
Byte [] IP = new byte [4]; // store the IP address of the receiver
Byte [] DATA = new byte [300]; // stores the information sent by the user. The maximum length of the information is 300 bytes.
Byte [] data1 = {, 1}; // The information in the sent broadcast package is "1"
Sendmes (){
Try {
Trans = new initramsocket (8880 );
} Catch (socketexception e ){
System. Out. println ("unable to open port ");
New newmess ("unable to open the port ");
}
Try {
Adsid = inetaddress. getlocalhost ();
IP = convert (adsid. gethostaddress (); // obtain the IP address of the local area network to form a broadcast address.
Switch (IP [0]) // adaptive process
{
Case 10: IP [1] = (byte) 255;
IP [2] = (byte) 255;
IP [3] = (byte) 255;
Break;
Case (byte) 172: IP [2] = (byte) 255;
IP [3] = (byte) 255;
Break;
Case (byte) 192: IP [2] = (byte) 255;
IP [3] = (byte) 255;
Break;
}
Adsid = inetaddress. getbyaddress (IP );
} Catch (unknownhostexception e ){
System. Out. println ("the target host cannot be found ");
New newmess ("the target host cannot be found ");
}
Udpmess = new datagrampacket (data1, 5, addh, 8889 );
Try {
Trans. Send (udpmess); // send a message
} Catch (ioexception e ){
System. Out. println ("data transmission error ");
System. Out. println (E. getmessage ());
}
Trans. Close ();
}
// The parameter IP address is the IP address of the receiver, and data is the sent data.
// Go out from port 9001 of the Local Machine and send it to port 9000 of the target party
Sendmes (byte [] IP, byte data []) {
This. IP = IP;
This. Data = data;
Try {
Trans = new initramsocket (9001 );
} Catch (socketexception e ){
System. Out. println ("unable to open port ");
}
Try {
Adsid = inetaddress. getbyaddress (IP );
} Catch (unknownhostexception e ){
System. Out. println ("the target host cannot be found ");
}
Udpmess = new datagrampacket (data, data. length, addh, 9000 );
Try {
Trans. Send (udpmess );
} Catch (ioexception e ){
System. Out. println ("data transmission error ");
}
Trans. Close ();
}
Byte [] convert (string temp) {// convert the IP address to the byte [] Array
Byte [] B = new byte [4];
Int I, J, K;
For (I = 0, j = 0, K = 0; I <temp. Length (); I ++ ){
If (temp. charat (I) = '.'){
B [k ++] = (byte) integer. parseint (temp. substring (J, I ));
J = I + 1;
}
If (k = 3) break;
}
B [k] = (byte) integer. parseint (temp. substring (J, temp. Length ()));
Return B;
}
}
Next post: Introduction
Based on the feedback received after the broadcast package
Form a graphical interface