[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
Trang 1 trong tổng số 1 trang
[Java]Chương trình Client-Server sử dụng Socket ở chế độ có nối kết TCP/IP Đề tài LIST Tenthumuc
Đề:
Phần Code:
Chương trình Server:
Class luồng Server:
Class luồng Client:
-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 ý.
- 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.
- 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.
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();
}
}
}
}
- 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();
}
}
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();
}
}
}
}
- 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();
}
}
}
}
- 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 ý.
Re: [Java]Chương trình Client-Server sử dụng Socket ở chế độ có nối kết TCP/IP Đề tài LIST Tenthumuc
- Code:
File folder;
File[] listOfFiles;
shippou777- Posts : 460
Thanked : 8
11/10/2011
Tài Sản
Thú nuôi:
Re: [Java]Chương trình Client-Server sử dụng Socket ở chế độ có nối kết TCP/IP Đề tài LIST Tenthumuc
- Code:
File folder;
File[] listOfFiles;
- VD:
- Code:
String path="D:/HK1 2012-2013/Lap trinh truyen thong/Demo/Thu muc/";
- Code:
folder = new File(path);
- 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.
Re: [Java]Chương trình Client-Server sử dụng Socket ở chế độ có nối kết TCP/IP Đề tài LIST Tenthumuc
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.
Hồi đó tui làm Kruskal.
shippou777- Posts : 460
Thanked : 8
11/10/2011
Tài Sản
Thú nuôi:
Similar topics
» [Java] trao đổi giữa 2 chương trình bằng Socket. Đề tài đọc số từ Client gởi lên Server
» [Java] trao đổi giữa 2 chương trình bằng Socket. Đề tài thực hiện phép toàn Operant1 OP Operant2
» [Java]Bài tập về nhà từ điển Anh-Việt trao đổi thông tin giữa 2 luồng trong 1 chường trình bằng ống dẫn Pipe
» Chương trình tính N!
» Tổng hợp các chương trình tạo máy ảo.
» [Java] trao đổi giữa 2 chương trình bằng Socket. Đề tài thực hiện phép toàn Operant1 OP Operant2
» [Java]Bài tập về nhà từ điển Anh-Việt trao đổi thông tin giữa 2 luồng trong 1 chường trình bằng ống dẫn Pipe
» Chương trình tính N!
» Tổng hợp các chương trình tạo máy ảo.
Trang 1 trong tổng số 1 trang
Permissions in this forum:
Bạn không có quyền trả lời bài viết
|
|