1. Trang chủ
  2. » Giáo Dục - Đào Tạo

APJI lab6

3 48 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

APJI-Lab6-Networking Part2 Application Programming I Module – Networking Part2 Lab Guide for Session Session Objectives In this session, you will be practicing with  DatagramSocket, DatagramPacket class to send and receive data under User Datagram Protocol Part I: Getting Started – 30 minutes Following code use DatagramSocket, DatagramPacket and MulticastSocket class to create an application that simulate a radio station and a radio that receive the news Scan the code first, type the code, compile, run and observe the result package demo; public class Radio { public static void main(String[] args) throws IOException, InterruptedException { // TODO code application logic here try { int port = 10000; InetAddress address = InetAddress.getByName("230.0.0.1"); MulticastSocket ms = new MulticastSocket(port); ms.joinGroup(address); DatagramPacket packet; while (true) { byte[] buffer = new byte[1000]; packet = new DatagramPacket(buffer, buffer.length, address, port); ms.receive(packet); String line = new String(buffer, 0, packet.getLength()); System.out.println(line); } } catch (Exception ex) { Logger.getLogger(Radio.class.getName()).log(Level.SEVERE, null, ex); } } } package demo; © 2009 FPT-Aptech Page / APJI-Lab6-Networking Part2 public class RadioStation { public static void main(String[] args) throws SocketException, IOException { // TODO code application logic here try { InetAddress address = InetAddress.getByName("230.0.0.1"); DatagramSocket socket = new DatagramSocket(4445); int port = 10000; Scanner input = new Scanner(System.in); int i=0; while (true) { System.out.print(i+++":"); String line = input.nextLine(); byte[] buffer = line.getBytes(); DatagramPacket data = new DatagramPacket(buffer, buffer.length, address, port); socket.send(data); } } catch (Exception ex) { Logger.getLogger(RadioStation.class.getName()).log(Level.SEVERE, null, ex); } } } Question: Why we use MulticastSocket class in this example? Part II: Workshops – 30 minutes • • Quickly look at Module 7’s workshops for reviewing basic steps for creating and using DatagramSocket, DatagramPacket class Try to compile, run and observe the output of sample code provided for related workshop Discuss with your class-mate and your instructor if needed Part III: Lab Assignment– 60 minutes Do the assignment for Module carefully Discuss with your class-mates and your instructor if needed See DCJ_Module_7_Assignment.pdf file Part IV: Homework Exercise 1: Create a simple live music podcast service using datagram socket? © 2009 FPT-Aptech Page / APJI-Lab6-Networking Part2 © 2009 FPT-Aptech Page /

Ngày đăng: 27/10/2019, 09:29

Xem thêm:

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

w