Chào các bạn! Vì nhiều lý do từ nay Truyen2U chính thức đổi tên là Truyen247.Pro. Mong các bạn tiếp tục ủng hộ truy cập tên miền mới này nhé! Mãi yêu... ♥

dạng 3

Dạng 3- Dạng bài tập về lập trình ứng dụng mô hình Client-Server ở chế độ có nối kết.

1. Viết chương trình nhập vào 1 dãy số phía client và gửi dãy số sang chương trình server, server tính tổng rồi gửi lại client để in ra màn hình.

2. Viết chương trình Client liên tiếp cho phép nhập vào các xâu kí tự chữ thường rồi gửi sang server, server đổi sang chữ in hoa và gửi lại phía client để in ra màn hình.

3. Viết chương trình Client liên tiếp cho phép nhập vào các xâu kí tự chữ in hoa rồi gửi sang server, server đổi sang chữ thường và gửi lại phía client để in ra màn hình.

4. Viết chương trình Server nhập vào các số nguyên và gửi sang phía client cho đến khi nhập vào chữ ‘A’ thì dừng. Client nhận được chữ ‘A’ thì tính tổng các số đã nhận được và in kết quả ra màn hình.

5. Viết chương trình Server nhập vào các số nguyên và gửi sang phía client cho đến khi nhập vào chữ ‘K’ thì dừng. Client nhận được chữ ‘K’ thì tính tổng các số nguyên tố đã nhận được và in kết quả ra màn hình.

1. Viết chương trình nhập vào 1 dãy số phía client và gửi dãy số sang chương trình server, server tính tổng rồi gửi lại client để in ra màn hình.

Code:

…………| Server |…………….    

import java.io.*;

import java.net.*;

public class Server {

    public static void main(String[] args) throws Exception {

        ServerSocket ss = null;

        String fromClient;

        int tong = 0, a, b;

        try {

            ss = new ServerSocket(1234);

            System.out.println("Đang chờ Client kết nối...");

        } catch (Exception e) {

            System.err.println("Không kết nối được với Client ở cổng 1234! " + e);

        }

        Socket cs = null;

        try {

            cs = ss.accept();

            System.out.println("Client đã kết nối!");

            DataInputStream in = new DataInputStream(cs.getInputStream());

            try {

                fromClient = in.readLine();

                a = Integer.parseInt(fromClient);

            } catch (Exception e) {

                a = 0;}

            try {

                fromClient = in.readLine();

                b = Integer.parseInt(fromClient);

            } catch (Exception e) {

                b = 0;}

            PrintWriter out = new PrintWriter(cs.getOutputStream(), true);

            tong = a + b;

            out.println(tong);

            in.close();

            out.close();

        } catch (Exception e) {

            System.out.println("Không kết nối được với Client! ");}

        cs.close();}}

…………| Client|………...

import java.io.*;

import java.net.*;

public class Client {

static int nhap(){

        String st;

        DataInputStream nhap = new DataInputStream(System.in);

        try {

            st = nhap.readLine();

            n = Integer.parseInt(st);

        } catch (IOException | NumberFormatException e) {

            n = 0;}

        return n;}

    public static void main(String[] args) throws Exception {

        Socket cs = null;

        String st = "";

        int tong = 0;

        int a, b;

        try {

            cs = new Socket("localhost", 1234);

            PrintWriter out = new PrintWriter(cs.getOutputStream(), true);

            System.out.print("a = ");

            a = nhap();

            out.println(a);

            System.out.print("b = ");

            b = nhap();

            out.println(b);

            DataInputStream in = new DataInputStream(cs.getInputStream());

            try{

                st = in.readLine();

            }catch(Exception e){

                st = "";}

            tong = Integer.parseInt(st);

            System.out.println("tong = "+tong);

            out.close();

            in.close();

        } catch (IOException | NumberFormatException e) {

            System.out.println("Không kết nối được với Server! ");}

        System.out.println("Kết thúc chương trình! Hẹn gặp lại!");

        cs.close();}}

2. Viết chương trình Client liên tiếp cho phép nhập vào các xâu kí tự chữ thường rồi gửi sang server, server đổi sang chữ in hoa và gửi lại phía client để in ra màn hình.

…………| Server |…………….                 

import java.io.*;

import java.net.*;

public class Server {

