Package ftp; import java net



Yüklə 18,03 Kb.
tarix03.01.2023
ölçüsü18,03 Kb.
#78308
paclkab;gnzxm,e ftp


package ftp;

import java.net.*;
import java.io.*;

class FTPClient {


public static void main(String args[]) throws Exception {
Socket soc = new Socket("127.0.0.1", 8888);
transferfileClient t = new transferfileClient(soc);
t.displayMenu();

}
}


class transferfileClient {
Socket ClientSoc;
DataInputStream din;
DataOutputStream dout;
BufferedReader br;

transferfileClient(Socket soc) {


try {
ClientSoc = soc;
din = new DataInputStream(ClientSoc.getInputStream());
dout = new DataOutputStream(ClientSoc.getOutputStream());
br = new BufferedReader(new InputStreamReader(System.in));
} catch (Exception ex) {
}
}

void SendFile() throws Exception {


String filename;
System.out.print("Fayl nomini kiriting :");
filename = br.readLine();

File f = new File(filename);


if (!f.exists()) {
System.out.println("Fayl mavjud emas...");
dout.writeUTF("Fayl topilmadi");
return;
}
dout.writeUTF(filename);
String msgFromServer = din.readUTF();
if (msgFromServer.compareTo("Fayl avvaldan mavjud") == 0) {
String Option;
System.out.println("Fayl avvaldan mavjud. Qayta yozmoqchimisiz (Y/N) ?");
Option = br.readLine();
if (Option == "Y") {
dout.writeUTF("Y");
} else {
dout.writeUTF("N");
return;
}
}
System.out.println("Fayl jo‘natilmoqda ...");
FileInputStream fin = new FileInputStream(f);
int ch;
do {
ch = fin.read();
dout.writeUTF(String.valueOf(ch));
}
while (ch != -1);

fin.close();


System.out.println(din.readUTF());
}

void ReceiveFile() throws Exception {


String fileName;
System.out.print("Fayl nomini kiriting :");
fileName = br.readLine();
dout.writeUTF(fileName);
String msgFromServer = din.readUTF();

if (msgFromServer.compareTo("Fayl topilmadi") == 0) {


System.out.println("Fayl serverda topilmadi ...");
return;
} else if (msgFromServer.compareTo("TAYYOR") == 0) {
System.out.println("Faylni qabul qilish ...");
File f = new File(fileName);
if (f.exists()) {
String Option;
System.out.println("Fayl avvaldan mavjud. Qayta yozmoqchimisiz (Y/N) ?");
Option = br.readLine();
if (Option == "N") {
dout.flush();
return;
}
}
FileOutputStream fout = new FileOutputStream(f);
int ch;
String temp;
do {
temp = din.readUTF();
ch = Integer.parseInt(temp);
if (ch != -1) {
fout.write(ch);
}
} while (ch != -1);
fout.close();
System.out.println(din.readUTF());
}
}

public void displayMenu() throws Exception {


while (true)

{
System.out.println("[ MENU ]");


System.out.println("1. Fayl jo‘natish");
System.out.println("2. Fayl qabul qilish");
System.out.println("3. Chiqish");
System.out.print("\nTanlovni kiriting :");
int choice;
choice = Integer.parseInt(br.readLine());
if (choice == 1) {
dout.writeUTF("SEND");
SendFile();
} else if (choice == 2) {
dout.writeUTF("GET");
ReceiveFile();
} else {
dout.writeUTF("DISCONNECT");
System.exit(1);
}
}
}
}

package ftp;

import java.net.*;
import java.io.*;

public class FTP_server {


public static void main(String args[]) throws Exception
{
ServerSocket soc=new ServerSocket(8888);
System.out.println("FTP Server 8888 Port raqamida ishga tushdi");
while (true)
{
System.out.println("Bog'lanishni kutish...");
transferfile t = new transferfile(soc.accept());
}
}
}

class transferfile extends Thread {


Socket ClientSoc;

DataInputStream din;


DataOutputStream dout;

transferfile(Socket soc) {


try {
ClientSoc = soc;
din = new DataInputStream(ClientSoc.getInputStream());
dout = new DataOutputStream(ClientSoc.getOutputStream());
System.out.println("FTP klient bog'landi...");
start();
} catch (Exception ex) {
}
}

void SendFile() throws Exception {


String filename = din.readUTF();
File f = new File(filename);
if (!f.exists()) {
dout.writeUTF("Fayl topilmadi");
return;
} else {
dout.writeUTF("TAYYOR");
FileInputStream fin = new FileInputStream(f);
int ch;
do {
ch = fin.read();
dout.writeUTF(String.valueOf(ch));
}
while (ch != -1);
fin.close();
dout.writeUTF("Fayl muvaffaqiyatli qabul qilindi");
}
}

void ReceiveFile() throws Exception {


String filename = din.readUTF();
if (filename.compareTo("Fayl topilmadi") == 0) {
return;
}
File f = new File(filename);
String option;

if (f.exists()) {


dout.writeUTF("Fayl avvaldan mavjud");
option = din.readUTF();
} else {
dout.writeUTF("SendFile");
option = "Y";
}
if (option.compareTo("Y") == 0) {
FileOutputStream fout = new FileOutputStream(f);
int ch;
String temp = "";
do {
temp=din.readUTF();
ch = Integer.parseInt(temp);
if (ch != -1) {
fout.write(ch);
}
}
while (ch != -1);
fout.close();
dout.writeUTF("Fayl muvaffaqiyatli jo'natildi");
} else {
return;
}
}

public void run() {


while (true) {
try {
System.out.println("Buyruqni kutish...");
String Command = din.readUTF();
if (Command.compareTo("GET") == 0) {
System.out.println("\tGET buyrug'i qabul qilindi...");
SendFile();
} else if (Command.compareTo("SEND") == 0) {
System.out.println("\tSEND buyrug'i qabul qilindi...");

ReceiveFile();


} else if (Command.compareTo("DISCONNECT") == 0) {
System.out.println("\tDisconnect buyrug'i qabul qilindi...");
System.exit(1);
}
} catch (Exception ex) {
}
}
}
}
Yüklə 18,03 Kb.

Dostları ilə paylaş:




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin