Java Networking Classes and Their Methods
1. URL ([Link])
- URL(String spec): Creates a URL object from a String.
- getProtocol(): Returns the protocol (http, https, ftp, etc.).
- getHost(): Returns the hostname.
- getPort(): Returns the port number.
- getPath(): Returns the file path.
- openConnection(): Opens a connection to the URL.
2. URLConnection ([Link])
- connect(): Opens a communications link.
- getInputStream(): Returns an input stream for reading.
- getOutputStream(): Returns an output stream for writing.
- setDoOutput(boolean): Sets the flag for output capability.
3. HttpURLConnection ([Link])
- setRequestMethod(String): Sets the HTTP method (GET, POST).
- getResponseCode(): Gets the response code from the server.
- getResponseMessage(): Gets the response message.
- disconnect(): Disconnects the connection.
4. InetAddress ([Link])
- getByName(String): Returns IP address of the host.
- getLocalHost(): Returns the local host address.
- getHostAddress(): Returns the IP address string.
- getHostName(): Returns the hostname.
5. ServerSocket ([Link])
- ServerSocket(int port): Creates a server socket.
Java Networking Classes and Their Methods
- accept(): Listens and accepts a connection.
- close(): Closes the server socket.
6. Socket ([Link])
- Socket(String host, int port): Creates a client socket.
- getInputStream(): Returns input stream.
- getOutputStream(): Returns output stream.
- close(): Closes the socket.
7. DatagramSocket ([Link])
- DatagramSocket(): Constructs a datagram socket.
- send(DatagramPacket): Sends a datagram packet.
- receive(DatagramPacket): Receives a datagram packet.
- close(): Closes the socket.
8. DatagramPacket ([Link])
- DatagramPacket(byte[] buf, int length): Creates a packet for receiving.
- DatagramPacket(byte[] buf, int length, InetAddress, int port): For sending.
- getData(): Returns the data buffer.
- getLength(): Returns the length of data.