Data and Computer Communications - SourceForge

13
Engr. Abdul-Rahman Mahmood MS, PMP, MCP, QMR(ISO9001:2000) [email protected] [email protected] alphapeeler.sf.net/pubkeys/pkey.htm http://alphapeeler.sourceforge.net pk.linkedin.com/in/armahmood http://alphapeeler.tumblr.com www.twitter.com/alphapeeler [email protected] www.facebook.com/alphapeeler [email protected] abdulmahmood-sss alphasecure mahmood_cubix 48660186 [email protected] [email protected] http://alphapeeler.sf.net/me http://alphapeeler.sf.net/acms/ Networks Programming VC++, VB, ASP

Transcript of Data and Computer Communications - SourceForge

Page 1: Data and Computer Communications - SourceForge

Engr. Abdul-Rahman MahmoodMS, PMP, MCP, QMR(ISO9001:2000)

[email protected] [email protected]

alphapeeler.sf.net/pubkeys/pkey.htm http://alphapeeler.sourceforge.net

pk.linkedin.com/in/armahmood http://alphapeeler.tumblr.com

www.twitter.com/alphapeeler [email protected]

www.facebook.com/alphapeeler [email protected]

abdulmahmood-sss alphasecure mahmood_cubix 48660186

[email protected] [email protected]

http://alphapeeler.sf.net/me http://alphapeeler.sf.net/acms/

Networks Programming

VC++, VB, ASP

Page 2: Data and Computer Communications - SourceForge
Page 3: Data and Computer Communications - SourceForge

FTP FTP was developed in 1985. First standardized in RFC

959 . Extensions to enhance flexibility and security (RFC 1579, RFC 2228).

Works on top of TCP and in general uses port 21;

Server responds with actions & command status messages.

Each session involves at least one file transfer.

Basic principle of file transfers using FTP is in Fig. 7.5.

FTP involves two connections: for control and data. FTP commands and replies are exchanged via control connection; data is exchanged over data connection.

In practice, a single connection is used for both data and control information exchange.

Page 4: Data and Computer Communications - SourceForge

3 categories of FTP commands:

1. Access control

2. Transfer parameter

3. ServiceFigure 7.5 FTP session

Access control commands

Access control commands include:

– USER—indicates the user;

– PASS—indicates the password;

– CWD—changes directory;

– CDUP—changes directory to parent;

– QUIT—logouts.

Page 5: Data and Computer Communications - SourceForge

Transfer parameter commands: PORT—publishes local data port;

PASV—makes server passive (listen);

TYPE—indicates data representation (A-ASCII, E-EBCDIC, I-Image, LLocal);

MODE—indicates transfer mode (S-Stream, B-Block, C-Compressed);

STRU—sets file structure (F-FILE, R-RECORD, P-PAGE).

Page 6: Data and Computer Communications - SourceForge

Service commands RETR—retrieves file;

STOR—sends and stores the file remotely;

APPE—sends file and appends;

DELE—deletes the file;

MKD—makes a new directory;

RMD—removes a directory;

PWD—prints working directory;

LIST—lists files.

Page 7: Data and Computer Communications - SourceForge

FTP Every command must generate some action and at

least one reply from the server.

This enables the synchronization of requests sent by clients and actions performed by the server and also allows the clients to know the server status.

Reply is a single line; however, multiple lines are also accepted.

The reply must contain a 3 digit status code which enables machines to assess the server status and a text message which describes the server status in human language.

Page 8: Data and Computer Communications - SourceForge

Commands / Response on control channel

01234567891011121314

C:\> telnet ftp.ietf.org 21220 ProFTPD 1.3.1 Server (ProFTPD) [64.170.98.33]USER anonymous331 Anonymous login ok, send complete email address as your passwordPASS [email protected] Anonymous access granted, restrictions applyCWD ietf/ftpext/250 CWD command successfulPASV227 Entering Passive Mode (64,170,98,33,151,31).RETR ftpext-charter.txt150 Opening ASCII mode data connection for ftpext-charter.txt (6060 bytes)226 Transfer completeQUIT221 Goodbye.

Page 9: Data and Computer Communications - SourceForge

Commands / Response on control channel Between line 10 and 12, you will notice that the file

was downloaded. To start the download, I had to open up another telnet window to open the data channel. To figure out to which IP address and port I had to connect to, we have to look at line number 9. We received a set of numbers (64,170,98,33,151,31) from the server in response to the PASV command. The first four related to the IP address 64.170.98.33 and the last two 151 and 31 help us identify which port to connect to. Multiply the first by 256 and add it to the second. So, 151 * 256 + 31 which is equal to 38687. Now that we have the IP address and port number, all we have to do is to open a second terminal and telnet to IP:Port as shown below:

Page 10: Data and Computer Communications - SourceForge

Commands / Response on control channel C:\> telnet 64.170.98.33 38687

