VB icon

IP Grabber

Email
Submitted on: 1/2/2015 5:00:00 PM
By: Craig Casey (from psc cd)  
Level: Beginner
User Rating: By 5 Users
Compatibility: Java (JDK 1.2)
Views: 2627
 
     Accepts connections and takes the IP of the request
 

HTML:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
//HTML for :IP Grabber
//**************************************
This is a simple server app that gets the IPs or incoming requests...
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: IP Grabber
// Description:Accepts connections and takes the IP of the request
// By: Craig Casey (from psc cd)
//
// Inputs:You have to connect to it any way you want... whether it's a web browser or a self-written app that uses the Socket class
//**************************************

// Sorry about the enormous pile of copyright 
// stuff above. I didn't put it there... please
// ignore it.
// 
// By: Craig Casey -> craig_c_11@hotmail.com
// This code, unlike Ian's non-legally-bound
// one-line entries that somehow manage to win
// contests, is PUBLIC DOMAIN.
// This means that ANYONE can use it without
// being harassed by me or anyone else.
// And NO, you do not need to have this comment
// block... in fact, you're encouraged to remove
// it from your code.
 
 
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class StaticGrabberData {
	static JTextArea IPListArea = new JTextArea(0, 0);
}
class DaemonGrabber extends Thread {
	DaemonGrabber () {
		setDaemon(true);
		start();
	}
	public void run ()
	{
		try {
			while (true) {
				ServerSocket s = new ServerSocket(8080);
				try {
					new GrabberWorker(s.accept());
				} catch (Throwable t) {
					JOptionPane.showMessageDialog(null, "The server fuX0R3d up!");
				} finally {
					try {
						s.close();
					} catch (IOException e) {
						JOptionPane.showMessageDialog(null, "Could n0t c1053 th3 s3rv3r s0ck3t!");
					}
				}
			}
		} catch (IOException e) {
			JOptionPane.showMessageDialog(null, "This wh0l3 th4ng is fuX0R3d up!");
		}
	}
}
class GrabberWorker extends Thread {
	Socket s;
	GrabberWorker ()
	{
		// This is only here for... um... no reason.
	}
	GrabberWorker (Socket acceptedConnection)
	{
		setDaemon(false);
		s = acceptedConnection;
		start();
	}
	public void run ()
	{
		try {
			StaticGrabberData.IPListArea.append((s.getInetAddress()).getHostAddress()+"\n");
		} catch (Throwable t) {
			JOptionPane.showMessageDialog(null, "There was a problem r34ding the s0cke1.");
		} finally {
			try {
				s.close();
			} catch (IOException e) {
				JOptionPane.showMessageDialog(null, "Could n0t c1053 4 s0ck3t!");
			}
		}
	}
}
public class IPGrabber {
	IPGrabber () 
	{
		new DaemonGrabber();
		final JFrame jFr = new JFrame("IP Grabber");
		final JFileChooser jFC = new JFileChooser();
		jFr.getContentPane().setLayout(new GridLayout(1, 1));
		JPanel jPane = new JPanel();
		jPane.setLayout(new GridLayout(0, 1));
		JButton jbClear = new JButton("Clear");
		JButton jbSave = new JButton("Save");
		JButton jbOpen = new JButton("Open");
		
		jPane.add(jbClear);
		jPane.add(jbSave);
		jPane.add(jbOpen);
		JSplitPane jpSplit = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, new JScrollPane(StaticGrabberData.IPListArea), jPane);
		jpSplit.setDividerLocation(300);
		jFr.getContentPane().add(jpSplit);
		jFr.pack();
		
		jFr.setSize(400, 100);
		
		jbClear.addActionListener(
			new ActionListener () {
				public void actionPerformed(ActionEvent e)
				{
					StaticGrabberData.IPListArea.replaceRange("", 0, StaticGrabberData.IPListArea.getText().length());
				}
			}
		);
		
		jbSave.addActionListener(
			new ActionListener () {
				public void actionPerformed(ActionEvent e)
				{
					try {
						int isApproved = jFC.showSaveDialog(jFr);
						if (isApproved == JFileChooser.APPROVE_OPTION) {
							File f = jFC.getSelectedFile();
							PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(f)), true);
							pw.print(StaticGrabberData.IPListArea.getText());
							pw.close();
						}
					} catch (Throwable t) {
						JOptionPane.showMessageDialog(null, "C0u1d n0t s4v3 fi13!");
					}
				}
			}
		);
		
		jbOpen.addActionListener(
			new ActionListener () {
				public void actionPerformed(ActionEvent e)
				{
					try {
						int isApproved = jFC.showOpenDialog(jFr);
						if (isApproved == JFileChooser.APPROVE_OPTION) {
							File f = jFC.getSelectedFile();
							BufferedReader br = new BufferedReader(new FileReader(f));
							StaticGrabberData.IPListArea.replaceRange("", 0, StaticGrabberData.IPListArea.getText().length());
							int i;
							String fConts = new String("");
							while ((i = br.read()) != -1) {
								fConts += (char)i;
							}
							br.close();
							StaticGrabberData.IPListArea.append(fConts);
						}
					} catch (Throwable t) {
						JOptionPane.showMessageDialog(null, "C0u1d n0t s4v3 fi13!");
					}
				}
			}
		);
			
		jFr.addWindowListener(
			new WindowAdapter () {
				public void windowClosing(WindowEvent e)
				{
					System.exit(0);
				}
			}
		);
		
		jFr.setVisible(true);
	}
	
	public static void main (String[] argv)
	{
		new IPGrabber();
	}
}


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.