    public static void main(String[] args) {

        ServerSocket ss = null;

        try {

            ss = new ServerSocket(1234);

            System.out.println("Đang chờ Client kết nối...");

        } catch (Exception e) {

            System.out.println("Không kết nối được với Client ở cổng 1234");}

        Socket cs = null;

        try {

            cs = ss.accept();

            System.out.println("Client đã kết nối!");

            System.out.println("----------------------------------------------");

            while (true) {

                DataInputStream in = new DataInputStream(cs.getInputStream());

                String fromClient = in.readLine();

                System.out.println("Chuỗi nhận từ Client: " + fromClient);

                PrintWriter out = new PrintWriter(cs.getOutputStream(), true);

                out.println(fromClient.toUpperCase());}

        } catch (Exception e) {

            System.out.println("Kết nối thất bại!");}}}

______________________

…………| Client|………...

import java.io.*;

import java.net.*;

public class Client {

public static void main(String[] args) {

        Socket cs = null;

        String st = "";

        try {

            cs = new Socket("localhost", 1234);

            System.out.println("Đã kết nối với Server qua cổng 1234!");

            System.out.println("----------------------------------------------");

            while (true) {

                System.out.print("Nhập vào chuỗi cần chuyển đổi: ");

                DataInputStream nhap = new DataInputStream(System.in);

                try{

                st = nhap.readLine();

                }catch(IOException e){

                    System.err.println("Lỗi: "+ e);

                }

                PrintWriter out = new PrintWriter(cs.getOutputStream(), true);

                out.println(st);

                //out.close();

                DataInputStream in = new DataInputStream(cs.getInputStream());

                String fromServer = in.readLine();

                //in.close();

                System.out.println("Chuỗi sau khi chuyển đổi    : " + fromServer);

                System.out.println("-------------------------------------------");

            }

        } catch (Exception e) {

            System.out.println("Không kết nối được với Server!");}}}

3. Viết chương trình Client liên tiếp cho phép nhập vào các xâu kí tự chữ in hoa rồi gửi sang server, server đổi sang chữ thường và gửi lại phía client để in ra màn hình.

…………| Server |…………….                 

import java.io.*;

import java.net.*;

public class Server {

 public static void main(String[] args) {

        ServerSocket ss = null;

        try {

            ss = new ServerSocket(1234);

            System.out.println("Đang chờ Client kết nối...");

        } catch (Exception e) {

            System.out.println("Không kết nối được với Client ở cổng 1234");

        }

        Socket cs = null;

        try {

            cs = ss.accept();

            System.out.println("Client đã kết nối!");

            System.out.println("----------------------------------------------");

            while (true) {

DataInputStream in = new DataInputStream(cs.getInputStream());

                String fromClient = in.readLine();

                System.out.println("Chuỗi nhận từ Client: " + fromClient);

                PrintWriter out = new PrintWriter(cs.getOutputStream(), true);

  out.println(fromClient.toLowerCase());

            }

        } catch (Exception e) {

            System.out.println("Kết nối thất bại!");}}}

___________________

………...| Client |…………..

import java.io.*;

import java.net.*;

public class Client {

public static void main(String[] args) {

        Socket cs = null;

        String st = "";

        try {

            cs = new Socket("localhost", 1234);

            System.out.println("Đã kết nối với Server qua cổng 1234!");

            System.out.println("----------------------------------------------");

            while (true) {

                System.out.print("Nhập vào chuỗi chữ in hoa: ");

                DataInputStream nhap = new DataInputStream(System.in);

                try{

                st = nhap.readLine();

                }catch(IOException e){

                    System.err.println("Lỗi: "+ e);}

                PrintWriter out = new PrintWriter(cs.getOutputStream(), true);

                out.println(st);

                DataInputStream in = new DataInputStream(cs.getInputStream());

                String fromServer = in.readLine();

                System.out.println("Chuỗi sau khi chuyển đổi : " + fromServer);

                System.out.println("-------------------------------------------");}

        } catch (Exception e) {

            System.out.println("Không kết nối được với Server!");}}}

4. Viết chương trình Server nhập vào các số nguyên và gửi sang phía client cho đến khi nhập vào chữ ‘A’ thì dừng. Client nhận được chữ ‘A’ thì tính tổng các số đã nhận được và in kết quả ra màn hình.

............| Server |…………………….

import java.io.*;

import java.net.*;

public class Server {

public static void main(String[] args) {

        ServerSocket ss = null;

        String st = "";

        int n = 0;

        try {

            ss = new ServerSocket(1234);

            System.out.println("Đang chờ Client kết nối...");

        } catch (Exception e) {

            System.err.println("Không kết nối được với server: " + e);}

        Socket cs = null;

        try {

            cs = ss.accept();

            System.out.println("Client đã kết nối!");

            System.out.println("Nhập vào các số nguyên, hoặc nhập \"A\"để kết thúc nhập:");

            DataInputStream nhap = new DataInputStream(System.in);

            PrintWriter out = new PrintWriter(cs.getOutputStream(), true);

            while (true) {

                try {

           st = nhap.readLine();

                } catch (Exception e) {                

                    st = "";  }                         

                out.println(st);

                if (st.equals("A")) {

                    break;}}

            System.out.println("Kết thúc chương trình! Hẹn gặp lại!");

            out.close();

            ss.close();

            cs.close();

        } catch (Exception e) {

            System.out.println("Lỗi truyền dữ liệu: " + e);}}}

