Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 13 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
13
Dung lượng
21,69 KB
Nội dung
Bài : Kết Nối TCP Sử Dụng Socket, chuẩn gửi nhận SỐ - SỐ Trang Máy Chủ : import java.io.*; import java.net.*; class TCPSS { public static void main(String[] args) throws Exception { int port = 2812; ServerSocket server = new ServerSocket(port); Socket client = server.accept(); BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream())); PrintWriter pw = new PrintWriter(client.getOutputStream()); String st = br.readLine(); double so = Double.parseDouble(st); double kqD = XuLy(so); String kqS = String.valueOf(kqD); pw.write(kqS + "\n"); pw.flush(); pw.close(); br.close(); client.close(); server.close(); } public static double XuLy(double so) { return Math.pow(so,3); } } Trang Máy Khách : import java.io.*; import java.net.*; class TCPCS { public static void main(String[] args) throws Exception { int portS = 2812; InetAddress ipS = InetAddress.getByName("localhost"); Socket server = new Socket(ipS,portS); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); BufferedReader br = new BufferedReader(new InputStreamReader(server.getInputStream())); PrintWriter pw = new PrintWriter(server.getOutputStream()); System.out.print("Nhap so bat ky : "); double so = Double.parseDouble(in.readLine()); String st = String.valueOf(so); pw.write(st+"\n"); pw.flush(); String kqS = br.readLine(); double kqD = Double.parseDouble(kqS); System.out.println("Lap Phuong So = " + kqD); pw.close(); br.close(); in.close(); server.close(); } } Bài : Kết Nối TCP Sử Dụng Socket, gửi nhận CHUỖI – CHUỖI Trang Máy Chủ : import java.io.*; import java.net.*; class TCPSC { public static void main(String[] args) throws Exception { int port = 2812; ServerSocket server = new ServerSocket(port); Socket client = server.accept(); BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream())); PrintWriter pw = new PrintWriter(client.getOutputStream()); String st = br.readLine(); String kq = XuLy(st); pw.write(kq + "\n"); pw.flush(); pw.close(); br.close(); client.close(); server.close(); } public static String XuLy(String st) { return st.toUpperCase(); } } Trang Máy Khách : import java.io.*; import java.net.*; class TCPCC { public static void main(String[] args) throws Exception { int portS = 2812; InetAddress ipS = InetAddress.getByName("localhost"); Socket server = new Socket(ipS,portS); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); BufferedReader br = new BufferedReader(new InputStreamReader(server.getInputStream())); PrintWriter pw = new PrintWriter(server.getOutputStream()); System.out.print("Nhap chuoi bat ky : "); String st = in.readLine(); pw.write(st+"\n"); pw.flush(); String kq = br.readLine(); System.out.println("Chuoi Hoa : " + kq); pw.close(); br.close(); in.close(); server.close(); } } Bài : Kết Nối UDP Sử Dụng Socket, chuẩn gửi nhận SỐ - SỐ import java.io.*; import java.net.*; class UDPSS { public static void main(String[] args) throws Exception { int port = 2812; DatagramSocket socket = new DatagramSocket(port); DatagramPacket packet; byte []data; try { data = new byte[1024]; packet = new DatagramPacket(data,data.length); socket.receive(packet); String st = new String(packet.getData(),0,packet.getLength()); double so = Double.parseDouble(st); InetAddress ipC = packet.getAddress(); int portC = packet.getPort(); double kqD = XuLy(so); String kqS = String.valueOf(kqD); data = kqS.getBytes(); packet = new DatagramPacket(data,data.length,ipC,portC); socket.send(packet); socket.close(); } catch (UnknownHostException evt) { evt.printStackTrace(); } } public static double XuLy(double so) { return Math.sqrt(so); } } Trang Máy Khách : import java.io.*; import java.net.*; class UDPCS { public static void main(String[] args) throws Exception { DatagramSocket socket = new DatagramSocket(); DatagramPacket packet; byte []data; InetAddress ipS = InetAddress.getByName("localhost"); int portS = 2812; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Nhap so bat ky : "); double so = Double.parseDouble(in.readLine()); String st = String.valueOf(so); data = st.getBytes(); packet = new DatagramPacket(data,data.length,ipS,portS); socket.send(packet); data = new byte[1024]; packet = new DatagramPacket(data,data.length); socket.receive(packet); String kqS = new String(packet.getData(),0,packet.getLength()); double kqD = Double.parseDouble(kqS); System.out.println("Can Bac Hai Tinh Duoc : " + kqD); socket.close(); } } Bài : Kết Nối UDP Sử Dụng Socket, gửi nhận CHUỖI – CHUỖI class UDPSC { public static void main(String[] args) throws Exception { int port = 2812; DatagramSocket socket = new DatagramSocket(port); DatagramPacket packet; byte []data; try { data = new byte[1024]; packet = new DatagramPacket(data,data.length); socket.receive(packet); String st = new String(packet.getData(),0,packet.getLength()); InetAddress ipC = packet.getAddress(); int portC = packet.getPort(); String kq = XuLy(st); data = kq.getBytes(); packet = new DatagramPacket(data,data.length,ipC,portC); socket.send(packet); socket.close(); } catch (UnknownHostException evt) { evt.printStackTrace(); } } public static String XuLy(String st) { st = st.trim(); while (st.indexOf(" ")>=0) { st = st.replaceAll(" "," "); } return st; } } Trang Máy Khách : import java.io.*; import java.net.*; class UDPCC { public static void main(String[] args) throws Exception { DatagramSocket socket = new DatagramSocket(); DatagramPacket packet; byte []data; InetAddress ipS = InetAddress.getByName("localhost"); int portS = 2812; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Nhap chuoi bat ky : "); String st = in.readLine(); data = st.getBytes(); packet = new DatagramPacket(data,data.length,ipS,portS); socket.send(packet); data = new byte[1024]; packet = new DatagramPacket(data,data.length); socket.receive(packet); String kq = new String(packet.getData(),0,packet.getLength()); System.out.println("Chuoi Ket Qua Tu May Chu : " + kq); socket.close(); } } Tổng Hợp Một Số Hàm Xử Lý A, hàm chuẩn hoá chuỗi, kết trả chuỗi sau chuẩn hoá : public static String ChuanHoa(String st) { st=st.trim(); while (st.indexOf(" ")>=0) { st=st.replaceAll(" "," "); } st = st.toLowerCase(); String[] ARR=st.split(" "); for (int i = 0; i < ARR.length; i++) ARR[i]=ARR[i].substring(0, 1).toUpperCase().concat(ARR[i].substring(1)); st=""; for (int i = 0; i < ARR.length-1; i++) st=st.concat(ARR[i])+" "; st=st.concat(ARR[ARR.length-1]); return st; } B, Hàm đảo ngược chuỗi : public static String DaoNguoc(String st) { StringBuffer temp = new StringBuffer(st); return temp.reverse().toString(); } C, Hàm đếm số ký tự chuỗi (khơng tính dấu cách) : public static int DemKyTu(String st) { int dem=0; for (int i=0; i