This document provides an overview of network protocols in Java. It begins with an introduction to networking concepts like protocols, clients, servers and the internet protocol stack. It then discusses socket programming in Java using TCP and UDP. Examples are provided of a simple TCP client-server application that converts text to uppercase and a UDP client-server that sends and receives datagrams. The document aims to teach how to build networked applications in Java using common network protocols.
The presentation given at MSBTE sponsored content updating program on 'Advanced Java Programming' for Diploma Engineering teachers of Maharashtra. Venue: Guru Gobind Singh Polytechnic, Nashik
Date: 22/12/2010
Session: Java Network Programming
The Presentation given at Guru Gobind Singh Polytechnic, Nashik for Third Year Information Technology and Computer Engineering Students on 08/02/2011.
Topic: Java Network Programming
The presentation given at MSBTE sponsored content updating program on 'Advanced Java Programming' for Diploma Engineering teachers of Maharashtra. Venue: Guru Gobind Singh Polytechnic, Nashik
Date: 22/12/2010
Session: Java Network Programming
The Presentation given at Guru Gobind Singh Polytechnic, Nashik for Third Year Information Technology and Computer Engineering Students on 08/02/2011.
Topic: Java Network Programming
When we desire a communication between two applications possibly running on different machines, we need sockets. This presentation aims to provide knowledge of basic socket programming to undergraduate students. Basically, this presentation gives the importance of socket in the area of networking and Unix Programming. The presentation of Topic (Sockets) has designed according to the Network Programming Subject, B.Tech, 6th Semester syllabus of Punjab Technical University Kapurthala, Punjab.
When we desire a communication between two applications possibly running on different machines, we need sockets. This presentation aims to provide knowledge of basic socket programming to undergraduate students. Basically, this presentation gives the importance of socket in the area of networking and Unix Programming. The presentation of Topic (Sockets) has designed according to the Network Programming Subject, B.Tech, 6th Semester syllabus of Punjab Technical University Kapurthala, Punjab.
presentation will make you aware about the programming techniques in shell , and will help you to build more strong foundation into the world of shell programming.
Slides for protocol layering and network applicationsjajinekkanti
why and how Protocol layering is done in TCP/IP protocol suite, how multiplexing and demultiplexing can be done in different protocol layers in TCP\IP.
This presentation is about:
Uses of Networking.
Various types of networking.
Applications used for networking.
Methods of network security.
Methods of communication -2G,3G,4G,Fiber Optics
Transmission Media.
Various types of protocols.
Cloud Computing
Protection against Viruses.
Aplication and Transport layer- a practical approachSarah R. Dowlath
This presentation was done for a Networking course. It really shows from a more practical standpoint how the application layer and the transport layer communicates with each other and operates on a whole to get the job done. It gives the reader more insight of how the pieces come together in an IT networking world.
This presentation is used in many places including Vignan, AITAM, This contains both tutorials and hands on training. This presentation explains Unix Internals, Socket programming, both data gram based and IP based concepts are explained with live examples.
Module 2: Application layer
Principles of network applications, Web and HTTP, File transfer protocol, Electronic mail, Domain name system, DDNS, SSH, SNMP ,Internet Applications.
this is a power point presentation on chat applicationmdprince1262
this is a power point presentation on chat application it was a minor academic project in my college in order to exchange sessional or mid exams by making some small/ minor project and present through the presentation and on the basis of performance of presentation students are getting marks, its a great approach to motivate students to do projects
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualityInflectra
In this insightful webinar, Inflectra explores how artificial intelligence (AI) is transforming software development and testing. Discover how AI-powered tools are revolutionizing every stage of the software development lifecycle (SDLC), from design and prototyping to testing, deployment, and monitoring.
Learn about:
• The Future of Testing: How AI is shifting testing towards verification, analysis, and higher-level skills, while reducing repetitive tasks.
• Test Automation: How AI-powered test case generation, optimization, and self-healing tests are making testing more efficient and effective.
• Visual Testing: Explore the emerging capabilities of AI in visual testing and how it's set to revolutionize UI verification.
• Inflectra's AI Solutions: See demonstrations of Inflectra's cutting-edge AI tools like the ChatGPT plugin and Azure Open AI platform, designed to streamline your testing process.
Whether you're a developer, tester, or QA professional, this webinar will give you valuable insights into how AI is shaping the future of software delivery.
Generating a custom Ruby SDK for your web service or Rails API using Smithyg2nightmarescribd
Have you ever wanted a Ruby client API to communicate with your web service? Smithy is a protocol-agnostic language for defining services and SDKs. Smithy Ruby is an implementation of Smithy that generates a Ruby SDK using a Smithy model. In this talk, we will explore Smithy and Smithy Ruby to learn how to generate custom feature-rich SDKs that can communicate with any web service, such as a Rails JSON API.
UiPath Test Automation using UiPath Test Suite series, part 3DianaGray10
Welcome to UiPath Test Automation using UiPath Test Suite series part 3. In this session, we will cover desktop automation along with UI automation.
Topics covered:
UI automation Introduction,
UI automation Sample
Desktop automation flow
Pradeep Chinnala, Senior Consultant Automation Developer @WonderBotz and UiPath MVP
Deepak Rai, Automation Practice Lead, Boundaryless Group and UiPath MVP
Connector Corner: Automate dynamic content and events by pushing a buttonDianaGray10
Here is something new! In our next Connector Corner webinar, we will demonstrate how you can use a single workflow to:
Create a campaign using Mailchimp with merge tags/fields
Send an interactive Slack channel message (using buttons)
Have the message received by managers and peers along with a test email for review
But there’s more:
In a second workflow supporting the same use case, you’ll see:
Your campaign sent to target colleagues for approval
If the “Approve” button is clicked, a Jira/Zendesk ticket is created for the marketing design team
But—if the “Reject” button is pushed, colleagues will be alerted via Slack message
Join us to learn more about this new, human-in-the-loop capability, brought to you by Integration Service connectors.
And...
Speakers:
Akshay Agnihotri, Product Manager
Charlie Greenberg, Host
DevOps and Testing slides at DASA ConnectKari Kakkonen
My and Rik Marselis slides at 30.5.2024 DASA Connect conference. We discuss about what is testing, then what is agile testing and finally what is Testing in DevOps. Finally we had lovely workshop with the participants trying to find out different ways to think about quality and testing in different parts of the DevOps infinity loop.
Elevating Tactical DDD Patterns Through Object CalisthenicsDorra BARTAGUIZ
After immersing yourself in the blue book and its red counterpart, attending DDD-focused conferences, and applying tactical patterns, you're left with a crucial question: How do I ensure my design is effective? Tactical patterns within Domain-Driven Design (DDD) serve as guiding principles for creating clear and manageable domain models. However, achieving success with these patterns requires additional guidance. Interestingly, we've observed that a set of constraints initially designed for training purposes remarkably aligns with effective pattern implementation, offering a more ‘mechanical’ approach. Let's explore together how Object Calisthenics can elevate the design of your tactical DDD patterns, offering concrete help for those venturing into DDD for the first time!
UiPath Test Automation using UiPath Test Suite series, part 4DianaGray10
Welcome to UiPath Test Automation using UiPath Test Suite series part 4. In this session, we will cover Test Manager overview along with SAP heatmap.
The UiPath Test Manager overview with SAP heatmap webinar offers a concise yet comprehensive exploration of the role of a Test Manager within SAP environments, coupled with the utilization of heatmaps for effective testing strategies.
Participants will gain insights into the responsibilities, challenges, and best practices associated with test management in SAP projects. Additionally, the webinar delves into the significance of heatmaps as a visual aid for identifying testing priorities, areas of risk, and resource allocation within SAP landscapes. Through this session, attendees can expect to enhance their understanding of test management principles while learning practical approaches to optimize testing processes in SAP environments using heatmap visualization techniques
What will you get from this session?
1. Insights into SAP testing best practices
2. Heatmap utilization for testing
3. Optimization of testing processes
4. Demo
Topics covered:
Execution from the test manager
Orchestrator execution result
Defect reporting
SAP heatmap example with demo
Speaker:
Deepak Rai, Automation Practice Lead, Boundaryless Group and UiPath MVP
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Albert Hoitingh
In this session I delve into the encryption technology used in Microsoft 365 and Microsoft Purview. Including the concepts of Customer Key and Double Key Encryption.
Accelerate your Kubernetes clusters with Varnish CachingThijs Feryn
A presentation about the usage and availability of Varnish on Kubernetes. This talk explores the capabilities of Varnish caching and shows how to use the Varnish Helm chart to deploy it to Kubernetes.
This presentation was delivered at K8SUG Singapore. See https://feryn.eu/presentations/accelerate-your-kubernetes-clusters-with-varnish-caching-k8sug-singapore-28-2024 for more details.
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Network protocols and Java programming
1. School of Systems Engineering
BSc Computer Science - “Java” (SE2JA11)
Network Protocols in Java
Dr. Giuseppe Di Fatta
- Associate Professor of Computer Science
Web: http://www.personal.reading.ac.uk/~sis06gd/
Email: G.DiFatta@reading.ac.uk
- Director of the MSc Advanced Computer Science
http://www.reading.ac.uk/sse/pg-taught/sse-mscadvancedcomputerscience.aspx
These lecture slides are available at:
http://www.personal.reading.ac.uk/~sis06gd/resources.html
2. Outline
Introduction to Networking
• Network protocols
• Client-server network applications
Java Socket programming using TCP and UDP
Simple Mail Transfer Protocol (SMTP)
Code examples:
• How to send an email in Java
• How to implement a simple FTP client in Java
Java, Dr. Giuseppe Di Fatta, 2007-2013
2
3. What’s a protocol?
human protocols:
• “what’s the time?”
• “I have a question”
• introductions
… specific msgs sent
… specific actions taken
when msgs received,
or other events
network protocols:
• machines rather than
humans
• all communication
activity in Internet
governed by protocols
A protocol defines:
• format and order of messages
sent and received among
network entities
• actions taken on message
transmission, receipt
Java, Dr. Giuseppe Di Fatta, 2007-2013
3
4. What’s a protocol?
a human protocol and a computer network protocol:
Hi
TCP connection
req
Hi
TCP connection
response
Got the
time?
Get http://www.google.com/
2:00
<file>
time
Java, Dr. Giuseppe Di Fatta, 2007-2013
4
5. Internet protocol stack
• application: supporting network
applications
– FTP, SMTP, STTP
• transport: host-host data transfer
– TCP, UDP
• network: routing of datagrams from
source to destination
– IP, routing protocols
• link: data transfer between neighboring
network elements
– PPP, Ethernet
application
transport
network
link
physical
• physical: bits “on the wire”
Java, Dr. Giuseppe Di Fatta, 2007-2013
5
6. Network applications: some jargon
Process: program running
user agent: interfaces with
within a host.
user “above” and network
• within same host, two
“below”.
processes communicate
• implements user interface
using interprocess
& application-level
communication (defined by
protocol
OS).
– Web: browser
• processes running in
– E-mail: mail reader
different hosts
– streaming audio/video:
communicate with an
media player
application-layer protocol
Java, Dr. Giuseppe Di Fatta, 2007-2013
6
7. Applications and application-layer protocols
Application: communicating,
distributed processes
– e.g., e-mail, Web, P2P file
sharing, instant messaging
– running in end systems (hosts)
– exchange messages to
implement application
application
transport
network
data link
physical
Application-layer protocols
– one “piece” of an app
– define messages exchanged by
apps and actions taken
– use communication services
provided by lower layer
protocols (TCP, UDP)
application
transport
network
data link
physical
Java, Dr. Giuseppe Di Fatta, 2007-2013
application
transport
network
data link
physical
7
8. Application-Layer Protocol
A protocol defines:
• Types of messages
exchanged, e.g., request &
response messages
• Syntax of message types:
what fields in messages & how
fields are delineated
• Semantics of the fields, i.e.,
meaning of information in
fields
• Rules for when and how
processes send & respond to
messages
Public-domain protocols:
• standardised by IETF
• allow for interoperability
• e.g., HTTP, SMTP, FTP
Example of P2P protocols:
• BitTorrent
Example of some proprietary
P2P protocols:
• KaZaA
• Skype
Java, Dr. Giuseppe Di Fatta, 2007-2013
8
9. ARPANET-INTERNET Transition
• The Internet was born on January 1st, 1983: when
ARPANET changed from NCP to the TCP/IP protocol suite.
–
–
Flag day: a change which requires a complete restart or conversion of a relevant
software component or data. The change is large and expensive; reversing the change
is difficult or impossible.
“The goal is to make a complete switch over from the NCP to IP/TCP by 1 January
1983.” (Jon Postel, NCP/TCP Transition Plan, RFC 801)
Bob Kahn and Vint Cerf
- fathers of the Internet: inventors of the TCP/IP protocol suite
Java, Dr. Giuseppe Di Fatta, 2007-2013
9
10. IETF
The Internet Engineering Task Force (IETF) is a loosely self-organized group
of people who contribute to the engineering and evolution of Internet
technologies. The IETF in no way "runs the Internet“.
IETF quotes (believes)
Robert Kahn (interview, 2002):
People don't think of the Internet as a logical architecture. They think of it as what
AOL does. The Internet is an architectural philosophy, rather than a technology.
David Clark (1992-07-16, 24th IETF meeting):
We reject: kings, presidents and voting.
We believe in: rough consensus and running code.
Jon Postel (RFC 793 - September 1981) - Robustness Principle:
TCP implementations will follow a general principle of robustness:
be conservative in what you do, be liberal in what you accept from
others.
Jon Postel (1943-1998): see RFC 2468, October 1998
Java, Dr. Giuseppe Di Fatta, 2007-2013
10
11. Client-Server Paradigm
Typical network app has two
pieces: client and server
Client:
• initiates contact with server
(“speaks first”)
• typically requests service from
server,
• Web: client implemented in
browser; e-mail: in mail reader
application
transport
network
data link
physical
request
Server:
• provides requested service to client
• e.g., Web server sends requested Web
page, mail server delivers e-mail
Java, Dr. Giuseppe Di Fatta, 2007-2013
reply
application
transport
network
data link
physical
11
12. Addressing Hosts and Processes
• For a process to receive
messages, it must have an
identifier
• Every host has a unique 32bit IP address
• Q: does the IP address of
the host on which the
process runs suffice for
identifying the process?
• Answer: No, many
processes can be running
on same host
• Identifier includes both the
IP address and port
numbers associated with
the process on the host.
• Example port numbers:
– HTTP server: 80
– Mail server: 25
Java, Dr. Giuseppe Di Fatta, 2007-2013
12
13. IP Addresses
Current world population:
7 billion
IP v4 addresses: 232 = 4 billion
IP v6 addresses: 2128
Java, Dr. Giuseppe Di Fatta, 2007-2013
13
14. Processes communicating across network
• process sends/receives
messages to/from its socket
• socket analogous to door
– sending process shoves
message out door
– sending process assumes
transport infrastructure on
other side of door which brings
message to socket at receiving
process
host or
server
host or
server
process
controlled by
app developer
process
socket
socket
TCP with
buffers,
variables
Internet
TCP with
buffers,
variables
controlled
by OS
• API: (1) choice of transport protocol; (2) ability to fix a few parameters
Java, Dr. Giuseppe Di Fatta, 2007-2013
14
15. Internet transport protocols services
TCP service:
UDP service:
• connection-oriented: setup
required between client and
server processes
• reliable transport between
sending and receiving process
• flow control: sender won’t
overwhelm receiver
• congestion control: throttle
sender when network
overloaded
• does not providing: timing,
minimum bandwidth
guarantees
• unreliable data transfer
between sending and
receiving process
• does not provide:
connection setup, reliability,
flow control, congestion
control, timing, or
bandwidth guarantee
Q: why bother? Why is there a
UDP?
Java, Dr. Giuseppe Di Fatta, 2007-2013
15
16. Internet Applications
Application
e-mail
remote terminal access
Web
file transfer
streaming multimedia
Internet telephony
Application
layer protocol
Underlying
transport protocol
SMTP [RFC 2821]
Telnet [RFC 854]
HTTP [RFC 2616]
FTP [RFC 959]
proprietary
TCP
TCP
TCP
TCP
TCP or UDP
proprietary
typically UDP
Java, Dr. Giuseppe Di Fatta, 2007-2013
16
17. Socket programming
Goal: learn how to build client/server application that
communicate using sockets.
Socket API
• introduced in BSD4.1 UNIX, 1981
• explicitly created, used, released by
applications
• client/server paradigm
• two types of transport service via socket
API:
– unreliable datagram (UDP)
– reliable, byte stream-oriented (TCP)
socket
a host-local, application-created,
OS-controlled
interface
(a
“door”) into which application
process can both send and
receive
messages
to/from
another application process.
Java, Dr. Giuseppe Di Fatta, 2007-2013
17
18. Socket-programming using TCP
Socket: a door between application process and
end-end-transport protocol (UCP or TCP)
TCP service: reliable transfer of bytes from one
process to another
controlled by
application
developer
controlled by
operating
system
process
process
socket
TCP with
buffers,
variables
host or
server
internet
socket
TCP with
buffers,
variables
controlled by
application
developer
controlled by
operating
system
host or
server
Java, Dr. Giuseppe Di Fatta, 2007-2013
18
19. Socket programming with TCP
Client must contact server
• server process must first be
running
• server must have created
socket (door) that welcomes
client’s contact
Client contacts server by:
• creating client-local TCP
socket
• specifying IP address, port
number of server process
• When client creates socket:
client TCP establishes
connection to server TCP
• When contacted by client,
server TCP creates new
socket for server process to
communicate with client
– allows server to talk with
multiple clients
– source port numbers used
to distinguish clients
application viewpoint
TCP provides reliable, in-order
transfer of bytes (“pipe”)
between client and server
Java, Dr. Giuseppe Di Fatta, 2007-2013
19
20. Stream jargon
stream of bytes
file
stream of bytes
stream of bytes
Process
• A stream is a sequence
of characters that flow
into or out of a process.
• An input stream is
attached to some input
source for the process,
eg, keyboard or socket.
• An output stream is
attached to an output
source, eg, monitor or
socket.
Input streams
Internet
Output streams
stream of bytes
file
stream of bytes
stream of bytes
Java, Dr. Giuseppe Di Fatta, 2007-2013
Internet
20
21. Socket programming with TCP
Process
Client
Process
output
stream
monitor
inFromUser
input
stream
outToServer
1) client reads line from standard
input (inFromUser stream) ,
sends to server via socket
(outToServer stream)
2) server reads line from socket
3) server converts line to
uppercase, sends back to client
4) client reads, prints modified line
from socket (inFromServer
stream)
keyboard
inFromServer
Example client-server app:
input
stream
client TCP
clientSocket
socket
to network
Java, Dr. Giuseppe Di Fatta, 2007-2013
TCP
socket
from network
21
22. Client/server socket interaction: TCP
2
4
Server
Client
TCP connection
3
-1
1
create socket for
incoming request:
welcomeSocket =
ServerSocket()
TCP connection setup
wait for incoming
connection request
connectionSocket =
welcomeSocket.accept()
read request from
connectionSocket
write reply to
connectionSocket
close
connectionSocket
2
3
create socket to server
0
clientSocket =
Socket()
reliable fullduplex channel
1
send request using
clientSocket
read reply from
4 clientSocket
close
clientSocket
Java, Dr. Giuseppe Di Fatta, 2007-2013
22
23. Example: Java TCP client (1)
public static void main(String argv[])
{
String sentence;
String modifiedSentence;
Create
input stream
Create
client socket,
connect to server
Create
output stream
attached to socket
BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in));
Socket clientSocket = new Socket("hostname", 6789);
DataOutputStream outToServer =
new DataOutputStream(clientSocket.getOutputStream());
Note: to run this example you still need to add some code to manage exceptions.
Java, Dr. Giuseppe Di Fatta, 2007-2013
23
24. Example: Java TCP client (2)
Create
input stream
attached to socket
BufferedReader inFromServer =
new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
sentence = inFromUser.readLine();
Send line
to server
outToServer.writeBytes(sentence + 'n');
Read line
from server
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " + modifiedSentence);
clientSocket.close();
}
Note: to run this example you still need to add some code to manage exceptions.
Java, Dr. Giuseppe Di Fatta, 2007-2013
24
25. Example: Java TCP server (1)
Create
welcoming socket
at port 6789
Wait, on welcoming
socket for contact
by client
Create input
stream, attached
to socket
public static void main(String argv[])
{
String clientSentence;
String capitalizedSentence;
ServerSocket welcomeSocket = new ServerSocket(6789);
while(true) {
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient =
new BufferedReader(new
InputStreamReader(connectionSocket.getInputStream()));
Note: to run this example you still need to add some code to manage exceptions.
Java, Dr. Giuseppe Di Fatta, 2007-2013
25
26. Example: Java TCP server (2)
Create output
stream, attached
to socket
DataOutputStream outToClient =
new DataOutputStream(connectionSocket.getOutputStream());
Read in line
from socket
clientSentence = inFromClient.readLine();
capitalizedSentence = clientSentence.toUpperCase() + 'n';
Write out line
to socket
outToClient.writeBytes(capitalizedSentence);
}
}
End of while loop,
loop back and wait for
another client connection
Note: to run this example you still need to add some code to manage exceptions.
Java, Dr. Giuseppe Di Fatta, 2007-2013
26
27. Socket programming with UDP
UDP: no “connection” between
client and server
• no handshaking
• sender explicitly attaches IP
address and port of
destination to each packet
• server must extract IP
address, port of sender from
received packet
application viewpoint
UDP provides unreliable transfer
of groups of bytes (“datagrams”)
between client and server
UDP: transmitted data may be
received out of order, or lost
Java, Dr. Giuseppe Di Fatta, 2007-2013
27
28. Client/server socket interaction: UDP
Server (running on hostid)
create socket,
port=x, for
incoming request:
serverSocket =
DatagramSocket()
read request from
serverSocket
write reply to
serverSocket
specifying client
host address,
port number
Client
create socket,
clientSocket =
DatagramSocket()
Create, address (hostid, port=x,
send datagram request
using clientSocket
read reply from
clientSocket
close
clientSocket
Java, Dr. Giuseppe Di Fatta, 2007-2013
28
29. Example: Java client (UDP)
input
stream
Client
process
monitor
inFromUser
keyboard
Process
Input: receives
packet (TCP
received “byte
stream”)
UDP
packet
receivePacket
packet (TCP sent
“byte stream”)
sendPacket
Output: sends
client UDP
clientSocket
socket
to network
UDP
packet
UDP
socket
from network
Java, Dr. Giuseppe Di Fatta, 2007-2013
29
30. Example: Java UDP client (1)
Create
input stream
Create
client socket
Translate
hostname to IP
address using DNS
public static void main(String args[])
{
BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in));
DatagramSocket clientSocket = new DatagramSocket();
InetAddress IPAddress = InetAddress.getByName("hostname");
byte[] sendData = new byte[1024];
byte[] receiveData = new byte[1024];
String sentence = inFromUser.readLine();
sendData = sentence.getBytes();
Note: to run this example you still need to add some code to manage exceptions.
Java, Dr. Giuseppe Di Fatta, 2007-2013
30
31. Example: Java UDP client (2)
Create datagram
with data-to-send,
length, IP addr, port
DatagramPacket sendPacket =
new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
Send datagram
to server
clientSocket.send(sendPacket);
Read datagram
from server
clientSocket.receive(receivePacket);
DatagramPacket receivePacket =
new DatagramPacket(receiveData, receiveData.length);
String modifiedSentence =
new String(receivePacket.getData());
System.out.println("FROM SERVER:" + modifiedSentence);
clientSocket.close();
}
Note: to run this example you still need to add some code to manage exceptions.
Java, Dr. Giuseppe Di Fatta, 2007-2013
31
32. Example: Java UDP server (1)
Create
datagram socket
at port 9876
public static void main(String args[])
{
DatagramSocket serverSocket = new DatagramSocket(9876);
byte[] receiveData = new byte[1024];
byte[] sendData = new byte[1024];
while(true)
{
Create space for
received datagram
Receive
datagram
DatagramPacket receivePacket =
new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
Note: to run this example you still need to add some code to manage exceptions.
Java, Dr. Giuseppe Di Fatta, 2007-2013
32
33. Example: Java UDP server (2)
String sentence = new String(receivePacket.getData());
Get IP addr
port #, of
sender
InetAddress IPAddress = receivePacket.getAddress();
int port = receivePacket.getPort();
String capitalizedSentence = sentence.toUpperCase();
sendData = capitalizedSentence.getBytes();
Create datagram
to send to client
DatagramPacket sendPacket =
new DatagramPacket(sendData, sendData.length, IPAddress, port);
Write out
datagram
to socket
serverSocket.send(sendPacket);
}
}
End of while loop,
loop back and wait for
another datagram
Note: to run this example you still need to add some code to manage exceptions.
Java, Dr. Giuseppe Di Fatta, 2007-2013
33
34. Electronic Mail
outgoing
message queue
Three major components:
• user agents
• mail servers
• Simple Mail Transfer Protocol
(SMTP)
User Agent
• a.k.a. “mail reader”
• composing, editing, reading mail
messages
• e.g., Eudora, Outlook, Elm,
Mozilla Thunderbird
• outgoing, incoming messages
stored on server
user
agent
user mailbox
mail
server
SMTP
SMTP
mail
server
SMTP
user
agent
mail
server
user
agent
user
agent
user
agent
user
agent
Java, Dr. Giuseppe Di Fatta, 2007-2013
34
35. Mail Servers
Mail Servers
• mailbox contains incoming
messages for user
• message queue of outgoing
(to be sent) mail messages
SMTP protocol between mail
servers to send email
messages
– client: sending mail server
– “server”: receiving mail
server
SMTP
mail
server
user
agent
SMTP
mail
server
SMTP
mail
server
Java, Dr. Giuseppe Di Fatta, 2007-2013
IMAP
or
POP3
user
agent
35
36. SMTP [RFC 2821]
http://tools.ietf.org/html/rfc2821
http://tools.ietf.org/html/rfc821
• uses TCP to reliably transfer email message from client to
server: TCP port 25
• direct transfer: sending server (or user client) to receiving server
• 3 phases of transfer
– handshaking (greeting)
– transfer of messages
– closure
• command/response interaction
– commands: ASCII text
– response: status code and phrase
• messages must be in 7-bit ASCII
Java, Dr. Giuseppe Di Fatta, 2007-2013
36
37. Try SMTP yourself
We can send email without
using an email client
(thunderbird, outlook, etc.)
We can talk SMTP directly to
the server:
telnet servername 25
see “220” reply from server
Commands:
–
–
–
–
–
HELO
MAIL FROM
RCPT TO
DATA
QUIT
Replies:
– 220 mailserver time
– 250 ok
– 500 unrecognized command
– 501 Syntactically invalid HELO
argument(s)
user
agent
SMTP
mail
server
telnet+SMTP
TCP
IP
Ethernet
telnet
connection
> telnet smtp.reading.ac.uk 25
220 vimp1.rdg.ac.uk ESMTP Exim Wed, 28 Feb 2007 16:40
helo whatsoever.blabla.org
250 vimp1.rdg.ac.uk Hello imogen.rdg.ac.uk
mail from: <myUserID@reading.ac.uk>
250 OK
rcpt to: <toSomeone@reading.ac.uk>
250 Accepted
data
354 Enter message, ending with "." on a line by itself
Hi there!
bye.
.
250 OK id=1HMRrh-0007OW-BX
quit
221 vimp1.rdg.ac.uk closing connection
>
– …
Java, Dr. Giuseppe Di Fatta, 2007-2013
37
38. SMTP: final words
• SMTP uses persistent connections
• SMTP requires message (header & body) to be in 7-bit ASCII
• SMTP server uses CRLF.CRLF to determine end of message
SMTP: protocol for exchanging
email msgs
RFC 822: standard for text
message format:
• header lines, e.g.,
– to, from, subject
different from SMTP commands!
• body
header
blank
line
body
– the message-body
• Termination sequence
– “n.n”
Java, Dr. Giuseppe Di Fatta, 2007-2013
38
39. Java Code: send an email (1/2)
private static void pause(int seconds) {
long msec = 1000 * seconds;
try {
Thread.sleep(msec);
} catch (InterruptedException e) {
}
}
public static String sendCommand(Socket theSocket, String cmd)
{
DataOutputStream outToServer = new DataOutputStream(theSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new
InputStreamReader(theSocket.getInputStream()));
pause(1); //needed for some MS servers!
System.out.println(“SENT TO SERVER: " + cmd);
outToServer.writeBytes(cmd + 'n');
String res = inFromServer.readLine();
System.out.println(“REPLY FROM SERVER: " + res);
System.out.println();
return res;
}
Note: to run this example you still need to add some code to manage exceptions.
Java, Dr. Giuseppe Di Fatta, 2007-2013
39
40. Java Code: send an email (2/2)
public static void main(String args[]) {
String mailServer = "smtp.reading.ac.uk";
int smtp_port = 25;
String myEmailAddress = "G.DiFatta@reading.ac.uk";
String dstAddress = "G.DiFatta@reading.ac.uk";
String command, answer;
Socket clientSocket = new Socket(mailServer, smtp_port);
command = "helo myself.uk";
answer = sendCommand(clientSocket, command);
command = "mail from: <" + myEmailAddress + ">";
answer = sendCommand(clientSocket, command);
command = "rcpt to: <" + dstAddress + ">";
answer = sendCommand(clientSocket, command);
command = "data";
answer = sendCommand(clientSocket, command);
//send the content of the email:
StringBuffer data = new StringBuffer();
data.append("blabla...n");
data.append("blabla...");
command = data.toString() + "n.n";
answer = sendCommand(clientSocket, command);
command = "quit";
answer = sendCommand(clientSocket, command);
clientSocket.close();
}
Note: to run this example you still need to add some code to manage exceptions.
Java, Dr. Giuseppe Di Fatta, 2007-2013
40
41. Java Example: a simple FTP client
String ftp_url="ftp://anonymous:na@ftp.fu-berlin.de/pub/misc/movies/database/";
String type = ";type=i";
String file1 = "filesizes";
try{
String filename=file1;
URL url = new URL(ftp_url+filename+type);
URLConnection urlc = url.openConnection();
InputStream is = urlc.getInputStream();
//For text files use BufferedReader and InputStreamReader
//For binary files use BufferedInputStream
BufferedReader bin = new BufferedReader(new InputStreamReader(is));
String line;
System.out.println("--------- "+filename+" -----------");
while((line = bin.readLine()) != null){
System.out.println(line);
}
System.out.println("--------------------");
bin.close();
} catch (MalformedURLException e1) {
System.out.println(e1);
e1.printStackTrace();
} catch (IOException e1) {
System.out.println(e1);
e1.printStackTrace();
}
Java, Dr. Giuseppe Di Fatta, 2007-2013
41
42. Conclusions
Java is:
Object-Oriented Programming (OOP) language
Strongly typed, data declarations, statements
A standard set of Application Programming Interfaces (API)
Main features:
Garbage collection
Threads
Exceptions
Network oriented
Network programming in Java is easy and fun.
These lecture slides are available at:
http://www.personal.reading.ac.uk/~sis06gd/resources.html
Java, Dr. Giuseppe Di Fatta, 2007-2013
42