This will now show you all the contents of the file ftpext-charter.txt being thrown into your second terminal window. Once this is done, you can proceed to type further commands on the control channel (the first terminal window).

Page 11: Data and Computer Communications - SourceForge

import java.io.*;

import java.net.*;

public class FTPSocket {

private Socket sock = null;

/*data streams for reading/writing to/from the socket*/

private DataOutputStream os = null;

private DataInputStream is = null;

/*server details*/

private String host = "alphapeeler.eu5.org";

private int port = 21;

//private String user = "anonymous";

//private String pass = "[email protected]";

private String user = "alphapeeler.eu5.org";

private String pass = “abc12345";

public static void main(String[] args) {

FTPSocket ftp = new FTPSocket();

ftp.FTPOperations();

}

@SuppressWarnings("deprecation")

public void FTPOperations() {

try {

/*create and open the socket*/

sock = new Socket(this.host, this.port);

/*create the I/O data streams*/

os = new

DataOutputStream(sock.getOutputStream());

is = new DataInputStream(sock.getInputStream());

if(sock != null && os != null && is != null) {

/*Connection successful.*/

System.out.println("Connected OK!");

try {

os.writeBytes("USER "+ user +"\r\n");

os.writeBytes("PASS "+ pass +"\r\n");

os.writeBytes("SYST\r\n");

os.writeBytes("PWD \r\n");

os.writeBytes("CDUP\r\n");

os.writeBytes("RMD newDir\r\n");

os.writeBytes("MKD newDir\r\n");

os.writeBytes("CWD newDir\r\n");

os.writeBytes("LIST -a\r\n");

os.writeBytes("CDUP\r\n");

os.writeBytes("TYPE A\r\n");

os.writeBytes("RETR README.html\r\n");

os.writeBytes("STOR c://globdata.ini\r\n");

os.writeBytes("QUIT\r\n");

/*check the server reply against OK*/

String rcv;

while((rcv = is.readLine()) != null) {

System.out.println(rcv);

if(rcv.indexOf("Ok") != -1)

/*Server success confirmation*/

break;

}

} catch(Exception e) {

System.out.println("Communication error.");

}

}

else {

/*Unsuccessful connection.*/

System.out.println("Connection error!");

}

} catch(Exception e) {

System.out.println("Host " + host + "unknown");

}

}

}

Page 12: Data and Computer Communications - SourceForge

FTP console results Connected OK!

220---------- Welcome to Pure-FTPd ----------

220-You are user number 33 of 200 allowed.

220-Local time is now 17:53. Server port: 21.

220-This is a private system - No anonymous login

220-IPv6 connections are also welcome on this server.

220 You will be disconnected after 5 minutes of inactivity.

331 User alphapeeler.eu5.org OK. Password required

230-OK. Current restricted directory is /

230 53 Kbytes used (0%) - authorized: 1572864 Kb

215 UNIX Type: L8

257 "/" is your current location

250 OK. Current directory is /

250-53 Kbytes used (0%) - authorized: 1572864 Kb

250 The directory was successfully removed

257-"newDir" : The directory was successfully created

257 53 Kbytes used (0%) - authorized: 1572864 Kb

250 OK. Current directory is /newDir

425 No data connection

250 OK. Current directory is /

200 TYPE is now ASCII

425 No data connection

553 Can't open that file: No such file or directory

221-Goodbye. You uploaded 0 and downloaded 0 kbytes.

221 Logout.

Page 13: Data and Computer Communications - SourceForge

FTP - Apache Commons Net APIimport java.io.*;import org.apache.commons.net.ftp.FTP;import org.apache.commons.net.ftp.FTPClient;public class ApacheJavaFTP {public static void main(String[] args) {/*sets server parameters*/String srv = new String("ftp.ietf.org");int port = 21;String user = new String("anonymous");String pass = new String("[email protected]");/*creates FTPClient object*/FTPClient ftpClient = new FTPClient();try {/*connects to FTP server*/ftpClient.connect(srv, port);/*authenticates on the server*/ftpClient.login(user, pass);/*sets connection to client-server mode*/ftpClient.enterLocalPassiveMode();/*sets transferred file type*/ftpClient.setFileType(FTP.ASCII_FILE_TYPE);/*remote file*/String rf = "/ietf/ftpext/ftpext-charter.txt";/*local file*/File lf =new File("C:/ftpext-charter.txt");

/*enables data writing*/OutputStream os =new BufferedOutputStream(newFileOutputStream(lf));/*gets remote file content*/if(ftpClient.retrieveFile(rf, os))System.out.println("File transfer success.");/*closes output stream*/os.close();} catch (IOException ex) {System.out.println("Error: " + ex.getMessage());ex.printStackTrace();}finally {try {/*logouts and disconnects*/if (ftpClient.isConnected()) {ftpClient.logout();ftpClient.disconnect();}} catch (IOException ex) {ex.printStackTrace();}}}}

Output on console:

File transfer success.