2.现行网络虽然都支持组播的传输,但在客户认证、QOS等方面还需要完善,这些缺点在理论上都有成熟的解决方案,只是需要逐步推广应用到现存网络当中。
实现:
1、单播(ServerSocket)
(1)服务端
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 block e.printStackTrace(); } } class MyThread extends Thread { Socket socket; public MyThread(Socket socket) { // TODO Auto-generated constructor stub this.socket = socket; } public void run() { // TODO Auto-generated method stub while (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 block e.printStackTrace(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub new TcpServer().init(); } }
public class TcpClient { Socket socket; public void init() { try { socket = new Socket("192.0.0.1", 5656); while (true) { InputStream 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 block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub new TcpClient().init(); } }
(1)端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 method stub new UdpServer().init(); } public void init() { try { datagramSocket = new DatagramSocket(8002); try { outPacket = new DatagramPacket(bs, bs.length, InetAddress.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 block e.printStackTrace(); } } catch (SocketException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
public class UdpClient { DatagramSocket datagramSocket; int port = 7070; DatagramPacket inPacket; byte[] bs = new byte[1024]; String ip = "172.22.67.6"; /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub new UdpClient().init(); } public void init() { try { datagramSocket =new DatagramSocket(8003, InetAddress.getByName("127.0.0.1")); } catch (Exception e1) { // TODO Auto-generated catch block e1.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 block e.printStackTrace(); } } }
(1)端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 stub new MulUdpClient().init(); } public void init() { try { datagramSocket = new MulticastSocket(port); address = InetAddress.getByName(ip); datagramSocket.joinGroup(address); } catch (Exception e1) { // TODO Auto-generated catch block e1.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 block e.printStackTrace(); } } } }
public class MulUdpServer { MulticastSocket multicastSocket; String ip = "230.0.0.0"; int port = 5050; InetAddress socketAddress; DatagramPacket datagramPacket; int i = 0; /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub new MulUdpServer().initServer(); } public void initServer() { try { multicastSocket = new MulticastSocket(port); socketAddress = InetAddress.getByName(ip); multicastSocket.joinGroup(socketAddress); multicastSocket.setLoopbackMode(false); datagramPacket = new DatagramPacket(new byte[0], 0, socketAddress, port); new Thread() { public void run() { // TODO Auto-generated method stub while (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 block e.printStackTrace(); } Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }.start(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
【计算机网络】关于三种网络通信模式以及java中三种通信方式的实现介绍
原文地址:http://blog.csdn.net/scboyhj__/article/details/42460331