Introduction to "Computer network" about three kinds of network communication modes and the implementation of three kinds of communication methods in Java

Source: Internet
Author: User
Tags switches

There are three modes of communication in the current network: unicast, broadcast, multicast (multicast), which has the advantage of multicast and broadcast at the latest, and has the most development foreground.


One, unicast:


The "one-on" communication mode between hosts, switches and routers in the network are forwarded to the data only and not replicated. If 10 clients require the same data, the server needs to be routed one at a time, repeating the same work 10 times. However, due to its ability to respond to each customer in a timely manner, the current web browsing is all based on IP unicast protocol. Routers and switches in the network select the transmission path based on their destination address, transferring the IP unicast data to its specified destination.
Advantages of unicast:


1. The server responds to client requests in a timely manner


2. The server sends out data for each customer's request, which makes it easy to personalize the service.


The disadvantages of unicast:


1. The server sends data streams for each client, server traffic = number of clients x client traffic, and servers are overwhelmed in streaming media applications where the number of clients is large and each client is heavily traffic.


2. The existing network bandwidth is a pyramid structure, and the inter-city inter-provincial backbone bandwidth is only 5% of the sum of all of its user bandwidth. If you use all unicast protocols, you will be overwhelmed by the network backbone. Current-to-peer applications have often blocked the trunk, and as long as 5% of customers are using the network at full speed, others will not play. It is almost impossible to expand the backbone by 20 times times.


Second, broadcast:


The "one-to-all" communication mode between the hosts, the network sends the signal to each of the hosts unconditionally and forwards, all the hosts can receive all the information (whether you need it or not), because it does not have the path choice, so its network cost can be very low. The cable network is a typical broadcast network, and our TV is actually receiving signals from all channels, but only one channel is restored to the screen. Broadcasting is also allowed in the data network, but it is restricted to the two-layer switch's LAN range, which prohibits broadcast data from passing through the router, preventing broadcast data from affecting large areas of the host.
Advantages of broadcasting:


1. The network equipment is simple, the maintenance is simple, the cloth net cost is low


2. Server traffic is extremely low because the server does not send data separately to each client.


Disadvantages of broadcasting:


1. It is not possible to provide personalized services in a timely manner for each customer's requirements and time.


2. The network allows the server to provide data with limited bandwidth, the maximum bandwidth of the client = Total service bandwidth. For example, the cable client's line support 100 channels (if using digital compression technology, theoretically can provide 500 channels), even if the service provider has more financial resources to allocate more transmission equipment, to the fiber backbone, can not exceed this limit. In other words, it is impossible to provide more diversified and more personalized services to many customers.


3. Broadcasts are prohibited from transmitting on internet broadband Internet.


Third, multicast:


A "pair of" communication patterns between hosts, that is, a host that joins the same group can accept all the data in the group, and the switches and routers in the network only replicate and forward their required data to the person in need. The host can request to the router to join or exit a group, and routers and switches in the network have the option of replicating and transmitting data, i.e., data is transferred to only those hosts that are in the group. This allows the data to be transferred to multiple hosts that are required (to join the group) at once, and to ensure that other communications from other hosts that do not need to join the group are not affected.
Advantages of Multicast:


1. Clients that require the same data stream join the same group to share a single data stream, saving the server's load. Have the advantages of broadcasting.


2. Because the multicast protocol is to replicate and forward the data stream according to the needs of the recipient, the total service bandwidth of the server is not limited by the bandwidth of the customer's access port. The IP protocol allows for 2亿6千多万个 (268435456) multicast, so the services it provides can be very rich.


3. This protocol, like the unicast protocol, allows transmission over internet broadband Internet.


The disadvantages of multicast:


1. Compared with the unicast protocol, there is no error correction mechanism, which is difficult to compensate after packet loss, but it can be compensated by some fault-tolerant mechanism and QoS.


2. Although the current network support multicast transmission, but in customer authentication, QoS and other aspects need to be perfected, these shortcomings in theory have mature solutions, just need to gradually spread to the existing network.


Realize:

1. Unicast (ServerSocket)

(1) Service side

