1 Network Programming Introduction zBased on Classes in the java.net package zLecture focuses on:...
-
Upload
joshua-harper -
Category
Documents
-
view
222 -
download
0
Transcript of 1 Network Programming Introduction zBased on Classes in the java.net package zLecture focuses on:...
![Page 1: 1 Network Programming Introduction zBased on Classes in the java.net package zLecture focuses on: yTCP and UDP yNetwork programming basics yIdentifying.](https://reader036.fdocuments.us/reader036/viewer/2022082517/56649d985503460f94a81dbd/html5/thumbnails/1.jpg)
1
Network ProgrammingIntroduction
Based on Classes in the java.net package
Lecture focuses on: TCP and UDP Network programming basics Identifying a machine Servers and Clients Ports and sockets Data transfer using sockets
![Page 2: 1 Network Programming Introduction zBased on Classes in the java.net package zLecture focuses on: yTCP and UDP yNetwork programming basics yIdentifying.](https://reader036.fdocuments.us/reader036/viewer/2022082517/56649d985503460f94a81dbd/html5/thumbnails/2.jpg)
Lots of java programs use Transmission Control Protocol (TCP)
Connection-based (where java stream sockets provides continuous data stream), reliable, data streams will always get there. Also high overhead. We will focus on TCP/IP sockets.
User Datagram Protocol (UDP) is not connection-based (connectionless service with datagram sockets allowing one message, an unreliable protocol which is much faster, but the message won’t always get there
TCP and UDP
![Page 3: 1 Network Programming Introduction zBased on Classes in the java.net package zLecture focuses on: yTCP and UDP yNetwork programming basics yIdentifying.](https://reader036.fdocuments.us/reader036/viewer/2022082517/56649d985503460f94a81dbd/html5/thumbnails/3.jpg)
Network Programming Basics•The classes in java.net: Java programs can use TCP or UDP to communicate over the Internet. The URL, URLConnection (for Web applications), Socket, and ServerSocket (client-server applications) classes all use TCP to communicate over the network.•The DatagramPacket, DatagramSocket, and MulticastSocket classes are for use with UDP.
![Page 4: 1 Network Programming Introduction zBased on Classes in the java.net package zLecture focuses on: yTCP and UDP yNetwork programming basics yIdentifying.](https://reader036.fdocuments.us/reader036/viewer/2022082517/56649d985503460f94a81dbd/html5/thumbnails/4.jpg)
Network Programming Basics
Historically error-prone, difficult, complex
JAVA has complete network package, java.net
I/O stream library works quite well for TCP/IP
Threading is also very useful and relatively easy here
![Page 5: 1 Network Programming Introduction zBased on Classes in the java.net package zLecture focuses on: yTCP and UDP yNetwork programming basics yIdentifying.](https://reader036.fdocuments.us/reader036/viewer/2022082517/56649d985503460f94a81dbd/html5/thumbnails/5.jpg)
Identifying a Machine
Uniquely identify a machine from all the others in the world
IP (Internet Protocol) address that can exist in two forms:
![Page 6: 1 Network Programming Introduction zBased on Classes in the java.net package zLecture focuses on: yTCP and UDP yNetwork programming basics yIdentifying.](https://reader036.fdocuments.us/reader036/viewer/2022082517/56649d985503460f94a81dbd/html5/thumbnails/6.jpg)
Identify a Machine(Continue)
DNS(Domain Name Service) formjava.sun.com
“Dotted quad” form:123.255.28.120
static InetAddress.getByName() produces java object containing address
![Page 7: 1 Network Programming Introduction zBased on Classes in the java.net package zLecture focuses on: yTCP and UDP yNetwork programming basics yIdentifying.](https://reader036.fdocuments.us/reader036/viewer/2022082517/56649d985503460f94a81dbd/html5/thumbnails/7.jpg)
Servers and Clients
Two machines must connectServer waits for connectionClient initiates connection (create
socket)Once the connection is made, server
& client look identical
![Page 8: 1 Network Programming Introduction zBased on Classes in the java.net package zLecture focuses on: yTCP and UDP yNetwork programming basics yIdentifying.](https://reader036.fdocuments.us/reader036/viewer/2022082517/56649d985503460f94a81dbd/html5/thumbnails/8.jpg)
Testing w/o a network
For testing your program, you can do it w/o network. (server & client on same machine)
Localhost: the IP address for testing without a network
Three ways to identify:
![Page 9: 1 Network Programming Introduction zBased on Classes in the java.net package zLecture focuses on: yTCP and UDP yNetwork programming basics yIdentifying.](https://reader036.fdocuments.us/reader036/viewer/2022082517/56649d985503460f94a81dbd/html5/thumbnails/9.jpg)
Test w/o networking(Continue)
InetAddress addr=InetAddress.getByName(null);
Equivalently: InetAddress.getByName(“localhost”);
Or using the reserved IP number for the loopback: InetAddress.getByName(“127.0.0.1”);
![Page 10: 1 Network Programming Introduction zBased on Classes in the java.net package zLecture focuses on: yTCP and UDP yNetwork programming basics yIdentifying.](https://reader036.fdocuments.us/reader036/viewer/2022082517/56649d985503460f94a81dbd/html5/thumbnails/10.jpg)
Port number
IP address isn’t enough to identify a unique server
many servers can exist on one machine
port number: Unique in a MachineNot a physical location, but a
software abstraction to represent a service
![Page 11: 1 Network Programming Introduction zBased on Classes in the java.net package zLecture focuses on: yTCP and UDP yNetwork programming basics yIdentifying.](https://reader036.fdocuments.us/reader036/viewer/2022082517/56649d985503460f94a81dbd/html5/thumbnails/11.jpg)
Port number (Continue)
When set up client and server, you must specify IP address and port.
Port number range: 0 to 65,535 1-1024 are reserved, others may be
used.
![Page 12: 1 Network Programming Introduction zBased on Classes in the java.net package zLecture focuses on: yTCP and UDP yNetwork programming basics yIdentifying.](https://reader036.fdocuments.us/reader036/viewer/2022082517/56649d985503460f94a81dbd/html5/thumbnails/12.jpg)
Sockets
Software abstraction used to represent the connection between two machines
Socket is the actual 2-way connector.
![Page 13: 1 Network Programming Introduction zBased on Classes in the java.net package zLecture focuses on: yTCP and UDP yNetwork programming basics yIdentifying.](https://reader036.fdocuments.us/reader036/viewer/2022082517/56649d985503460f94a81dbd/html5/thumbnails/13.jpg)
Sockets
The following sequence establishes a socket from the client class
InetAddress addr =
InetAddress.getByName(null);Socket socket =
new Socket(addr, 8080);
![Page 14: 1 Network Programming Introduction zBased on Classes in the java.net package zLecture focuses on: yTCP and UDP yNetwork programming basics yIdentifying.](https://reader036.fdocuments.us/reader036/viewer/2022082517/56649d985503460f94a81dbd/html5/thumbnails/14.jpg)
Sockets
At the server class, the following establishes the server port, and waits for a client request to establish a socket
ServerSocket s = new
ServerSocket(8080);Socket socket = s.accept();
![Page 15: 1 Network Programming Introduction zBased on Classes in the java.net package zLecture focuses on: yTCP and UDP yNetwork programming basics yIdentifying.](https://reader036.fdocuments.us/reader036/viewer/2022082517/56649d985503460f94a81dbd/html5/thumbnails/15.jpg)
Data transfer using sockets
Once you have a Socket, you call getInputStream() and getOutputStream() to produce the corresponding InputStream and OutputStream objects
You can convert these to readers and writers, wrap them in a BufferedReader or BufferedWriter and PrintWriter
![Page 16: 1 Network Programming Introduction zBased on Classes in the java.net package zLecture focuses on: yTCP and UDP yNetwork programming basics yIdentifying.](https://reader036.fdocuments.us/reader036/viewer/2022082517/56649d985503460f94a81dbd/html5/thumbnails/16.jpg)
Data transfer using sockets(continue)
At the server and client classesBufferedReader in = new BufferedReader( new InputStreamReader( socket.getInputStream()));PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(
socket.getOutputStream())),true);
![Page 17: 1 Network Programming Introduction zBased on Classes in the java.net package zLecture focuses on: yTCP and UDP yNetwork programming basics yIdentifying.](https://reader036.fdocuments.us/reader036/viewer/2022082517/56649d985503460f94a81dbd/html5/thumbnails/17.jpg)
Data transfer using sockets (continue)
From then on, it’s like reading and writing any other I/O stream!
while (true) { String str = in.readLine(); if (str.equals("END")) break; System.out.println("Echoing: " +
str); out.println(str); }
Example: JabberClient JabberServer