…..…….| Client |………………

import java.io.*;

import java.net.*;

public class Client {

public static void main(String[] args) throws Exception {

        Socket cs = null;

        String fromServer;

        int[] mang = new int[100];

        int tong = 0, k = 0;

        try {

            cs = new Socket("localhost", 1234);

            System.out.println("Đã kết nối thành công!");

            System.out.println("Các số nhận từ Server: ");

            try (DataInputStream in = new DataInputStream(cs.getInputStream())) {

                do {

                    fromServer = in.readLine();

                    try {

                        mang[k] = Integer.parseInt(fromServer);

                        System.out.print(mang[k] + "  ");

                    } catch (Exception e) {

                        mang[k] = 0;}

                    k++;

                } while (!fromServer.equals("A"));

                for (int i = 0; i < k; i++) {

                    tong = tong + mang[i];}

                 System.out.println();

                System.out.println("Tổng các số nhận từ Server: " + tong);

                in.close();}

            cs.close();

        } catch (Exception e) {  System.out.println("Không kết nối được với Server! ");}}}

5. Viết chương trình Server nhập vào các số nguyên và gửi sang phía client cho đến khi nhập vào chữ ‘K’ thì dừng. Client nhận được chữ ‘K’ thì tính tổng các số nguyên tố đã nhận được và in kết quả ra màn hình.

…….....| Server |……………..

import java.io.*;

import java.net.*;

public class Server {

public static void main(String[] args) {

        ServerSocket ss = null;

        String st = "";

        int n = 0;

        try {

            ss = new ServerSocket(1234);

            System.out.println("Đang chờ Client kết nối...");

        } catch (Exception e) {

            System.err.println("Không kết nối được với server: " + e);}

        Socket cs = null;

        try {

            cs = ss.accept();

            System.out.println("Client đã kết nối!");

            System.out.println("Nhập vào các số nguyên, nhập \"K\" hoặc \"k\" để kết thúc nhập:");

            DataInputStream nhap = new DataInputStream(System.in);

            PrintWriter out = new PrintWriter(cs.getOutputStream(), true);

            while (true) {

                try {

                    st = nhap.readLine().toUpperCase();

                } catch (Exception e) {                

                st = "";     }                      

                out.println(st);

                if (st.equals("K")) {

                    break;}}

            System.out.println("Kết thúc chương trình! Hẹn gặp lại!");

            out.close();

            ss.close();

            cs.close();

        } catch (Exception e) {

            System.out.println("Lỗi truyền dữ liệu: " + e);}}}

……….| Client |...............

import java.io.*;

import java.net.*;

public class Client {

public static void main(String[] args) throws Exception {

        Socket cs = null;

        String fromServer;

        int[] mang = new int[100];

        int tong = 0, k = 0;

        try {

            cs = new Socket("localhost", 1234);

            System.out.println("Đã kết nối thành công!");

            System.out.println("Các số nhận từ Server: ");

            try (DataInputStream in = new DataInputStream(cs.getInputStream())) {

                do {

                    fromServer = in.readLine();

                    try {  mang[k] = Integer.parseInt(fromServer);

                        System.out.print(mang[k] + "  ");

                    } catch (Exception e) {

                        mang[k] = 0;}

                    k++;

                } while (!fromServer.equals("K"));

                System.out.println("");

                int dem = 0, tongNT = 0;

                System.out.println("Các số nguyên tố là:");

                for (int i = 0; i < k; i++) {

                    if (mang[i] != 0 && mang[i] != 1) {

                        for (int j = 2; j < mang[i]; j++) {

                            if (mang[i] % j == 0) {

                                dem++;}}

                        if (dem == 0) {

                            System.out.print(mang[i] + " ");

                            tongNT += mang[i];

                        } else {

                            dem = 0;}}}

                System.out.println("");

                System.out.println("Tổng các số nguyên tố nhận từ Server: " + tongNT);

                in.close();}

            cs.close();

        } catch (Exception e) {

            System.err.println("Không kết nối được với Server! " + e);}}}

Bạn đang đọc truyện trên: Truyen247.Pro

Tags: