Beruflich Dokumente
Kultur Dokumente
Core to Java EE
Collection
Generics
Collection
A collection is a container object that holds a
group of objects known as elements.
It contains operations for Adding, Updating, and
Removing elements
A Collections framework is a unified architecture
for representing and manipulating collections. It
has:
Collection Interface
Collection Interface
Collection Interface
Wildcards in Collection
Collection Framework
TreeSet
Set
AbstractSet
Collection
HashSet
LinkedHashSet
AbstractCollection
Vector
AbstractList
List
ArrayList
AbstractSequentialList
LinkedList
Deque
Queue
Interfaces
AbstractQueue
Abstract Classes
PriorityQueue
Concrete Classes
Stack
SortedMap
Map
TreeMap
AbstractMap
Interfaces
Abstract Classes
HashMap
Concrete Classes
LinkedHashMap
Array List
Link List
Set
balanced
tree
ArrayList
Vector
HashMap
Hashtable
linked list
TreeSet
(sortedSet)
HashSet
List
Map
Resizable
array
LinkedList
TreeMap
(sortedMap)
Network Programming
Client-Server Model
Client
Client
Server
Client-Server Model
Problem?
.
Concurrent Servers
1.
2.
3.
Decisions
Client
Server
Welcoming
Socket
handshake
Client
socket
bytes
bytes
Connection
Socket
2.
3.
4.
Client Process
Out to monitor
Out to server
In from server
In from user
Client
TCP
Socket
To Network
From Network
BufferedReader inFromUser =
new BufferedReader(
new InputStreamReader(System.in));
Socket clientSocket =
new Socket(hostName, port);
Create
output stream
attached to socket
Create
input stream
attached to
socket
Send line
to server
Read line
from server
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " +
modifiedSentence);
clientSocket.close();
Close the
connection
}
}
4.
5.
Server Port
Create
welcome socket
at port 6789
ServerSocket welcomeSocket =
new ServerSocket(SERVER_PORT);
Wait, on welcome
socket for contact
by client
Create
Input stream,
attached
to socket
Create output
stream,
attached
to socket
clientSentence = inFromClient.readLine();
capitalizedSentence =
clientSentence.toUpperCase()+\r\n;
outToClient.print(capitalizedSentence);
}
}
}
Client
create socket,
connect to hostid, port=6789
clientSocket =
new Socket (hostid,
6789)
send request
using
clientSocket
Create socket
Loop
Send Message To Well-known port
of the Server
Receive Message From Server
Close Socket
BufferedReader inFromUser =
new BufferedReader(
new InputStreamReader(System.in));
Create datagram
with data-tosend,
length, IP addr,
port
Send datagram
to server
DatagramSocket clientSocket =
new DatagramSocket();
InetAddress ipAddress =
InetAddress.getByName(hostName);
byte[] sendData = new byte[1024];
byte[] receiveData = new byte[1024];
sentence = inFromUser.readLine();
sendData = sentence.getBytes();
DatagramPacket sendPacket = new DatagramPacket(
sendData, sendData.length, ipAddress, port);
clientSocket.send(sendPacket);
Read
datagram
from server
modifiedSentence =
new String(receivePacket.getData());
System.out.println("FROM SERVER:" +
modifiedSentence);
Close the
connection
clientSocket.close();
}
}
Create socket
Bind socket to a specific port where
clients can contact you
Loop
Receive UDP Message from client x
Send UDP Reply to client x
Close Socket
Create
datagram
socket
at port 6789
Create space
for
received
datagram
Receive
datagram
InetAddress IPAddress =
receivePacket.getAddress();
int port = receivePacket.getPort();
String capitalizedSentence =
sentence.toUpperCase();
sendData = capitalizedSentence.getBytes();
Create
datagram
to send to
client
Write out
datagram
to socket
DatagramPacket sendPacket =
new DatagramPacket(sendData,
sendData.length, IPAddress, port);
serverSocket.send(sendPacket);
}
}
}
TCP
UDP
Encapsulates an address
Performs name lookup (converting a host name
into an IP address)
Performs reverse lookup (converting the address
into a host name)
java.net.InetAddress (1)
java.net.InetAddress (2)
String getHostName()
Gives you the host name (for example www.sun.com)
String getHostAddress()
Gives you the address (for example 192.18.97.241)
InetAddress getLocalHost()
InetAddress[] getAllByName(String hostName)
What Is a URL?
http
Protocol
Identifier
//www.java.sun.com
Resource
Identifier
Examples:
Host Name
Port Number
File name
Reference
Creating URL
java.net.URL
Absolute URL
Relative URL
WEB
Server
/pages/index.html
game.html
net.html
http://www.gamelan.com/pages/game.
html
http://www.gamelan.com/pages/net.h
tml
try {
URL gamelan = new URL("http",
"www.gamelan.com", 80,
"pages/Gamelan.network.html");
} catch (MalFormedURLException ex){
// exception handler code here
Parsing a URL
getProtocol
getHost
getFile
getPort
getRef
Output
protocol = http
host = java.sun.com
filename = docs/books/tutorial/index.html
port = 80
ref = DOWNLOADING
Create
URL
Open
the URL
Input
stream
Read every
thing from
URL and
print
on std out
Writing to a URLConnection
Create a URL
Open a connection to the URL
Set output capability on the URLConnection
Get an output stream from the connection. This
output stream is connected to the standard input
stream of the cgi-bin script on the server
Write to the output stream
Close the output stream
Use http://java.sun.com/cgi-bin/backwards for
testing
Writing to a URLConnection
public class Reverse {
public static void main(String[] args)
throws Exception {
if (args.length != 1) {
System.err.println("Usage: java Reverse " +
"string_to_reverse");
System.exit(1);
}
String stringToReverse=URLEncoder.encode(args[0]);
URL url = new URL(
"http://java.sun.com/cgi-bin/backwards");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
Example contd.
PrintWriter out = new PrintWriter(
conn.getOutputStream());
out.println("string=" + stringToReverse);
out.close();
BufferedReader in = new BufferedReader(
new InputStreamReader(
conn.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}
Questions?