public class TCPServer {ServerSocket serversocket; Socket socket;inetaddress address;public void init () {try {address=inetaddress.getbyname ("128.0.0.0"); ServerSocket = New ServerSocket (5656,10,address); while (true) {System.out.println ("waiting ..."); socket = Serversocket.accept (); new MyThread (socket). Start (); System.out.println ("one Start");}} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}} Class MyThread extends Thread {socket socket;public MyThread (socket socket) {//TODO auto-generated constructor STUBTHIS.S Ocket = socket;} public void Run () {//TODO auto-generated method Stubwhile (True) {try {outputstream outputstream = Socket.getoutputstream (); Outputstream.write (("Rou wo wo" + thread.currentthread (). GetName ()). GetBytes ()); try {sleep (2000);} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}} /** * @param args */public static void main (String[] (args) {//TODO auto-generated method Stubnew tcpserver (). init ();}} 

(2) Client

public class TcpClient {socket socket;public void init () {try {socket = new socket ("192.0.0.1", 5656) and while (true) {InputS  Tream InputStream = Socket.getinputstream (); int i = 0;byte bs[] = new Byte[1024];while ((i = Inputstream.read (BS)) > 0) {System.out.println (New String (BS));}}} catch (Unknownhostexception e) {//Todo auto-generated catch Blocke.printstacktrace ()} catch (IOException e) {//Todo Aut O-generated catch Blocke.printstacktrace ();}} /** * @param args */public static void main (string[] args) {//TODO auto-generated method Stubnew TcpClient (). init ();}}

2. Unicast (Datagramsocket)

(1) End 1

public class Udpserver {datagramsocket datagramsocket;int port = 7070;datagrampacket outpacket;byte[] bs = new byte[1024]; String IP = "172.22.67.6"; int i = 0;/** * @param args */public static void main (string[] args) {//TODO auto-generated met Hod stubnew udpserver (). Init (); public void Init () {try {datagramsocket = new Datagramsocket (8002); try {outpacket = new Datagrampacket (BS, Bs.length,ineta Ddress.getbyname ("127.0.0.1"), 8003); while (true) {Outpacket.setdata ("Jia you Nuan wo zi" + (++i)). GetBytes ()); Datagramsocket.send (Outpacket); SYSTEM.OUT.PRINTLN ("Send ~" + i); Thread.Sleep (2000);}} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}} catch (SocketException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}

(2) End 2:

public class UdpClient {datagramsocket datagramsocket;int port = 7070;datagrampacket inpacket;byte[] bs = new Byte[1024];s  Tring IP = "172.22.67.6";/** * @param args */public static void main (string[] args) {//TODO auto-generated method stubnew UdpClient (). init ();} public void Init () {try {datagramsocket =new datagramsocket (8003,                      inetaddress.getbyname ("127.0.0.1"));  } catch (Exception E1) {//TODO auto-generated catch Blocke1.printstacktrace ();} try {inpacket = new Datagrampacket (BS, bs.length); while (true) {datagramsocket.receive (inpacket); SYSTEM.OUT.PRINTLN ("UDP receive" + New String (BS));}} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}

3, broadcast (no client and service side of the points)

(1) End 1:

public class Muludpclient {String IP = "230.0.0.0"; MulticastSocket datagramsocket;int port = 5050;datagrampacket inpacket;byte[] Receive = new byte[1025];inetaddress address;/** * @param args */public static void main (string[] args) {//TODO auto-generated method Stubnew muludpclient (). I NIT ();} public void Init () {try {datagramsocket = new MulticastSocket (port); address = Inetaddress.getbyname (IP); Datagramsocket.joingroup (address);} catch (Exception E1) {//TODO auto-generated catch Blocke1.printstacktrace ();} Inpacket = new Datagrampacket (Receive, receive.length); while (true) {try {System.out.println ("Wait for server"); Datagramsocket.receive (Inpacket); SYSTEM.OUT.PRINTLN ("Receive" + New String (receive));} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}

(2) End 2:

public class Muludpserver {MulticastSocket multicastsocket; String IP = "230.0.0.0"; int port = 5050;inetaddress Socketaddress;datagrampacket Datagrampacket;int i = 0;/** * @param arg s */public static void Main (string[] args) {//TODO auto-generated method Stubnew muludpserver (). Initserver (); public void Initserver () {try {multicastsocket = new MulticastSocket (port); socketaddress = Inetaddress.getbyname (IP); Multicastsocket.joingroup (socketaddress); Multicastsocket.setloopbackmode (false);d Atagrampacket = new Datagrampacket (new Byte[0], 0, Socketaddress,port); new Thread () {public void run () {//TODO auto-generated method Stubwhil E (true) {try {try {datagrampacket.setdata ("Wo ai ni Dudu" + (++i)). GetBytes ()); Multicastsocket.send (Datagrampacket); System.out.println ("Send success!" + i);} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();} Thread.Sleep (2000);} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}. Start ();} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}} 










Introduction to "Computer network" about three kinds of network communication modes and the implementation of three kinds of communication methods in Java

Related Article

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.