Week 10 Flashcards
What is networking in Java?
Java provides a framework for communication between machines using:
TCP (Transmission Control Protocol): Reliable, connection-based
UDP (User Datagram Protocol): Unreliable, connectionless
What are sockets in networking?
A socket is a connection endpoint for sending and receiving data between two machines
Sockets are bound to a specific port on a server to listen for client requests
What are the differences between TCP and UDP?
TCP:
Connection-based
Guarantees data delivery and order
Error-checked and reliable
Example applications: HTTP, FTP
UDP:
Connectionless
No guarantee of delivery or order
Lightweight and faster due to minimal overhead
Example applications: Streaming, DNS
What are ports in networking?
A port is a 16-bit number used to direct data to specific applications on a machine
Well-known ports: 0-1023 (e.g., port 80 for HTTP)
Used in both TCP and UDP communication
What is required for socket communication?
Server:
Runs on a specific machine (IP address) and listens on a port
Accepts client connections and creates a new socket for communication
Client:
Knows the server’s hostname and port
Opens a connection to the server’s socket
How do you read/write data using sockets in Java?
- Open a socket
- Open input/output streams to the socket
- Read and write data using the streams
- Close the streams
- Close the socket
What are Java classes for TCP and UDP communication?
TCP:
Socket and ServerSocket: For client-server communication
URL and URLConnection: For HTTP connections
UDP:
DatagramPacket: Represents a UDP packet
DatagramSocket: For sending and receiving packets
MulticastSocket: For group communication
How is data sent and received with UDP?
Data is sent as independent packets (datagrams)
No dedicated channel is established
The receiver must handle missing or out-of-order packets
What are client-server pairs in Java?
Server:
Creates a ServerSocket to listen for incoming connections
Accepts a client connection and communicates via a new Socket
Client:
Creates a Socket to connect to the server’s IP and port
Exchanges data with the server through streams
What are common uses of TCP and UDP?
TCP:
Reliable communication for web services, file transfers
UDP:
Fast communication for streaming, gaming, or broadcasting
What are the key advantages of using TCP over UDP?
Ensures data integrity and order
Better for applications where reliability is critical
What are the advantages of UDP over TCP?
Lower latency
Suitable for real-time applications where minor data loss is acceptable
What is Java RMI?
A framework allowing objects in one JVM to invoke methods on objects in another JVM
Facilitates remote communication between Java programs on different machines
What are the basic components of RMI?
Registry: Maps names to remote objects using bind and rebind
Server: Registers remote objects with the RMI registry and waits for client invocations
Client: Looks up remote objects in the registry and invokes methods on them
What is required for an object to be remote?
The object must implement an interface extending java.rmi.Remote
Methods in the interface must declare throws java.rmi.RemoteException