Amaliy qism - Tbta va dt kafedrasi

|
Tbta va dt kafedrasi
|
bet | 9/10 | Sana | 19.05.2024 | Hajmi | 1,34 Mb. | | #243589 |
Bog'liq TFTPAmaliy qism
Java dasturlash tilida TFTP protokoli asosida tarmoq dasturi:
Client(back end):
TFTPUtils.puts("DATA received, sending ACK packet");
packet_ack.createACK(packet_data.getPacketNumber());
packet_ack.sendPacket(out);
if (packet_data.getSize() < 4 + TFTPPacket.TFTP_PACKET_DATA_SIZE) {
TFTPUtils.puts("File transferred");
break; }
} else if (packet_data.isError()) {
try {
TFTPUtils.puts("Error packet received with message: " + packet_data.getString(4, packet_data.getSize()));
} catch (Exception e) {
}
disconnect();
} else {
TFTPUtils.puts("Unexpected packet");
disconnect();
break mainloop;
}
}
} catch (IOException ioException) {
TFTPUtils.puts("IO Exception in thread: " + ioException.getMessage());
}
return true;
}
public void disconnect() {
TFTPUtils.puts("Disconnected from server");
try {
server_socket.close();
out.close();
in.close();
fin.close();
fout.close();
} catch (Exception e) { } }}
Client(UI qismi):
package tftpclient;
import tftp.TFTPUtils;
import jargs.gnu.CmdLineParser;
public class TFTPClientUI {
private static void printUsage() {
System.err.println(
"\n" +
"Usage: TFTP -o host [-u] -s source [-d destination] [-p port] [-h]\n" +
" -h --help Prints this help" +
" -o --open Specifies the server ip address" +
" -u --upload Upload file to server" +
" -s --source Specifies source file name" +
" -d --destination Specifies destination file name" +
" -p --port Specifies the port number to be used\n");
}
public static void main(String[] args) {
CmdLineParser parser = new CmdLineParser();
CmdLineParser.Option opt_help = parser.addBooleanOption('h', "help");
CmdLineParser.Option opt_host = parser.addStringOption('o', "open");
CmdLineParser.Option opt_upload = parser.addBooleanOption('u', "upload");
CmdLineParser.Option opt_source = parser.addStringOption('s', "source");
CmdLineParser.Option opt_dest = parser.addStringOption('d', "destination");
CmdLineParser.Option opt_port = parser.addIntegerOption('p', "port");
try {
parser.parse(args);
}
catch (CmdLineParser.OptionException e) {
System.err.println(e.getMessage());
printUsage();
System.exit(2);
}
Boolean help = (Boolean)parser.getOptionValue(opt_help, Boolean.FALSE);
if (help) {
printUsage();
System.exit(2);
}
String source = (String)parser.getOptionValue(opt_source, new String(""));
if (source == "") {
printUsage();
System.exit(2);
}
String dest = (String)parser.getOptionValue(opt_dest, new String(source));
String host = (String)parser.getOptionValue(opt_host, new String("127.0.0.1"));
int port = (Integer)parser.getOptionValue(opt_port, new Integer(5555));
Boolean upload = (Boolean)parser.getOptionValue(opt_upload, Boolean.FALSE);
TFTPClient client = new TFTPClient(port, host);
if (upload) {
TFTPUtils.puts("Sending file to server: " + source);
client.sendFile(source, dest);
} else {
TFTPUtils.puts("Getting file from server: " + source);
client.getFile(source, dest); } } }
Server:
package tftpserver;
import java.net.*;
import java.util.Hashtable;
import java.io.*;
import tftp.*;
public class TFTPServer {
static final int MAX_CONNECTIONS = 10;
protected String ftproot;
protected int port;
protected ServerSocket serverSocket;
protected Socket clientSocket = null;
protected Hashtable clients;
public TFTPServer(int port, String ftproot) {
//- initialize settings
this.port = port;
this.ftproot = ftproot;
clients = new Hashtable();
try {
serverSocket = new ServerSocket(port);
} catch (IOException e) {
TFTPUtils.fatalError("Could not listen on port: " + port);
}
TFTPUtils.puts("Server successfully started. Listening on port " + port + " started");
while (true) {
try {
clientSocket = serverSocket.accept();
} catch (Exception e) {
TFTPUtils.fatalError("An exeption was thrown while accepting a client connection");
}
if (clients.size() == MAX_CONNECTIONS) {
TFTPPacket packet_error = new TFTPPacket();
packet_error.createError(0, "Server is full, please try again later");
try {
packet_error.sendPacket(new BufferedOutputStream(clientSocket.getOutputStream()));
} catch (Exception e) {
TFTPUtils.puts("Unable to send error message to client"); }
} else {
clients.put(clientSocket, new TFTPServerClient(this, clientSocket)); }
} }
public void removeConnection(Socket clientSocket) {
TFTPUtils.puts("Removing client connection " + clientSocket.getInetAddress().getHostAddress());
if (clients.get(clientSocket) != null) {
try {
clients.get(clientSocket).fin.close();
} catch (Exception ie) {}
try {
clients.get(clientSocket).fout.close();
} catch (Exception ie) {}
try {
clients.get(clientSocket).in.close();
} catch (Exception ie) {}
try {
clients.get(clientSocket).out.close();
} catch (Exception ie) {}
try {
clients.get(clientSocket).interrupt();
} catch (Exception ie) {}
}
try {
clientSocket.close();
} catch (Exception ie) {}
try {
clients.remove(clientSocket);
} catch (Exception ie) {}
}
public void shutdown() {
try {
serverSocket.close();
} catch (IOException e) { } }}
Client-Server:
package tftpserver;
import java.io.*;
import java.net.*;
import tftp.TFTPPacket;
import tftp.TFTPUtils;
public class TFTPServerClient extends Thread {
public enum TClientRequest { WRQ, RRQ, UNDEFINED };
public TClientRequest request;
protected TFTPServer server;
public Socket socket;
public TFTPPacket last_packet;
public BufferedOutputStream out;
public BufferedInputStream in;
public DataOutputStream fout;
public DataInputStream fin;
public TFTPServerClient(TFTPServer server, Socket socket) {
this.server = server;
this.socket = socket;
last_packet = new TFTPPacket();
TFTPUtils.clientMessage(this, "Client connected, waiting for request...");
start(); }
public void run () {
try {
in = new BufferedInputStream(socket.getInputStream());
out = new BufferedOutputStream(socket.getOutputStream());
last_packet.getPacket(in);
if (last_packet.isWRQ()) {
TFTPUtils.clientMessage(this, "Write request received");
this.request = TClientRequest.WRQ;
File file_exists = null;
try {
file_exists = new File(server.ftproot + last_packet.getString(2, last_packet.getSize()));
} catch (Exception e) {
TFTPUtils.fatalError(e.getMessage());
}
if (file_exists != null && file_exists.exists()) {
try {
TFTPUtils.clientMessage(this, "PUT failed. File already exists on server: " + server.ftproot + last_packet.getString(2, last_packet.getSize()));
} catch (Exception e) {}
//- inform client about the error
TFTPPacket packet_error = new TFTPPacket();
packet_error.createError(6, TFTPPacket.TFTP_ERROR_6);
packet_error.sendPacket(out);
server.removeConnection(this.socket);
} else {
try {
fout = new DataOutputStream(
new FileOutputStream(server.ftproot + last_packet.getString(2, last_packet.getSize()))
);
} catch (Exception e) {
TFTPUtils.clientMessage(this, "Unable to open file for writing");
//- inform client about the error
TFTPPacket packet_error = new TFTPPacket();
packet_error.createError(0, "Internal file access error");
packet_error.sendPacket(out);
server.removeConnection(this.socket); }
TFTPUtils.clientMessage(this, "Starting PUT transfer");
TFTPPacket packet_ack = new TFTPPacket();}
try {
// wait for ack
packet_data = new TFTPPacket();
packet_ack = new TFTPPacket();
packet_no = 0;
mainloop:
while (true) {
TFTPUtils.clientMessage(this, "Sending DATA packet");
for (int i = 0; i < TFTPPacket.TFTP_PACKET_DATA_SIZE; i++) data[i] = 0;
bytes_read = 0;
try {
bytes_read = fin.read(data, 0, TFTPPacket.TFTP_PACKET_DATA_SIZE);
if (bytes_read > 0) {
packet_no++;
packet_data.createData(packet_no, data, bytes_read);
packet_data.sendPacket(out);
TFTPUtils.clientMessage(this, "Data packet sent, waiting ACK for packet no " + packet_no);
}
//wait for acknoledgement
packet_ack.getPacket(in);
if (packet_ack.isACK()) {
TFTPUtils.clientMessage(this, "ACK received for packet no" + packet_ack.getPacketNumber());
continue; }
if (bytes_read < TFTPPacket.TFTP_PACKET_DATA_SIZE) {
//- this is our last packet
TFTPUtils.clientMessage(this, "File was successfully sent!");
server.removeConnection(socket);
break mainloop;
}
} catch (Exception e) {
TFTPUtils.clientMessage(this, "Exception in sending file to client");
} }
} catch (Exception ioException) {
TFTPUtils.clientMessage(this, "IO Exception in thread: " + ioException.getMessage()); } }
} catch( EOFException ie ) {
} catch( IOException ie ) {
ie.printStackTrace();
} finally { server.removeConnection(socket); } }}
Server(UI):
package tftpserver;
import java.io.File;
import tftp.*;
public class TFTPServerUI {
/**
* @param args
*/
public static void main(String[] args) {
TFTPServer server;
File current_file = new File(".");
TFTPUtils.puts("Starting server on port 5555");
try {
server = new TFTPServer(5555, current_file.getCanonicalPath() + "\\ftproot\\");
server.shutdown();
} catch (Exception e) {
TFTPUtils.puts("Unhandled exception was thrown"); } }}
Xulosa
Zamonaviy sharoitda telekommunikatsiya sohasini innovatsion rivojlantirish masalalarini yechishga qaratilgan kompleks yondashuv bilimli va tajribali mutaxassislarni talab qiladi. Sohani innovatsion rivojlantirish yo‘liga o‘tishda mutaxassislarni tayyorlashda yangicha yondashuvlarga majbur qiladi. Telekommunikatsiya tarmog‘ini innovatsion rivojlantirish uchun ko‘p mutaxassislarni tarmoq texnologiyalari, tarmoq yechimlari va tarmoq integratorlari bo‘yicha tayyorlash kerak.
Men ushbu individual loyihamda OSI modelining amaliy pog`onasida ishlatiluvchi TFTP protokolini ko`rib chiqdim.
Trivial File Transfer Protocol (TFTP) - bu ma'lumotlar bazasi protokoli (UDP) bilan birgalikda ishlaydigan ulanishsiz xizmat. TFTP konfiguratsiya fayllarini va Cisco IOS operatsion tizimini uzatish, shuningdek TFTP-ni qo'llabquvvatlaydigan tizimlar o'rtasida fayllarni uzatish uchun marshrutizatorlarda ishlatiladi. TFTP soddaligi va dasturiy ta'minotning pastligi bilan ajralib turadi. U serverga ulanayotganda fayllarni o'qishi yoki yozishi mumkin, ammo u ro'yxatlar va kataloglarni saqlamaydi. Shuning uchun TFTP FTP-ga qaraganda tezroq.
SFTP va TFTP ikkita protokol bo'lib, ular funksionallik, xavfsizlik va foydalanishda aniq farqlarga ega. SFTP to'liq xususiyatli fayl uzatish protokoli bo'lib, u fayllarni tarmoq orqali uzatishning xavfsiz usulini ta'minlaydi, TFTP esa, asosan, disksiz ish stantsiyalarini yuklash va tarmoq qurilmalari o'rtasida konfiguratsiya fayllarini uzatish uchun ishlatiladigan oddiyroq protokoldir. SFTP va TFTP o'rtasida tanlov qilishda, muayyan foydalanish holatini hisobga olish va talablarga eng mos keladigan protokolni tanlash muhimdir.
|
| |