k4info
Bạn có muốn phản ứng với tin nhắn này? Vui lòng đăng ký diễn đàn trong một vài cú nhấp chuột hoặc đăng nhập để tiếp tục.

[Java]Chương trình Client-Server sử dụng Socket ở chế độ có nối kết TCP/IP Đề tài LIST Tenthumuc

2 posters

Go down

funy [Java]Chương trình Client-Server sử dụng Socket ở chế độ có nối kết TCP/IP Đề tài LIST Tenthumuc

Bài gửi by Admin Sat Oct 20, 2012 12:16 pm

Đề:
Code:

 Viết chương trình theo mô hình Client-Server sử dụng Socket ở chế độ có nối kết.  2
Trong đó:
o  Server sẽ nhận từ Client câu lệnh dạng sau:  “LIST  Tênthưmục”
Server sẽ gửi cho Client danh sách các file và thư mục con hiện có trong thư mục
đó, nếu thư mục tồn tại.
o  Client sẽ cho phép người dùng nhập câu lệnh, gửi qua Server, nhận kết quả từ
Server và hiển thị ra màn hình.
Phần chương trình:
Code:

- Phần thư mục cần kiểm tra là nằm ở Server
- Có thể đổi đường dẫn mặc định ở Server tại dòng: String path="D:/HK1 2012-2013/Lap trinh truyen thong/Demo/Thu muc/"; Đây là đường dẫn mặc định của tôi.
Path:
[Java]Chương trình  Client-Server sử dụng Socket ở chế độ có nối kết TCP/IP Đề tài LIST Tenthumuc 20-10-10
Phần Code:
Chương trình Server:
Class luồng Server:
Code:

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;


public class ServerFolder extends Thread{

   ServerSocket ss;
   Socket s;
   int port =7;
   Scanner input;
   PrintWriter output;
   String path="D:/HK1 2012-2013/Lap trinh truyen thong/Demo/Thu muc/";
   String fname;
   String fname2="";
   String s1[];
   String list[];
   String error1;
   File folder;
   File[] listOfFiles;
   int numfile;
   int flag =0;
   public ServerFolder()
   {
      try {
         ss = new ServerSocket(this.port);
         output = new PrintWriter(System.out);
         output.println("Server khởi tạo thành công!");
         output.flush();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
   public ServerFolder(int p)
   {
      try {
         this.port =p;
         ss = new ServerSocket(this.port);
         output = new PrintWriter(System.out);
         output.println("Server khởi tạo thành công!");
         output.flush();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
   public void Recived()
   {
      try {
         input = new Scanner(s.getInputStream());
         fname = input.nextLine();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
   public void Procesing()
   {
      s1 = new String[100];
      s1 = fname.split(" ");
      if((s1[0].equals("LIST")) || (s1[0].equals("list")) || (s1[0].equals("List"))) {
                  path=path+s1[1];
               try {
                  list = new String[200];
                  folder = new File(path);
                  listOfFiles = folder.listFiles();
                  numfile = listOfFiles.length;                   
                  for (int i = 0; i < numfile; i++)
                     {
                        if (listOfFiles[i].isFile()) { list[i] = listOfFiles[i].getName(); }
                     }
                  for (int j = 0; j < numfile; j++)
                     {
                     fname2=list[j]+","+fname2;   
                     }
                  } catch (Exception e)
                  {
                     error1 = "Thư mục không tồn tại!";
                     flag =1;
                  }
               }
      else {
               error1 = "Lệnh sai hoặc lệnh Server không hỗ trợ";
               flag =1;
          }
      if(flag ==1) fname2 = flag+","+error1;
      else fname2 = flag+","+fname2;
   }
   public void Send()
   {
         try {
            output = new PrintWriter(s.getOutputStream());
            output.println(fname2);
            output.flush();
         } catch (IOException e) {
            e.printStackTrace();
         }   
   }
   public void run()
   {
      while(true)
      {
         try {
            s=ss.accept();
            Recived();
            Procesing();
            Send();
         } catch (IOException e) {
            e.printStackTrace();
         }
      }
   }
}

Hàm Main Server:
Code:

import java.io.PrintWriter;


public class Main {


   public static void main(String[] args) {
      ServerFolder S = new ServerFolder(8080);
      PrintWriter output = new PrintWriter(System.out);
      output.println("Server đã chạy và chờ kết nối từ Client...");
      S.start();
   }

}

Chương trình Client:
Class luồng Client:
Code:
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;


public class ClientFolder extends Thread {
   Socket s;
   String address;
   int port =7;
   Scanner input;
   PrintWriter output;
   String s1;
   String s2="";
   String list[];
   boolean connected = false;
   public ClientFolder() {}
   public ClientFolder(String ad, int p)
   {
      this.address = ad;
      this.port =p;
      output = new PrintWriter(System.out);
      output.println("Client được khởi tạo!");
      output.flush();
   }
   public void connected()
   {
      try {
         this.s = new Socket(this.address, this.port);
         this.connected = true;
      } catch (UnknownHostException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
   public void disconnect()
   {
      try {
         this.s.close();
         this.connected = false;
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
   public  String InputString()
   {
      input = new Scanner(System.in);
      output = new PrintWriter(System.out);
      output.println("Lưu ý đường dẫn đã được mặc định tại Server là:");
      output.flush();
      output.println("D:/HK1 2012-2013/Lap trinh truyen thong/Demo/Thu muc/");
      output.flush();
      output.print("Nhập lệnh và thư mục[LENH_TENTHUMUC] hoặc @ để thoát:");
      output.flush();
      s1 = input.nextLine();
      return s1;
   }
   public void Recived()
   {
      try {
         input = new Scanner(s.getInputStream());
         s2= input.nextLine();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
   
   public void Send()
   {
      try {
         output = new PrintWriter(s.getOutputStream());
         output.println(s1);
         output.flush();
      } catch (IOException e) {
         e.printStackTrace();
      }
      
   }
   public void Look()
   {
      output = new PrintWriter(System.out);
      list = new String[200];
      list = s2.split(",");
      int a = Integer.valueOf(list[0]).intValue();
      if(a==1)
      {
         output.println(list[1]);
         output.flush();
      }
      else
      {
         output.println("Các file có trong thu mục là:");
         output.flush();   
         for(int i =1; i< list.length; i++)
         {
            output.println(list[i]);
            output.flush();
         }
      }
   }
}
Hàm main Client
Code:
import java.io.PrintWriter;
import java.util.Scanner;


public class MainClient {

   /**
    * @param args
    */
   public static void main(String[] args) {
      // TODO Auto-generated method stub
      String address = "10.0.0.177";
      int port=7;
      Scanner input = new Scanner(System.in);
      PrintWriter output = new PrintWriter(System.out);
      output.print("Nhập địa chỉ máy chủ:");
      output.flush();
      address = input.nextLine();
      output.print("Nhập địa cổng port:");
      output.flush();
      port = input.nextInt();
      ClientFolder c = new ClientFolder(address,port);
      while(true)
      {
         c.connected();
         if(c.connected)
         {
            if(c.InputString().equals("@"))
            {
               output.println("Ngắt kết nối Server! Kết thúc chương trình!");
               break;
            }
            else
            {
               c.Send();
               c.Recived();
               c.Look();
               c.disconnect();
            }
         }
         else
         {
            output.print("Kết nối Server thất bại! ");
            output.flush();
         }
      }
   }

}
Test code:
[Java]Chương trình  Client-Server sử dụng Socket ở chế độ có nối kết TCP/IP Đề tài LIST Tenthumuc 20-10-11
[Java]Chương trình  Client-Server sử dụng Socket ở chế độ có nối kết TCP/IP Đề tài LIST Tenthumuc 20-10-12
-Lưu ý cho anh em:
- Nếu mình không xem TCP/IPv4 của mình mình có thể gõ trực tiếp vào là localhost giống như mình.
- Mỗi người 1 cách anh em có thắt mắc chỗ nào có thể comment tại đây. Chương trình có thê còn lỗi mong anh em góp ý.


[You must be registered and logged in to see this link.]
Admin
Admin

Posts : 1013
Thanked : 47
Gia Nhập 25/08/2010

Tài Sản
Thú nuôi:

https://k4info.forumvi.com

Về Đầu Trang Go down

funy Re: [Java]Chương trình Client-Server sử dụng Socket ở chế độ có nối kết TCP/IP Đề tài LIST Tenthumuc

Bài gửi by shippou777 Thu Nov 08, 2012 11:24 am

Code:

File folder;
File[] listOfFiles;
2 đối tượng File, File[] đó dùng sao bác oi....
avatar
shippou777

Posts : 460
Thanked : 8
Gia Nhập 11/10/2011

Tài Sản
Thú nuôi:

Về Đầu Trang Go down

funy Re: [Java]Chương trình Client-Server sử dụng Socket ở chế độ có nối kết TCP/IP Đề tài LIST Tenthumuc

Bài gửi by Admin Thu Nov 08, 2012 10:46 pm

Code:
File folder;
File[] listOfFiles;
- File folder là 1 biến kiểu File, biến này có nhiêm vụ đọc hết tất cả các file có trong thư mục đường dẫn mình dẫn đến
- VD:
Code:
String path="D:/HK1 2012-2013/Lap trinh truyen thong/Demo/Thu muc/";
- Khi đó ta cần đặt biến folder kiểu File với đường dẫn để nó đọc toàn bộ file có trong đường dẫn
Code:
folder = new File(path);
- File[] listOfFiles; 1 mảng kiểu File dùng để lưu lại tên file lẫn dịnh dạng file.
- Cách sử dụng kết hợp với biến folder kiểu File như sau listOfFiles = folder.listFiles();
- Sau đó ta có thể truy xuất từng tên file lẫn định dạng file bình thường vì đã lưu vào mảng listOfFiles. Và để biết được số phần tử của mảng file( số file có trong 1 thư mục) ta dùng listOfFiles.lenght(); là ra
-OK xong

P/S: Bác Cường em vừa cho 1 người nick của bác muốn liên hệ xin thuật toán Prim gì đó của bác.
Admin
Admin

Posts : 1013
Thanked : 47
Gia Nhập 25/08/2010

Tài Sản
Thú nuôi:

https://k4info.forumvi.com

Về Đầu Trang Go down

funy Re: [Java]Chương trình Client-Server sử dụng Socket ở chế độ có nối kết TCP/IP Đề tài LIST Tenthumuc

Bài gửi by shippou777 Thu Nov 08, 2012 11:15 pm

Prim hình như hồi xưa chú Đức làm của ông Long đc 10đ đó. Hỏi xin nó đi.
Hồi đó tui làm Kruskal.
avatar
shippou777

Posts : 460
Thanked : 8
Gia Nhập 11/10/2011

Tài Sản
Thú nuôi:

Về Đầu Trang Go down

funy Re: [Java]Chương trình Client-Server sử dụng Socket ở chế độ có nối kết TCP/IP Đề tài LIST Tenthumuc

Bài gửi by Sponsored content


Sponsored content


Về Đầu Trang Go down

Về Đầu Trang

- Similar topics

 
Permissions in this forum:
Bạn không có quyền trả lời bài viết