Bài tập môn lập trình mạng - Cách 2

4 28 0
Bài tập môn lập trình mạng - Cách 2

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

Thông tin tài liệu

[r]

(1)

Trang 1/4

BÀI TP ðIU KIN

Mơn : LP TRÌNH MNG

(cách 2)

CÂU HI

Giải phương trình bậc dùng giao thức TCP: Client gởi pt bậc → 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 thienthanh Server kết nối ở cổng 1234 * ðoạn 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);

} {

handleClient(); }while(true); }

public static void handleClient() {

Socket clientsock = null; String kq = "";

try {

(2)

Trang 2/4

/* Co ket noi xay Lay cac thong tin tu may client in 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);

if (aStr.equalsIgnoreCase("quit")) break; String bStr=in.readLine();

System.out.println(" Nhan tu Client ("+hostname+") gia tri B = "+bStr);

if (bStr.equalsIgnoreCase("quit")) break; String cStr=in.readLine();

System.out.println(" Nhan tu Client ("+hostname+") gia tri C = "+cStr);

if (cStr.equalsIgnoreCase("quit")) break; 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=" Ph/trinh co vo so nghiem"; else

kq=" Ph/trinh vo nghiem";

else kq=" Ph/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 = " Ph/trinh co nghiem: x1="+x1+", x2="+x2;

else

if (delta==0) kq= " Ph/trinh co nghiem kep: x1=x2="+(-b/(2*a));

else kq=" Ph/trinh vo nghiem"; }

System.out.println("Ket qua:" +kq); out.println("Ket qua tu Server:" +kq); }

}

catch(IOException ioEx) {

(3)

Trang 3/4

}

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);

} } } }

* ðoạn chương trình tại Client như sau:

Client.java

package baitap3;

import java.io.*; import java.net.*; import java.util.*; public class Client {

public static void main(String[] args) { try{

/* Gia su ten may chu la thienthanh,

co the thay bang dia chi IP cua may Server */ Socket clientsock = new Socket("thienthanh", 1234);

PrintWriter out = new PrintWriter(clientsock.getOutputStream(), true); BufferedReader in = new BufferedReader(new

InputStreamReader(clientsock.getInputStream())); BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));

System.out.println("\n -"); System.out.println(" Giai phuong trinh bac hai dung giao thuc TCP"); System.out.println(" -"); System.out.println(" Hay cho biet gia tri cua A, B va C hoac type QUIT de thoat");

while (true) {

System.out.print("\n Nhap vao he so A = "); String aStr = stdIn.readLine();

out.println(aStr);

//So sanh chuoi nhan duoc Neu la "quit" thi ket thuc if (aStr.equalsIgnoreCase("quit")) break;

System.out.print(" Nhap vao he so B = "); String bStr = stdIn.readLine();

(4)

Trang 4/4

//So sanh chuoi nhan duoc Neu la "quit" thi ket thuc if (bStr.equalsIgnoreCase("quit")) break;

System.out.print(" Nhap vao he so C = "); String cStr = stdIn.readLine();

out.println(cStr);

//So sanh chuoi nhan duoc Neu la "quit" thi ket thuc if (cStr.equalsIgnoreCase("quit")) break;

System.out.println(in.readLine()); }

// Dong luong va ngat ket noi out.close();

in.close();

clientsock.close();

System.out.println("\n Da ngat ket noi khoi Server, thoat khoi chuong trinh");

System.exit(1); }catch(IOException e){

//Neu co loi thi in man hinh System.out.println(e);

System.exit(1); }

Ngày đăng: 09/03/2021, 05:10

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan