Implement unicast in LAN Using Java

Source: Internet
Author: User

I have written an article about java multicast. Today, let's take a look at how java implements unicast in the LAN.

Let's add two key points of network programming:

1. It is easier to look at the problem with the process as the object. A process is the smallest program unit, and a network process occupies a port when establishing a network connection.

2. What we call unicast and multicast is actually inter-process communication.

Therefore, the most important thing to achieve unicast is to specify the IP address and process port.

If it is a local test, the same IP address is inevitable, as long as the sending process and receiving process use different ports (Ensure that the destination port is consistent with the port of the receiving process during sending.). Otherwise, an exception occurs.

Two methods are available for unicast in the LAN: one is connectionless and the other is connection-oriented.

Let's first look at the connectionless method:

First, we inherit the initramsocket class and encapsulate methods for sending and receiving messages.

Import java.net. *; import java. io. *;/*** inherit the datagram socket class * method for sending and receiving messages * @ author michael **/public class my‑ramsocket extends DatagramSocket {static final int MAX_LEN = 100; mydomainramsocket () throws SocketException {super ();} mydomainramsocket (int port) throws SocketException {super (port);} public void sendMessage (InetAddress receiverHost, int receiverPort, String message) throws IOException {byte [] sendBuffer = message. getBytes (); DatagramPacket datasync = new DatagramPacket (sendBuffer, sendBuffer. length, receiverHost, receiverPort); this. send (dataphin);} public String receiveMessage () throws IOException {byte [] receiveBuffer = new byte [MAX_LEN]; DatagramPacket datasync = new DatagramPacket (receiveBuffer, MAX_LEN); this. receive (datasync); String message = new String (receiveBuffer); return message ;}}

Next let's look at the receiver:

Import java.net. *;/*** implement inter-process communication without connection * @ author michael **/public class extends er {public static void main (String [] args) {try {InetAddress receiverHost = InetAddress. getByName ("127.0.0.1"); int myPort = 1234; // the current process port int receiverPort = 5689; // The receiving process port String message = "Hi Sender "; mydomainramsocket mySocket = new mydomainramsocket (myPort); System. out. println (mySocket. receiveMessage (); mySocket. sendMessage (receiverHost, receiverPort, message); mySocket. close ();} catch (Exception ex) {System. out. println (ex );}}}

Sender:

Import java.net. *;/*** implement inter-process communication without connection * @ author michael **/public class Sender {public static void main (String [] args) {try {InetAddress receiverHost = InetAddress. getByName ("127.0.0.1"); int myPort = 5689; int receiverPort = 1234; String message = "Hello er Er"; mydomainramsocket mySocket = new mydomainramsocket (myPort); mySocket. sendMessage (receiverHost, receiverPort, message); System. out. println (mySocket. receiveMessage (); mySocket. close () ;}catch (Exception ex ){}}}

In the code, I use a local test, but I have tried online testing.

Next, let's look at the connection-oriented method:

The connection-oriented method is basically the same as the non-connection method, but there is an additional process of establishing and disconnecting.

Import java.net. *;/*** implement inter-process communication in connection-oriented mode * @ author michael **/public class extends er {public static void main (String [] args) {try {InetAddress senderHost = InetAddress. getByName ("127.0.0.1"); int senderPort = 1234; int myPort = 4568; String message = "Hi Sender"; mydomainramsocket mySocket = new mydomainramsocket (myPort ); // establish a connection with the other party mySocket. connect (senderHost, senderPort); System. out. println (mySocket. receiveMessage (); mySocket. sendMessage (senderHost, senderPort, message); // disconnect mySocket. disconnect (); mySocket. close ();} catch (Exception ex) {System. out. println ("An exception has occured:" + ex );}}}
Import java.net. *;/*** implement inter-process communication in connection-oriented mode * @ author michael **/public class Sender {public static void main (String [] args) {try {InetAddress receiverHost = InetAddress. getByName ("127.0.0.1"); int receiverPort = 4568; int myPort = 1234; String message = "Hello er Er"; mydomainramsocket mySocket = new mydomainramsocket (myPort); mySocket. connect (receiverHost, receiverPort); mySocket. sendMessage (receiverHost, receiverPort, message); System. out. println (mySocket. receiveMessage (); mySocket. disconnect (); mySocket. close ();} catch (Exception ex) {System. out. println (ex );}}}
The test is the same as the previous one.

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.