Thứ Sáu, 10 tháng 12, 2010

Java Dùng TCP Giải Phương Trình Bậc 2

Chương trình giải phương trình bậc 2  trong đó: Client nhận 3 tham so a b c nguoi dung nhập vao va chuyển sang Server . Server xử lý và gửi kết quả lại cho client.
Chương trình khá đơn giản phù hợp cho những bạn mới bắt đầu học java. Nếu có gì thắc mắc trong chương trình comment lại cho mình sẽ giải thích .^^!
Server
import java.io.*;
import java.net.*;

public class TCPGiaiBac2Server {
      static int defaultPort = 7;

      /**
       * @param args
       */
      public static void main(String[] args) throws IOException {
            // TODO Auto-generated method stub
            ServerSocket ss = new ServerSocket(defaultPort);
            System.out.println("Server starting ....");
            while (true) {
                  Socket s = ss.accept();
                  GiaiBac2Server thread = new GiaiBac2Server(s);
                  thread.start(); // cho chay luong
            }
      }

}

// Ðinh nghia lop ke thua tu Thread de xu mot ket noi moi
class GiaiBac2Server extends Thread {
      private Socket connectToClient; // khoi tao mot socket de ket noi voi client

      public GiaiBac2Server(Socket socket) {
            connectToClient = socket;
      }

      // Cài dat hàm run() de thuc hien theo luong moi duoc tao ra
      public void run() {
            try {
                  PrintWriter out = null;
                  out = new PrintWriter(connectToClient.getOutputStream(), true);
                  DataInputStream inFromClient = new DataInputStream(connectToClient
                              .getInputStream());

                  while (true) {
                        String chuoiKq = "";
                        float a = inFromClient.readFloat();
                        float b = inFromClient.readFloat();
                        float c = inFromClient.readFloat();

                        float delta = b * b - 4 * a * c;

                        if (a == 0)
                              if (b == 0)
                                    if (c == 0) {
                                          chuoiKq = "Phuong trinh co vo so nghiem : \n";
                                          out.println(chuoiKq);
                                    } else {
                                          chuoiKq = "Phuong trinh vo nghiem :\n";
                                          // outToClient.writeBytes(chuoiKq);
                                          out.println(chuoiKq);
                                    }
                              else {

                                    chuoiKq = "Phuong trinh co nghiem duy nhat: \n" + c / b;
                                    out.println(chuoiKq);
                              }
                        else {
                              if (delta < 0) {
                                    chuoiKq = "Phuong trinh vo nghiem";
                                    out.println(chuoiKq);
                              } else if (delta > 0) {
                                    float x1 = (float) (-b + Math.sqrt(delta)) / (2 * a);
                                    float x2 = (float) (-b - Math.sqrt(delta)) / (2 * a);
                                    chuoiKq = "phuong trinh co 2 nghiem phan biet x1 =:"
                                                + x1 + "\n" + "Va x2 =" + x2;

                                    out.println(chuoiKq);

                              } else {
                                    chuoiKq = ("Phuong trinh co nghiem kep x1=x2" + (-b / (2 * a)));

                                    out.println(chuoiKq);
                              }
                        }

                  }
            } catch (IOException ex) {
                  System.err.println(ex);
            }
      }
}

Client
import java.io.*;
import java.net.*;

public class TCPGiaiBac2Client {

      static int portServer = 7;
      static String ipServer = "localhost";

      public static void main(String arg[])

      {
            try {
                  BufferedReader banPhim = new BufferedReader(new InputStreamReader(
                              System.in));
                  Socket s = new Socket(ipServer, portServer);

                  DataOutputStream outToServer = new DataOutputStream(s
                              .getOutputStream());
                  LuongNhanGiaiBac2Client thread = new LuongNhanGiaiBac2Client(s);
                  thread.start();
                  while (true) {
                        System.out.print("Nhap vao 3 so a b c");
                        System.out.print("Nhap so thu nhat: ");
                        float so1 = Float.parseFloat(banPhim.readLine());

                        System.out.print("Nhap so thu hai: ");
                        float so2 = Float.parseFloat(banPhim.readLine());

                        System.out.print("Nhap vao so thu ba :");
                        float so3 = Float.parseFloat(banPhim.readLine());
                        // gui du lieu len server
                        outToServer.writeFloat(so1);
                        outToServer.writeFloat(so2);
                        outToServer.writeFloat(so3);
                  }

            } catch (IOException ex) {
                  System.err.println(ex);

            }
      }
}

class LuongNhanGiaiBac2Client extends Thread {
      private Socket ConnectToServer; // khoi tao mot socket de ket noi toi client

      public LuongNhanGiaiBac2Client(Socket socket) {
            ConnectToServer = socket;

      }

      public void run() {
            try {

                  BufferedReader in = new BufferedReader(new InputStreamReader(
                              ConnectToServer.getInputStream()));
                  while (true) {

                        String nghiem = in.readLine();

                        System.out.println("" + nghiem);
                  }
            } catch (IOException e) {
                  System.out.println("Loi" + e);
            }

      }

}

3 nhận xét:

  1. Cám ơn nhiều nghe...! Sao bạn không tạo thư mục cho dễ download, sẵng đó bạn làm dùm 2 bài kia luôn nghe, rồi mình cảm ơn tiếp tục nữa, hihi....!

    Trả lờiXóa
  2. uhm mình sẽ giải khi có time hì

    Trả lờiXóa
  3. có bài làm tương tự mà có from k ad ơi from hiển thị jfreamFrom

    Trả lờiXóa