RSS

Tag Archives: Java

All java

Java Networking: Enabling your app to access internet through a proxy server

So in my earlier blog post I made a web browser and all, and the first problem I encountered was getting it to connect to the internet. Why this is a problem is because I access the internet through a proxy server. Most institutions have proxies and can only access the internet at a particular port so its kind of nice to let them connect. Its just 3 lines of code:

System.getProperties().put("proxySet","true"); //Tells java that you'll be connecting through a proxy
System.getProperties().put("proxyHost","10.223.0.2"); // and the port the server is listening in
System.getProperties().put("proxyPort","3128"); //specifies the servers ip

 

 
Leave a comment

Posted by on June 28, 2012 in Code, Java

 

Tags: ,

Building the worst browser in Java

This is gonna be the worst browser you’ve ever seen. Java has a very poor support for Css/Javascript. But if you are trying to load a page in the web written a decade ago, then you just struck the jackpot. Anyway I’ll use this later on to show how other swing objects can be used.

and if you wanna try it out and improve it:

package wordpress.mycodeandlife.com;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;

public class WebBrowser extends JFrame implements ActionListener, HyperlinkListener{
	JTextField addressbar;
	JEditorPane page;
	JLabel status;

	WebBrowser(){
		super("gilo");
		addressbar = new JTextField();
		addressbar.addActionListener(this);

		page = new JEditorPane();
		page.setEditable(false);
		page.addHyperlinkListener(this);

		status = new JLabel();
		status.setText("Welcome to gilo");

		add(addressbar,BorderLayout.NORTH);
		add(status, BorderLayout.SOUTH);
		add(new JScrollPane(page),BorderLayout.CENTER);

		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(500,500);

		//These three are the proxy settings
		System.getProperties().put("proxySet","true");
		System.getProperties().put("proxyPort","3128");
		System.getProperties().put("proxyHost","10.223.0.2");
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		loadpage(e.getActionCommand());

	}

	@Override
	public void hyperlinkUpdate(HyperlinkEvent e) {
		if(e.getEventType()==HyperlinkEvent.EventType.ACTIVATED)
			loadpage(e.getURL().toString());
		else if(e.getEventType()==HyperlinkEvent.EventType.ENTERED)
			status.setText(e.getURL().toString());
		else if(e.getEventType() == HyperlinkEvent.EventType.EXITED)
			status.setText(" ");

	}

	public void loadpage(String url){

		status.setText("Loading "+ url + "....");
		try {

			page.setPage(url);
			addressbar.setText(url);
			this.setTitle(url);
			status.setText(url);

		} catch (IOException e) {
			status.setText("Error unable to connect to: " + url);
		}
	}

	public static void main(String args[]){

		WebBrowser brow = new WebBrowser();

	}

}

 
Leave a comment

Posted by on June 28, 2012 in Code, Java

 

Tags: ,

Java Networking

This week has been a busy one. I checked up on a lot of stuff majorly to do with Networking in Java. Below is the code for a chat app. I shall also provide links to some great resources. I do hope to add more features, currently improving it and then try file transfer.

<blockquote>/*ServerSide.java
* This is will be on the server and will always be running(its on an infinitive loop).
* The server accepts a client and in a new thread initializes a client socket for it.
* The clients 'PrintWriter' are stored in an array. When a client sends a message the message is received
* by the server and distributed through the PrintWriter's in the array.
*
*
*
*/

package com.gilo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Iterator;

public class ServerSide {
ArrayList&lt;PrintWriter&gt; cos; //Array of ClientOutputStreams

public class clientHandler implements Runnable{
BufferedReader reader;
Socket sock;

public clientHandler(Socket clientSocket){
try{
sock = clientSocket;
InputStreamReader isr = new InputStreamReader(sock.getInputStream());
reader = new BufferedReader(isr);

}catch(IOException e){
e.printStackTrace();
}

}

public void run(){
String message;

try{
while((message = reader.readLine()) != null){
telleveryone(message);
}

}catch(IOException err){
err.printStackTrace();
}
}

private void telleveryone(String message){
Iterator itr = cos.iterator();
while(itr.hasNext()){
PrintWriter pWriter = (PrintWriter) itr.next();
pWriter.println(message);
pWriter.flush();
}

}
}

public static void main(String args[]){
new ServerSide().createSocket();
}

public void createSocket(){
cos = new ArrayList&lt;PrintWriter&gt;();

try{
ServerSocket socket = new ServerSocket(5000);

while(true){
Socket clientSocket = socket.accept();
PrintWriter writer = new PrintWriter(clientSocket.getOutputStream());
cos.add(writer);

Thread t = new Thread(new clientHandler(clientSocket));
t.start();
}

}catch(IOException err){
System.out.println("ERROR: " + err.getMessage());
}

}

}</blockquote>

And the clientSide:

<blockquote>/*
* ClientSide.java
*
* The client socket takes in an ip address of the server as one of the arguments and the port
* Since I would test it on my own computer the ip provided is my computer's, so before running it change the ip.
* on cmd type on 'ipconfig' to get your computer's ip (look for something like ipv4 or 6 depending on the region you come from)
* on linux 'ifconfig' at the terminal will do.
*
*/

package com.gilo;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;

public class ClientSide extends JFrame{

JTextField outgoing;
JTextArea incoming;
BufferedReader reader;
PrintWriter writer;
Socket sock;
String username;

public ClientSide(){
super("Chat");
JPanel mainpanel = new JPanel();
incoming = new JTextArea(15,25);
incoming.setLineWrap(true);
incoming.setEditable(false);
JScrollPane qScroller = new JScrollPane(incoming);
qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);

outgoing = new JTextField(20);

outgoing.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
writer.println(username + " - " + e.getActionCommand());
writer.flush();

outgoing.setText("");
}catch(Exception err){
JOptionPane.showMessageDialog(null, "Error! Could not send the message \n"+err.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
}
}
});
JButton sendButton = new JButton("send");
sendButton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
writer.println(username + " - " + outgoing.getText());
writer.flush();

outgoing.setText("");
}catch(Exception err){
JOptionPane.showMessageDialog(null, "Error! Could not send the message \n"+err.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
}
}
});
mainpanel.add(qScroller);
mainpanel.add(outgoing);
mainpanel.add(sendButton);

setupNetworking(); //Sockets and connections are established here

Thread readerThread = new Thread(new IncomingReader());
readerThread.start();

getContentPane().add(BorderLayout.CENTER,mainpanel);

setSize(500,500);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void setupNetworking(){
try{
sock = new Socket("10.223.73.199",5000); //Change the ip address to one of your comp. see above for details
InputStreamReader isR = new InputStreamReader(sock.getInputStream());
reader = new BufferedReader(isR);
writer = new PrintWriter(sock.getOutputStream());

username =System.getProperty("user.name");
username =(String) JOptionPane.showInputDialog(null,"Username: ","Info",JOptionPane.INFORMATION_MESSAGE,null,null,username );

writer.println(username +" - " + " has joined the chat");
writer.flush();

}catch(UnknownHostException e){
e.printStackTrace();
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Error - Unable to connect to server \n " + e.getMessage(), "Error" , JOptionPane.ERROR_MESSAGE);

}
}

public static void main(String args[]){
ClientSide cs = new ClientSide();
}

public class IncomingReader implements Runnable{
public void run(){
String message;
try {
while((message=reader.readLine()) != null){
incoming.append(message + "\n");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

}</blockquote>
 
2 Comments

Posted by on June 24, 2012 in Code, Java

 

Tags: ,