P. 1
BÀI TẬP LẬP TRÌNH MẠNG

BÀI TẬP LẬP TRÌNH MẠNG

|Views: 2|Likes:

More info:

Published by: Huỳnh Quang Hiếu on Nov 02, 2013
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOCX, PDF, TXT or read online from Scribd
See more
See less

11/02/2013

pdf

text

original

BÀI TẬP LẬP TRÌNH MẠNG – UDP

Sinh viên thực hiện: Nguyễn Xuân Phong Lớp: 09T1 Nhóm: 10A

IPAddress. class UDPTimeClient { public static void main(String args[]) throws Exception { .trim(). port).send(sendPacket). // Nhan dl tu client serverSocket. // Tao cac mang byte de chua dl gui va nhan System.getPort().getBytes(). // Lay ngay gio de gui nguoc lai client String request = new String(receivePacket. // Lay dia chi IP cua may client InetAddress IPAddress = receivePacket.equals("getDate")) sendData = new Date().out. byte[] receiveData = new byte[1024].println("Server is started").*. receiveData. else sendData = "Server not know what you want?".getAddress().net.BÀI 1: Server: package Bai1. import java. class UDPTimeServer { public static void main(String args[]) throws Exception { // Gan cong 9876 cho chuong trinh DatagramSocket serverSocket = new DatagramSocket(9876).getBytes(). sendData. import java.*. if (request.*.out.getData()).*.length). while (true) { // Tao goi rong de nhan du lieu tu client DatagramPacket receivePacket = new DatagramPacket(receiveData. // Gui dl lai cho client serverSocket. } } } Client: package Bai1.*. DatagramPacket sendPacket = new DatagramPacket(sendData.util.net. byte[] sendData = new byte[1024]. System.length. // Lay port cua chuong trinh client int port = receivePacket. import java. import java. import java.io.receive(receivePacket).io.println(request).toString().

clientSocket. import java.getByName("localhost").*.send(sendPacket).// lay dl tu packet // nhan duoc System.DatagramSocket clientSocket = new DatagramSocket(). .net. clientSocket.// gui dl cho server // tao datagram rong de nhan dl DatagramPacket receivePacket = new DatagramPacket(receiveData.// nhan dl tu server String str = new String(receivePacket.io. byte[] sendData = new byte[1024]. IPAddress.receive(receivePacket).length).*. // tao datagram co noi dung yeu cau loai dl de gui cho server DatagramPacket sendPacket = new DatagramPacket(sendData. 9876). clientSocket.getData()).// gan cong dong InetAddress IPAddress = InetAddress.*.util. sendData.length.println(str).out. receiveData. class UDPTimeServer { public static void main(String args[]) throws Exception { // Gan cong 9876 cho chuong trinh DatagramSocket serverSocket = new DatagramSocket(9876). byte[] receiveData = new byte[1024]. } } Kết quả: BÀI 2: Server: package bai2.getBytes(). import java. import java. sendData = "getDate".close().

*.trim(). // Gui dl lai cho client serverSocket. else sendData = "Server not know what you want?".getAddress().getPort(). while (true) { // Tao goi rong de nhan du lieu tu client DatagramPacket receivePacket = new DatagramPacket(receiveData. // Lay port cua chuong trinh client int port = receivePacket.sleep(1000).length.out. // tao datagram co noi dung yeu cau loai dl de gui cho server DatagramPacket sendPacket = new DatagramPacket(sendData.getBytes().getBytes(). byte[] receiveData = new byte[1024]. sendData. if (request. byte[] sendData = new byte[1024].// Tao cac mang byte de chua dl gui va nhan System.getBytes(). sendData = "getDate". IPAddress.*. . class UDPTimeClient { public static void main(String args[]) throws Exception { DatagramSocket clientSocket = new DatagramSocket().net. import java.println(request). // Nhan dl tu client serverSocket. byte[] sendData = new byte[1024]. byte[] receiveData = new byte[1024]. // Lay dia chi IP cua may client InetAddress IPAddress = receivePacket. // Lay ngay gio de gui nguoc lai client String request = new String(receivePacket.equals("getDate")) sendData = new Date().send(sendPacket). while (true) { Thread.toString().length). System.io. } } } Client: package bai2. DatagramPacket sendPacket = new DatagramPacket(sendData.getByName("localhost").getData()). port). import java.println("Server is started").// gan cong dong InetAddress IPAddress = InetAddress. receiveData.receive(receivePacket).out.

int day. import java. //clientSocket.*.apache. clientSocket.m. IPAddress.out.*.*.month. import com.io.// nhan dl tu server String str = new String(receivePacket. import java.close().send(sendPacket).impl.receive(receivePacket). .println(str).util.sun.s. } } } Kết quả: BÀI 3: Server: package Bai3. byte[] sendData = new byte[1024]. class UDPTimeServer { public static void main(String args[]) throws Exception { // Gan cong 9876 cho chuong trinh DatagramSocket serverSocket = new DatagramSocket(9876). import java.internal.org.length. receiveData.y.println("Server is started").// gui dl cho server // tao datagram rong de nhan dl DatagramPacket receivePacket = new DatagramPacket(receiveData.net.DayDV. // Tao cac mang byte de chua dl gui va nhan System.dv.getData()).xerces.// lay dl tu // packet // nhan duoc System. byte[] receiveData = new byte[1024].h. 9876).sendData.length).xs.out. clientSocket.

receive(receivePacket).getBytes(). IPAddress. } else if (request. // Tao goi rong de nhan du lieu tu client DatagramPacket receivePacket = new DatagramPacket(receiveData. // Gui dl lai cho client serverSocket.equals("y")){ y=thoigian. sendData=Integer. } else if (request.toString(y).out.getBytes(). sendData=Integer. sendData=Integer.equals("month")){ month=thoigian. } DatagramPacket sendPacket = new DatagramPacket(sendData.toString(m).trim().toString(month).getBytes(). port).trim().equals("day")){ day=thoigian.trim(). } else if (request. sendData=Integer.equals("s")){ s=thoigian. } else if (request.length.length).getData()).getHours().toString(h).trim(). // Lay port cua chuong trinh client int port = receivePacket. sendData=Integer.getBytes(). // Lay dia chi IP cua may client InetAddress IPAddress = receivePacket.getBytes(). // Nhan dl tu client serverSocket.getDate().trim().getSeconds(). sendData.getMonth()+1. // Lay ngay gio de gui nguoc lai client String request = new String(receivePacket.getMinutes(). sendData=Integer.toString(s).trim(). System. } else{ sendData = "Server not know what you want?".getAddress().getPort(). } else if (request.getBytes().toString(day).send(sendPacket).equals("m")){ m=thoigian.getBytes().while (true) { Date thoigian=new Date(). receiveData.equals("h")){ h=thoigian. if (request.println(request).getYear()+1900. } } } Client: .

import java.io. //clientSocket.send(sendPacket). } } } Kết quả: . byte[] receiveData = new byte[1024].// gan cong dong InetAddress IPAddress = InetAddress.Scanner. String chuoi=in.// nhan dl tu server String str = new String(receivePacket. hour.net.year.close(). sendData. clientSocket.length).// lay dl tu packet // nhan duoc System.util.nextLine(). minute. // tao datagram co noi dung yeu cau loai dl de gui cho server DatagramPacket sendPacket = new DatagramPacket(sendData.receive(receivePacket). import java. sendData=chuoi.getBytes(). DatagramSocket clientSocket = new DatagramSocket(). byte[] sendData = new byte[1024].// gui dl cho server // tao datagram rong de nhan dl DatagramPacket receivePacket = new DatagramPacket(receiveData.print("Nhap yeu cau (day.out.getData()). while(true){ System.*. class UDPTimeClient { public static void main(String args[]) throws Exception { Scanner in = new Scanner(System.getBytes().package Bai3.out.println(str).month. 9876).in).getByName("localhost"). import java. //sendData = "getDate". receiveData.*. clientSocket.second):"). IPAddress.length.

println("Server is started"). String thoigian=df. // Lay port cua chuong trinh client int port = receivePacket.getData()). java.BÀI 4: Server: package Bai4.getAddress(). receiveData. java. java. // Lay dia chi IP cua may client InetAddress IPAddress = receivePacket.*.SimpleDateFormat.*. class Server { public static void main(String args[]) throws Exception { // Gan cong 9876 cho chuong trinh DatagramSocket serverSocket = new DatagramSocket(9876).util.getBytes().format(new Date()). // Nhan dl tu client serverSocket. // Tao cac mang byte de chua dl gui va nhan System. System.out.println(request).out.DateFormat. import import import import import java.net. while (true) { // Tao goi rong de nhan du lieu tu client DatagramPacket receivePacket = new DatagramPacket(receiveData.*. byte[] sendData = new byte[8].io.length).equals("getDate")){ sendData = thoigian. if (request.trim(). DateFormat df=new SimpleDateFormat("hh/mm/ss"). // Lay ngay gio de gui nguoc lai client String request = new String(receivePacket. byte[] receiveData = new byte[8].getPort().text.text.receive(receivePacket). } . java.

add(gio). public Client() { pn = new Panel(new GridLayout(1.awt.awt. TextField gio. stop = new Button("Stop"). DatagramPacket sendPacket = new DatagramPacket(sendData.net.length.UnknownHostException.send(sendPacket).*.DatagramSocket. } } } Client: package Bai4.else sendData = "Server not know what you want?".net.net. java. java. pn1. pn2.add(phut).net. giay = new TextField().net.add(stop).getBytes().*.event. gio = new TextField(). java. pn1.util. java. . import import import import import import import import import import import java.SocketException. pn2. sendData. pn.io. Panel pn. pn.DatagramPacket. pn2 = new Panel(new FlowLayout()). // Gui dl lai cho client serverSocket.ActionListener. add(pn1). port). pn2.addActionListener(this). 1)).add(start). stop.add(pn).*. pn1. stop. java.event. phut.add(pn2). phut = new TextField().addActionListener(this). java.InetAddress. java.ActionEvent. public class Client extends JFrame implements ActionListener { Button start. pn. java.net. java.add(giay).swing. import javax.JFrame. start. 3)). IPAddress. java.*. giay. start = new Button("Start"). pn1 = new Panel(new GridLayout(2.awt.

getByName("localhost").// gan cong // dong InetAddress IPAddress = InetAddress. receiveData.setText(a[0]). i++. clientSocket.length. } public static void main(String s[]) { new Client(). while (true) { Thread.setSize(300.// nhan dl tu server String str = new String(receivePacket. IPAddress. } @Override public void actionPerformed(ActionEvent e) { if(e. 100)."/").getBytes(). String a[] =new String[3]. int i=0. clientSocket. 9876). sendData = "getDate".getSource()==start){ try{ DatagramSocket clientSocket = new DatagramSocket(). . StringTokenizer stk=new StringTokenizer(str. sendData.length). byte[] sendData = new byte[8].send(sendPacket).receive(receivePacket). // tao datagram co noi dung yeu cau loai dl de gui cho // server DatagramPacket sendPacket = new DatagramPacket(sendData.hasMoreTokens()){ a[i]=stk.nextToken().// gui dl cho server // tao datagram rong de nhan dl DatagramPacket receivePacket = new DatagramPacket(receiveData.getData()). while(stk. } gio. byte[] receiveData = new byte[8]. show().sleep(1000).

out. // clientSocket.close().println(str).phut. } }catch (Exception ex) { // TODO: handle exception } } if(e.getSource()==stop){ } } } Kết quả: BÀI 5: . giay. System.setText(a[2]).setText(a[1]).

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->