Kafedra: tbtavadt "Tarmoqni dasturlash asoslari" fani bo’yicha



Download 2,7 Mb.
bet6/8
Sana29.05.2022
Hajmi2,7 Mb.
#617429
1   2   3   4   5   6   7   8
Bog'liq
TDA Laboratoriya 1-8.

next.html fayli:



html>



Next /title> <br />/head> <br /><body> <br /><h2>Bu faylda hech qanday ma'lumot yo'q /h1> <br /><a href="index.html">Asosiy sahifaga o'tish /a> <br />/body> <br />/html> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /><b>O’ZBEKISTON RESPUBLIKASI AXBOROT TEXNOLOGIYALARI VA KOMUNIKATSIYALARINI RIVOJLANTIRISH VAZIRLIGI</b> <br /><h2> </h2> <p>Kafedra: TBTAvaDT</p> <br />“Tarmoqni dasturlash asoslari” fani bo’yicha <br /> <br /><b>6-Laboratoriya ishi</b> <br />Mavzu: <b>Fayllarni</b> <b>ko’chiruvchi</b> <b>tarmoq</b> <b>dasturni</b> <b>yaratish.</b> <br /> <br /> <br /><b>.</b> <p> <b> 004/L1– guruh talabasi</b> </p> <br /><b>Bajardi: Sobirov Ibrohim </b> <br /><b>Tekshirdi: Akmuradov B</b> <br /> <br /> <br /><b>Ishdan</b> <b>maqsad: </b>Java dasturlash tili java.net.* va java.io.* paketlari klasslaridan foydalanib fayllarni ko’chirish tarmoq dasturini yaratish. <br /><span id='Dastur_kodi'><h2>Dastur kodi:</span></h2> <p>FTPServer klassi:</p> <br />package lab6; <p>import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File;</p> <br />import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import java.nio.file.Files; import java.util.Arrays; import java.util.Optional; <p>public class FTPServer {</p> <br />public static void main(String[] args) throws IOException { ServerSocket server = new ServerSocket(8080); System.<i>out</i>.println("Server started"); <br />File folder = new File("/home/ik/Projects/tda_lab_IK/folder_server"); <p>while (true) {</p> <br />Socket soc = server.accept(); <br />DataOutputStream dos = new DataOutputStream(soc.getOutputStream()); DataInputStream dis = new DataInputStream(soc.getInputStream()); <p>System.<i>out</i>.println("New session"); int act;</p> <br />String emptyMsg = "Folder is empty!"; <br /><i>printMenu</i>(dos); <p>do {</p> <br />File[] files = folder.listFiles(); if (files = null) { <br />dos.writeUTF("Internal server error"); break; <br />} <br />dos.writeUTF("Enter menu number in range [0-3]"); act = dis.readInt(); <br />String res = ""; switch (act) { <br />case 0: { <br />res = "Session ended"; System.<i>out</i>.println(res); <br />} <br />break; <p>case 1: {</p> <br />res = Arrays.<i>stream</i>(files) <br /> .map(File getName) <br />.reduce((s1, s2) > s1 + "\n" + s2) <br />.orElse(emptyMsg); System.<i>out</i>.println("Files list:\n" + res); <br />} <br />break; <p>case 2: {</p> <br />if (files.length = 0) { res = emptyMsg; <br />} else { <br />Optional<File> opt = Optional.<i>empty</i>(); <br />dos.writeUTF("Enter existing filename"); do { <br />String filename = dis.readUTF(); if (filename.equals("!")) { <br />res = "Cancelled"; dos.writeInt(1); break; <br />} <br />opt = Arrays.<i>stream</i>(files) <br />.filter(file > file.getName().equals(filename)) <br />.findFirst(); dos.writeInt(opt.isEmpty() ? 0 : 2); <br />} while (opt.isEmpty()); <p>if (opt.isPresent()) {</p> <br />byte[] bytes = Files.<i>readAllBytes</i>(opt.get().toPath()); dos.writeInt(bytes.length); <br />dos.write(bytes); <br />res = "Downloaded file: " + opt.get().getName(); <br />} <br />} <br />System.<i>out</i>.println("Download > " + res); <br />} <br />break; <p>case 3: {</p> <br />dos.writeUTF("Select your file"); String filename = dis.readUTF(); String originalName = filename; while (!filename.equals("!") & <br />new File(folder, filename).exists() <br />) { <br />dos.writeInt(0); <br />dos.writeUTF(filename + " exists. Dou you:\n" + "c: cancel\n" + <br />"r: rename\n" + <br />"s: select other file"); String s = dis.readUTF(); <br />if (s.equalsIgnoreCase("c")) { res = "Cancelled"; <br />break; <br />} <br />filename = dis.readUTF(); <br />if (s.equalsIgnoreCase("s")) { originalName = filename; <br />} <br />} <br />if (filename.equals("!")) { res = "Cancelled"; dos.writeInt(1); <br />} else if (!new File(folder, filename).exists()) { dos.writeInt(2); <br />int len = dis.readInt(); byte[] data = new byte[len]; dis.read(data); <br />Files.<i>write</i>(folder.toPath().resolve(filename), data); res = originalName + " uploaded as " + filename; <br /> <p>} <br />} <br />}</p> <br />if (filename.equals("!")) { res = "Cancelled"; dos.writeInt(1); <br />} else if (!new File(folder, filename).exists()) { dos.writeInt(2); <br />int len = dis.readInt(); byte[] data = new byte[len]; dis.read(data); <br />Files.<i>write</i>(folder.toPath().resolve(filename), data); res = originalName + " uploaded as " + filename; <br />} <p>System.<i>out</i>.println("Upload > " + res);</p> <br />} <br />break; default: { <br /><i>printMenu</i>(dos); continue; <br />} <br />} <br />dos.writeUTF(res); <br />System.<i>out</i>.println(" "); <br />} while (act = 0); <br />} <br />} <p>static void printMenu(DataOutputStream dos) throws IOException { dos.writeUTF("Menu:\n" +</p> <p>"0 > End session\n" + <br />"1 > List all files\n" + <br />"2 > Download file\n" + <br />"3 > Upload file\n" + "Other > Menu" <br />); <br />} <br />} <br /> </p> <p>FTPClient klassi:</p> <br />package lab6; <p>import java.io.DataInputStream; import java.io.DataOutputStream; import java.net.Socket;</p> <br />import java.nio.file.Files; import java.nio.file.Path; import java.util.List; import java.util.Scanner; <br />import java.util.stream.Collectors; <p>public class FTPClient {</p> <br />public static void main(String[] args) throws Exception { Scanner in = new Scanner(System.<i>in</i>); <p>Socket soc = new Socket("localhost", 8080);</p> <br />DataOutputStream dos = new DataOutputStream(soc.getOutputStream()); DataInputStream dis = new DataInputStream(soc.getInputStream()); <p>int act;</p> <br />Path folder = Path.<i>of</i>("/home/ik/Projects/tda_lab_IK/folder_client"); <p>System.<i>out</i>.println("Client started"); System.<i>out</i>.println(dis.readUTF());</p> <p>do { System.<i>out</i>.println(dis.readUTF()); act = in.nextInt(); dos.writeInt(act); </p> <p>switch (act) { case 2: {</p> <br />System.<i>out</i>.println(dis.readUTF()); String filename = in.next(); dos.writeUTF(filename); <br />int code; <p>while ((code = dis.readInt()) = 0) { System.<i>out</i>.println("File not found"); filename = in.next(); dos.writeUTF(filename);</p> <br />} <br />if (code = 2) { <br />int len = dis.readInt(); byte[] data = new byte[len]; dis.read(data); <br />Files.<i>write</i>(folder.resolve(filename), data); <br />} <br />} <br />break; case 3: { <br />System.<i>out</i>.println(dis.readUTF()); <br />List <br />list = Files.<i>list</i>(folder).collect(Collectors.<i>toList</i>()); if (list.isEmpty()) { <br />System.<i>out</i>.println("Empty client folder"); dos.writeUTF("!"); <br />} else { <br />for (int i = 0; i < list.size(); i +) { <br />System.<i>out</i>.println(i + "." + list.get(i).getFileName()); <br />} <br />int k; do { <br />System.<i>out</i>.print("Enter available file number: "); k = in.nextInt(); <br />} while (k = list.size()); dos.writeUTF(list.get(k).getFileName().toString()); <p>int code;</p> <br />while ((code = dis.readInt()) = 0) { System.<i>out</i>.println(dis.readUTF()); String s = in.next(); dos.writeUTF(s); <br />if (s.equalsIgnoreCase("c")) { break; <br />} <br />if (s.equalsIgnoreCase("s")) { <br />for (int i = 0; i < list.size(); i +) { <br />System.<i>out</i>.println(i + "." + list.get(i).getFileName()); <br />} <br />do { <br />System.<i>out</i>.print("Enter available file number: "); k = in.nextInt(); <br />} while (k = list.size()); dos.writeUTF(list.get(k).getFileName().toString()); <br />} else { <br />dos.writeUTF(in.next()); <br />} <br />} <br />if (code = 2) { <br />byte[] bytes = Files.<i>readAllBytes</i>(list.get(k)); dos.writeInt(bytes.length); <br />dos.write(bytes); <br />} <br />} <br />} <br />break; <br />} <br />System.<i>out</i>.println(dis.readUTF()); System.<i>out</i>.println(" "); <br />} while (act = 0); <br />} <br />} <br /> <br /> <br /> <br /></file></a></body>
Download 2,7 Mb.

Do'stlaringiz bilan baham:
1   2   3   4   5   6   7   8




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish