Bài tập điều kiện môn Lập trình mạng - Cách 1

pdf 6 trang hoanguyen 6320
Bạn đang xem tài liệu "Bài tập điều kiện môn Lập trình mạng - Cách 1", để tải tài liệu gốc về máy bạn click vào nút DOWNLOAD ở trên

Tài liệu đính kèm:

  • pdfbai_tap_dieu_kien_mon_lap_trinh_mang.pdf

Nội dung text: Bài tập điều kiện môn Lập trình mạng - Cách 1

  1. BÀI T P ðIU KI N Môn : LP TRÌNH M NG CÂU H I Gi i ph ươ ng trình b c 2 dùng giao th c TCP: Client g i pt b c 2 → Server gi i. Client nh p A, B, C g i cho Server, Server gi i xong g i k t qu v cho Client. BÀI LÀM Gi s máy Server có tên là thienthanh . Server k t n i c ng 1234 . * ðon ch ươ ng trình t i Server nh ư sau: Server.java package baitap3; import java.io.*; import java.net.*; import java.util.*; public class Server { public static ServerSocket servsock ; public static final int PORT = 1234; public static void main(String[] args) { try { /* Tao doi tuong ServerSocket dung de lang nghe ket noi tu cac may client gui den cong 1234 */ servsock = new ServerSocket(PORT); int localPort = servsock.getLocalPort(); System.out.println("\n Server dang mo port " +localPort+"."); } catch(IOException ioEx) { System.out.println("\n Khong mo duoc port: " +PORT); System.exit(1); } do { handleClient(); }while(true); } public static void handleClient() { Socket clientsock = null; String kq = ""; try { Trang 1/6
  2. /* Cho doi ket noi tu may client */ clientsock = servsock.accept(); /* Co ket noi xay ra. Lay cac thong tin tu may client in ra man hinh */ //lay ten may client String hostname = clientsock.getInetAddress().getHostName(); // lay dia chi IP cua may client String destAdd = clientsock.getInetAddress().getHostAddress(); // lay cong cua may client int destPort = clientsock.getPort(); System.out.println(" Chap nhan ket noi tu "+hostname+" ("+destAdd+") tai port "+destPort+"."); PrintWriter out = new PrintWriter(clientsock.getOutputStream(), true); BufferedReader in = new BufferedReader(new InputStreamReader(clientsock.getInputStream())); while (true) { String aStr=in.readLine(); System.out.println("\n Nhan tu Client ("+hostname+") gia tri A = "+aStr); String bStr=in.readLine(); System.out.println(" Nhan tu Client ("+hostname+") gia tri B = "+bStr); String cStr=in.readLine(); System.out.println(" Nhan tu Client ("+hostname+") gia tri C = "+cStr); float a,b,c; try{ a = Float.parseFloat(aStr); b = Float.parseFloat(bStr); c = Float.parseFloat(cStr); }catch(NumberFormatException e) {out.println(" So khong hop le");continue;} if (a==0) if (b==0) if (c==0) kq=" Phuong trinh co vo so nghiem"; else kq=" Phuong trinh vo nghiem"; else kq=" Phuong trinh co nghiem x="+(- c/b); else { double delta = b*b-4*a*c; double x1 = (-b+Math.sqrt(delta))/(2*a); double x2 = (-b-Math.sqrt(delta))/(2*a); if (delta>0) kq = " Phuong trinh co 2 nghiem: x1="+x1+", x2="+x2; else if (delta==0) kq= " Phuong trinh co nghiem kep: x1=x2="+(- b/(2*a)); else kq=" Phuong trinh vo nghiem"; } System.out.println("Ket qua:" +kq); out.println(kq); } } catch(IOException ioEx) { ioEx.printStackTrace(); } Trang 2/6
  3. finally { try { clientsock.close(); servsock.close(); System.out.println("\n Da ngat ket noi, thoat khoi chuong trinh"); System.exit(1); } catch(IOException ioEx) { System.out.println("\n Khong ngat duoc ket noi!"); System.exit(1); } } } } * ðon ch ươ ng trình t i Client nh ư sau: Client.java package baitap3; import java.awt.BorderLayout; import java.awt.Dimension; import javax.swing.JFrame; import javax.swing.JPanel; import java.net.*; import java.util.*; import java.io.*; import javax.swing.JLabel; import javax.swing.JTextField; import com.borland.jbcl.layout.XYLayout; import com.borland.jbcl.layout.*; import javax.swing.JButton; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Client extends JFrame { JPanel contentPane; JLabel Label_A = new JLabel(); JTextField txt_A = new JTextField(); JLabel Label_B = new JLabel(); JTextField txt_B = new JTextField(); XYLayout xYLayout1 = new XYLayout(); JTextField txt_C = new JTextField(); JLabel LabelC = new JLabel(); JTextField txt_kq = new JTextField(); Trang 3/6
  4. JLabel LabelKQ = new JLabel(); JButton btn_OK = new JButton(); JButton btn_RS = new JButton(); Socket cs;//socket kn voi server static BufferedReader in;//doi tuong dung de lay dl tu server PrintWriter out;//doi tuong dung de goi dl cho server String nameClient; JButton btn_Exit = new JButton(); public Client() { try { setDefaultCloseOperation(EXIT_ON_CLOSE); jbInit(); } catch (Exception exception) { exception.printStackTrace(); } } / * Component initialization. * * @throws java.lang.Exception */ private void jbInit() throws Exception { contentPane = (JPanel) getContentPane(); contentPane.setLayout(xYLayout1); setSize(new Dimension(400, 300)); setTitle("PTBH Client"); Label_A.setText("A ="); Label_B.setText("B ="); txt_B.setText(""); LabelC.setText("C = "); LabelKQ.setText("Ket qua nhan duoc tu Server:"); btn_OK.setText("OK"); btn_OK.addActionListener(new Client_btn_OK_actionAdapter(this)); btn_RS.setText("Reset"); btn_RS.addActionListener(new Client_btn_RS_actionAdapter(this)); btn_Exit.setText("Exit"); btn_Exit.addActionListener(new Client_btn_Exit_actionAdapter(this)); contentPane.add(Label_A, new XYConstraints(28, 30, 28, -1)); contentPane.add(Label_B, new XYConstraints(28, 65, 28, -1)); contentPane.add(LabelC, new XYConstraints(29, 100, 28, -1)); contentPane.add(LabelKQ, new XYConstraints(28, 134, 259, -1)); contentPane.add(txt_C, new XYConstraints(60, 96, 301, -1)); contentPane.add(txt_B, new XYConstraints(59, 61, 301, -1)); contentPane.add(txt_A, new XYConstraints(59, 28, 302, 20)); Trang 4/6
  5. contentPane.add(txt_kq, new XYConstraints(2, 154, 399, -1)); contentPane.add(btn_OK, new XYConstraints(48, 188, 74, -1)); contentPane.add(btn_Exit, new XYConstraints(288, 188, 74, -1)); contentPane.add(btn_RS, new XYConstraints(171, 188, 74, -1)); cs = new Socket("thienthanh", 1234); out = new PrintWriter(cs.getOutputStream(), true); in = new BufferedReader(new InputStreamReader(cs.getInputStream())); nameClient=InetAddress.getLocalHost().getHostName(); } public static void main(String[] args) throws IOException { Client ptbhclient = new Client(); ptbhclient.pack(); ptbhclient.setVisible(true); } public void btn_OK_actionPerformed(ActionEvent e) { try{ String aStr = txt_A.getText(); out.println(aStr); String bStr = txt_B.getText(); out.println(bStr); String cStr = txt_C.getText(); out.println(cStr); txt_kq.setText(in.readLine()); }catch (Exception ex){ ex.printStackTrace(); } } public void btn_RS_actionPerformed(ActionEvent e) { txt_A.setText(""); txt_B.setText(""); txt_C.setText(""); txt_kq.setText(""); } public void btn_Exit_actionPerformed(ActionEvent e) { try { in.close(); out.close(); cs.close(); System.exit(1); }catch(IOException et){ System.exit(1); } } Trang 5/6
  6. } class Client_btn_Exit_actionAdapter implements ActionListener { private Client adaptee; Client_btn_Exit_actionAdapter(Client adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.btn_Exit_actionPerformed(e); } } class Client_btn_RS_actionAdapter implements ActionListener { private Client adaptee; Client_btn_RS_actionAdapter(Client adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.btn_RS_actionPerformed(e); } } class Client_btn_OK_actionAdapter implements ActionListener { private Client adaptee; Client_btn_OK_actionAdapter(Client adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.btn_OK_actionPerformed(e); } } Trang 6/6