Stanislav Zorjan - Stasha - Full Stack Software Engineer and Game Development Hobbyist - Prague


1. Java - Primitives
2. Java - Strings
3. Java - Arrays
4. Java - Date
5. Java - Generics
6. Java - Collections
7. Java - Localization
8. Java - Formatting
9. Java - Regular Expressions
10. Java - System
11. Java - Serialization
12. Java - Multithreading
13. Java - IO/File
14. Java - Networking
15. Java - ORM - JPA
      a) Simple User entity class

 

 

package basics.basicsExamples;

import java.io.*;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;


/** ****************************************************************************
 * NETWORKING EXAMPLE CLASS
 * @author stanislav.zorjan
 * 
 * TODO Reading from sockets
 * TODO Writing to sockets
 * TODO UDP 
 */
public class NetworkingExample {

    URL url;
    URLStreamHandler ush;
    
    
    /** ************************************************************************
     * Default constructor
     */
    public NetworkingExample() {
	//createURL();
	//urlOperations();
	//readingURL();
	//writeToURL(); 
	readingFromSocket();
    }
    
    
    /** ************************************************************************
     * Method for creating url's
     */
    private void createURL(){
	try {
	    
	    ush = new sun.net.www.protocol.http.Handler();
	    
	    //ush = new sun.net.www.protocol.https.Handler();
	    //ush = new sun.net.www.protocol.ftp.Handler();
	    //ush = new sun.net.www.protocol.jar.Handler();
	    //ush = new sun.net.www.protocol.file.Handler();
	    //ush = new sun.net.www.protocol.mailto.Handler();
	    //ush = new sun.net.www.protocol.netdoc.Handler();
	    //ush = new sun.net.www.protocol.gopher.Handler();
	    
	    
	    //http://java.sun.com/developer/onlineTraining/protocolhandlers/
	    //ush = new URLStreamHandler() {
	    //
	    //	@Override
	    //	protected URLConnection openConnection(URL url) throws IOException {
	    //	   
	    //	    return new HttpURLConnection();
	    //	   
	    //	}
	    //};
	    
	    
	    
	    
	    //url = new URL("http://www.yahoo.com");
	    //url = new URL(url, "page0");
	    //url = new URL("http", "www.yahoo.com", "/page0");
	    //url = new URL(url, "", ush);
	    url = new URL("http", "www.stasha.info", 80, "/index.php?id=150/#simpleAnchor");
	    //url = new URL("http", "www.yahoo.com", 80, "/page0", ush);
	    
	    System.out.println(url);
	    
	    System.out.println(URLEncoder.encode("#$#%$#%@#$5", "UTF-8"));
	    
	    
	    
	} catch (UnsupportedEncodingException | MalformedURLException ex) {
	    ex.printStackTrace();
	}
    }
    
    
    /** ************************************************************************
     * URL Methods
     */
    private void urlOperations(){
	
	    
	System.out.println(url.equals(url)); 
	System.out.println(url.getAuthority()); 
	//System.out.println(url.getContent()); 
	System.out.println(url.getDefaultPort());
	//System.out.println(url.getFile());
	System.out.println(url.getHost());
	System.out.println(url.getPath());
	System.out.println(url.getPort());
	System.out.println(url.getProtocol());
	System.out.println(url.getQuery());
	System.out.println(url.getRef());
	System.out.println(url.getUserInfo());
	System.out.println(url.hashCode());
	//System.out.println(url.openConnection());
	//System.out.println(url.openConnection(Proxy.NO_PROXY));
	System.out.println(url.sameFile(url));
	System.out.println(url.toExternalForm());
	System.out.println(url.toString());

	try {

	    System.out.println(url.toURI());	

	} catch (URISyntaxException ex) {
	    Logger.getLogger(NetworkingExample.class.getName()).log(Level.SEVERE, null, ex);
	}
	
    }
    
    /** ************************************************************************
     * Method for displaying url content
     */
    private void readingURL(){
	
	
	InputStreamReader bis;
	BufferedReader br;
	
	try {
	    url = new URL("http://www.yahoo.com");
	
	    bis = new InputStreamReader(url.openStream());
	    br = new BufferedReader(bis);
	    
	    StringBuilder sb = new StringBuilder();
	    String str;
	    
	    while((str = br.readLine()) != null){
		sb.append(str).append("\n");
	    }
	    
	    bis.close();
	    br.close();
	    
	    
	    System.out.println(sb);
	    
	} catch (IOException ex) {
	   ex.printStackTrace();
	}
	
    }
    
    /** ************************************************************************
     * Method for writing to url
     */
    private void writeToURL(){
	
	try {
	    
	    url = new URL("http://www.testforexample.eu");
	    URLConnection uc = url.openConnection();
	    
	    uc.setDoOutput(true);
	    
	    OutputStreamWriter osw = new OutputStreamWriter(uc.getOutputStream());
	    osw.write("string to write");
	    osw.close();
	    
	    
	} catch (IOException ex) {
	    ex.printStackTrace();
	}
    }
    
    private void readingFromSocket(){
	try {
	    
	    
	    Socket socket = new Socket("http://www.yahoo.com", 80);
	    
	    BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
	    
	    StringBuilder sb = new StringBuilder();
	    String str;
	    
	    while((str = br.readLine()) != null){
		sb.append(str).append("\n");
	    }
	    
	    br.close();
	    
	    
	    System.out.println(sb);
	    
	    
	} catch (UnknownHostException ex) {
	    Logger.getLogger(NetworkingExample.class.getName()).log(Level.SEVERE, null, ex);
	} catch (IOException ex) {
	    Logger.getLogger(NetworkingExample.class.getName()).log(Level.SEVERE, null, ex);
	}
	
    }
    
    private void writingToSocket(){
	// TODO Writing to socket
    }
    
}