UNIT 5 MEGA NOTES JAVA PROGRAMMING (2)
UNIT 5 MEGA NOTES JAVA PROGRAMMING (2)
1. Socket Overview
2. Client Socket
Syntax:
Socket s = new Socket("localhost", 1234);
import java.net.*;
import java.io.*;
3. Server Socket
� 7-Line Notes:
Syntax:
1. Proxy Servers
Syntax:
Simple Example:
import java.net.*;
import java.io.*;
public class ProxyExample {
public static void main(String[] args) {
try {
Proxy proxy = new Proxy(Proxy.Type.HTTP, new
InetSocketAddress("123.45.67.89", 8080));
URL url = new URL("http://example.com");
HttpURLConnection conn = (HttpURLConnection)
url.openConnection(proxy);
BufferedReader br = new BufferedReader(new
InputStreamReader(conn.getInputStream()));
System.out.println("Connected through proxy");
br.close();
} catch (Exception e) {
System.out.println("Error: " + e);
}
}
}
2. Internet Addressing
Syntax:
InetAddress ip = InetAddress.getByName("www.google.com");
Simple Example:
import java.net.*;
Output (example):
IP Address: 142.250.195.132
Host Name: www.google.com
5.2 Java and the Net: The Networking Classes and Interfaces
1. InetAddress Class
Syntax:
InetAddress ip = InetAddress.getByName("www.google.com");
Simple Example:
import java.net.*;
� 7-Line Notes:
Syntax:
Simple Example:
import java.io.*;
import java.net.*;
� 7-Line Notes:
Syntax:
Simple Example:
import java.io.*;
import java.net.*;
� 7-Line Notes:
Syntax:
InetAddress ip = InetAddress.getByName("www.google.com");
InetAddress local = InetAddress.getLocalHost();
Simple Example:
import java.net.*;
Output:
Syntax:
Simple Example:
import java.net.*;
import java.net.*;
import java.io.*;
import java.net.*;
import java.io.*;
� 7-Line Notes:
Syntax:
DatagramSocket Example:
import java.net.*;
import java.net.*;
1. URL Class
� 7-Line Notes:
Syntax:
URL url = new URL("https://www.google.com");
Simple Example:
import java.net.*;
Output:
Protocol: https
Host: www.google.com
File:
2. URLConnection Class
� 7-Line Notes:
Syntax:
URLConnection con = url.openConnection();
Simple Example:
import java.net.*;
import java.io.*;
Output: