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

3 6 0
Bài tập lập trình mạng - Cách 1

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

Thông tin tài liệu

[r]

(1)

Trang 1/6

BÀI TP ðIU KIN Mơn : LP TRÌNH MNG

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 = "";

(2)

Trang 2/6

/* Cho doi ket noi tu may client */ clientsock = servsock.accept();

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

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

(3)

Trang 3/6

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

Ngày đăng: 10/03/2021, 14:08

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

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

Tài liệu liên quan