BASIC NETWORK PROGRAMMING
(LẬP TRÌNH MẠNG CƠ BẢN)
Introduction to Java Network Programmning
Basic File Operation with File Class
1
Introduction to Java Network
Programmning
2
Introduction
◼ Computer networking has grown explosively
Khoa CNTT – ĐH Nông Lâm TP. HCM 3/26
Introduction
◼ Since the 1970s, computer communication has
changed from a research topic to an essential part of
infrastructure
◼ Networking is used in every aspect of our lives:
◼ Business
◼ Advertising
◼ Production
◼ Shipping
◼ Planning
◼ Billing
◼ Accounting
Khoa CNTT – ĐH Nông Lâm TP. HCM 4/26
Introduction
◼ In short, computer networks are everywhere
◼ In 1980, the Internet was a research project that
involved a few dozen sites
◼ Today, the Internet has grown into a communication
system that reaches all of the world
◼ Internet and WWW have emerged as global
ubiquitous media for communication and changing the
way we conduct science, engineering, and commerce.
◼ They also changing the way we learn, live, enjoy,
communicate, interact, etc. It appears like the
modern life activities are getting completely centered
around the Internet.
Khoa CNTT – ĐH Nông Lâm TP. HCM 5/26
Internet Applications
PC client
Internet
Server
Local Area Network
PDA / Smart Phone
Khoa CNTT – ĐH Nông Lâm TP. HCM 6/26
Increased demand for Internet applications
◼ To take advantage of opportunities presented
by the Internet, businesses are continuously seeking
new and innovative ways and means for offering
their services via the Internet.
◼ This created a huge demand for software designers
with skills to create new Internet-enabled
applications or migrate existing legacy applications
on the Internet platform.
◼ Object-oriented Java technologies: Sockets,
Threads, RMI, Web services -- have emerged as
leading solutions for creating portable, efficient,
and maintainable large and complex Internet
applications.
Khoa CNTT – ĐH Nông Lâm TP. HCM 7/26
Elements of Client-Server Computing
a client, a server, and network
Client
Server
Network
Client machine
Server machine
Khoa CNTT – ĐH Nông Lâm TP. HCM 8/26
Networking Basics
◼ Applications Layer TCP/IP Stack
◼ Standard apps
◼ HTTP
◼ FTP Application
◼ Telnet (http,ftp,telnet,…)
◼ User apps Transport
◼ Transport Layer (TCP, UDP,..)
◼ TCP Network
◼ UDP (IP,..)
◼ Programming Interface:
Link
◼ Sockets
(device driver,..)
◼ Network Layer
◼ IP
◼ Link Layer
◼ Device drivers
Khoa CNTT – ĐH Nông Lâm TP. HCM 9/26
TCP Programming
◼ TCP (Transport Control Protocol) is a TCP/IP Stack
connection-oriented protocol that
provides a reliable flow of data between
two computers. Application
◼ When two applications want to (http,ftp,telnet,…)
communicate to each other reliably, Transport
they establish a connection and send (TCP, UDP,..)
data back and forth over that connection Network
◼ TCP provides a point-to-point channel (IP,..)
for applications that require reliable Link
communications. (device driver,..)
◼ Example applications:
◼ HTTP
◼ FTP
◼ SMTP / POP / IMAP
Khoa CNTT – ĐH Nông Lâm TP. HCM 10/26
UDP Programming
◼ UDP (User Datagram Protocol) TCP/IP Stack
is a protocol that sends
independent packets of data, Application
(http,ftp,telnet,…)
called datagrams, from one
Transport
computer to another with no (TCP, UDP,..)
guarantees about arrival. Network
◼ Example applications: (IP,..)
Link
◼ Clock server (device driver,..)
◼ Ping
Khoa CNTT – ĐH Nông Lâm TP. HCM 11/26
Understanding Ports
◼ The TCP and UDP protocols use ports to map
incoming data to a particular process running on
a computer. P
o TCP
server
r Client
t
app app app app
port port port port
TCP or UDP
Packet
Data port# data
Khoa CNTT – ĐH Nông Lâm TP. HCM 12/26
Sockets and Ports
port 13 Time Service
Client
port 80 Web Service
Socket Socket
Server
Khoa CNTT – ĐH Nông Lâm TP. HCM 13/26
Well-Known Ports
◼ 20 : FTP (Data)
◼ 21 : FTP
◼ 23 : Telnet
◼ 25 : SMTP
◼ 43 : Whois
◼ 53 : DNS
◼ 80 : HTTP
◼ 110 : POP3
◼ 1099 : RMI
Khoa CNTT – ĐH Nông Lâm TP. HCM 14/26
The World Wide Web
◼ Runs on the Internet
◼ Uses HTTP protocol
◼ Invented by Tim Berners-Lee (and a cast of
thousands)
Khoa CNTT – ĐH Nông Lâm TP. HCM 15/26
URL
URL = Unique Resource Locator
◼ protocol://host[:port][/path/][file][#anchor]
◼ http://www.javasoft.com/sfaq/index.html
◼ http://www.javasoft.com/sfaq/
◼ ftp://ftp.stinky.com/pub/java/course.zip
Khoa CNTT – ĐH Nông Lâm TP. HCM 16/26
HTTP
◼ port 80 default
◼ Client:
GET path HTTP/1.0
Header: value
blank line
◼ Server:
HTTP/1.0 OK 200
Header: value
blank line
data
◼ telnet demo again
Khoa CNTT – ĐH Nông Lâm TP. HCM 17/26
Basic File Operation with File class
18
Java File and Folder
◼ An abstract representation of file and directory
pathnames.
◼ For UNIX platforms, the prefix of an absolute
pathname is always "/". Relative pathnames have no
prefix.
◼ For Microsoft Windows platforms, the prefix of a
pathname that contains a drive specifier consists of
the drive letter followed by ":" and possibly followed
by "\\" if the pathname is absolute. A relative
pathname that does not specify a drive has no prefix.
◼ Examples:
◼ “D:\\TMP\\t.txt”
◼ “t.txt”
Khoa CNTT – ĐH Nông Lâm TP. HCM 19/26
The java.io.File Class
◼ public File(File parent, String child)
◼ Creates a new File instance from a parent abstract
pathname and a child pathname string.
◼ public File(String parent, String child)
◼ Creates a new File instance from a parent pathname
string and a child pathname string.
◼ public File(String pathname)
◼ Creates a new File instance by converting the given
pathname string into an abstract pathname. If the
given string is the empty string, then the result is the
empty abstract pathname.
◼ public String getCanonicalPath ()
◼ Returns the absolute and unique pathname string of
this abstract pathname.
Khoa CNTT – ĐH Nông Lâm TP. HCM 20/26
The java.io.File Class
◼ public boolean exists()
◼ Tests whether the file or directory denoted by this
abstract pathname exists.
◼ public boolean isDirectory()
◼ Tests whether the file denoted by this abstract
pathname is a directory.
◼ public boolean isFile()
◼ Tests whether the file denoted by this abstract
pathname is a normal file.
◼ public long length()
◼ Returns the length (the zise in Kbyte) of the file denoted
by this abstract pathname.
Khoa CNTT – ĐH Nông Lâm TP. HCM 21/26
The java.io.File Class
◼ public boolean delete()
◼ Deletes the file or directory denoted by this abstract
pathname. If this pathname denotes a directory,
then the directory must be empty in order to be
deleted.
◼ public String[] list()
◼ Returns an array of strings naming the files and
directories in the directory denoted by this abstract
pathname.
◼ public String[] list(FilenameFilter filter)
◼ Returns an array of strings naming the files and
directories in the directory denoted by this abstract
pathname that satisfy the specified filter.If the given
filter is null then all names are accepted.
Khoa CNTT – ĐH Nông Lâm TP. HCM 22/26
The java.io.File Class
◼ public File[] listFiles()
◼ Returns an array of abstract pathnames denoting the
files in the directory denoted by this abstract pathname.
◼ public File[] listFiles(FilenameFilter filter)
◼ Returns an array of abstract pathnames denoting the
files and directories in the directory denoted by this
abstract pathname that satisfy the specified filter.
◼ public boolean mkdir()
◼ Creates the directory named by this abstract pathname.
◼ public boolean mkdirs()
◼ Creates the directory named by this abstract pathname,
including any necessary but nonexistent parent
directories.
Khoa CNTT – ĐH Nông Lâm TP. HCM 23/26
The java.io.File Class
◼ public boolean isAbsolute()
◼ Tests whether this abstract pathname is absolute.
◼ public boolean canRead()
◼ Tests whether the application can read the file
denoted by this abstract pathname.
◼ public boolean canWrite()
◼ Tests whether the application can modify the file
denoted by this abstract pathname
◼ public boolean isHidden()
◼ Tests whether the file named by this abstract
pathname is a hidden file.
Khoa CNTT – ĐH Nông Lâm TP. HCM 24/26
The java.io.File Class
//List of all files in D: with extention tgz ???
import java.io.*;
import java.util.*;
public class DLister {
public static void main(String[] args) {
File path = new File("D:\\");
String[] list;
list = path.list(new DirFilter(".tgz"));
for(int i = 0; i < list.length; i++)
System.out.println(list[i]);
}}
class DirFilter implements FilenameFilter {
String afn;
DirFilter(String afn) { this.afn = afn; }
public boolean accept(File dir, String name) {
String f = new File(name).getName();
return f.indexOf(afn) != -1;
}}
Khoa CNTT – ĐH Nông Lâm TP. HCM 25/26
Comparing getPath methods
getPath(), getAbsolutePath(),
getCanonicalPath()
Ref: https://www.baeldung.com/java-path
Khoa CNTT – ĐH Nông Lâm TP. HCM 26/26
Comparing getPath methods
◼ File file = new File("foo/foo-one.txt");
◼ file.getPath()
◼ foo/foo-one.txt // on Unix systems
◼ foo\foo-one.txt // on Windows systems
◼ file.getAbsolutePath()
◼ /home/username/baeldung/foo/foo-one.txt // Unix
◼ C:\Users\username\baeldung\foo\foo-one.txt // Win
◼ file.getCanonicalPath()
◼ /home/username/baeldung/bar/bar-one.txt // Unix
◼ C:\Users\username\baeldung\bar\bar-one.txt // Win
Khoa CNTT – ĐH Nông Lâm TP. HCM 27/26
Comparing getPath methods
◼ File file = new File("bar/baz/../bar-one.txt");
◼ file.getPath()
◼ foo/foo-one.txt // on Unix systems
◼ foo\foo-one.txt // on Windows systems
◼ file.getAbsolutePath()
◼ /home/username/baeldung/bar/baz/../bar-one.txt //U
◼ C:\Users\username\baeldung\bar\baz\..\bar-one.txt
//W
◼ file.getCanonicalPath()
◼ /home/username/baeldung/bar/baz/baz-one.txt // Unix
◼ C:\Users\username\baeldung\bar\baz\baz-one.txt // W
Khoa CNTT – ĐH Nông Lâm TP. HCM 28/26
Exercise
◼ public boolean deleteDir(String path)
◼ public boolean find(String path, String filePattern)
◼ public boolean find(String path, String pattern)
Khoa CNTT – ĐH Nông Lâm TP. HCM 29/26