Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Sunday, July 10, 2011

simple Port Scanner

Back in my sixth semester Computer Networks lab, we had to write a simple port scanner (port 0 to 1024). The basic principle was to see whether a connection could be made at a port. If connection can be made, it is open, or else it is closed. I had used the following code then. However, it took 17 minutes to perform the scan.

import java.io.IOException; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; public class port_scanner { static Socket soc; public static void main(String[] args) throws IOException { for(int i=1;i<1024;i++){ try{ soc= new Socket(InetAddress.getLocalHost(),i); System.out.println("port "+i+" open"); soc.close(); }catch(IOException e){ System.out.println("port "+i+" closed"); } } } }

The better option was to use threading. I was lazy back then and the above program was sufficient to fetch me marks, and so I did not write a multi-threaded code. I needed to brush up threading because placements are going to start soon. Therefore, I implemented the multi-threaded version. It took just 2 seconds to perform the scan.

import java.io.IOException; import java.net.InetAddress; import java.net.Socket; class myThread implements Runnable{ Thread t; Socket soc; boolean state=true; int port; myThread(int port){ this.port=port; t=new Thread(this,"Thread"); t.start(); } boolean getState(){ return state; } @Override public void run(){ try{ soc= new Socket(InetAddress.getLocalHost(),port); soc.close(); }catch(IOException e){ state=false; } } } public class new_prt { static myThread threads[] = new myThread[1024]; public static void main(String args[]){ for(int j=0;j<1024;j++){ threads[j]=new myThread(i); } for(int j=0;j<1024;j++){ try{ threads[j].t.join(); if(threads[j].getState()==true) System.out.println("port "+threads[j].port + " open"); else System.out.println("port "+threads[j].port + " closed"); }catch(InterruptedException e){ } } } }

Saturday, July 2, 2011

XMPP or (How to programatically do stuffs in IM)

Hi.
It's been a while since I worked with the Smack API.... 6 months and so I might have forgotten many things. Anyways, it is a brief introduction to XMPP and more specifically to Smack API. It provides Java libraries for playing around with XMPP.



The above snippet will connect to the gmail server and make an status update.

Presence presence =new Presence(Presence.Type.available,
status,
24,
Presence.Mode.available);
Here Presence.Type is a enum to represent the presecence type. Note that presence type is 
often confused with presence mode. Generally, if a user is signed into a server, they have a 
presence type of available, even if the mode is away, dnd, etc. The presence type is only 
unavailable when the user is signing out of the server
status contains the string which you want to set as your status.
24 is the priority. The highest priory client will receive the packets.
Check this out for better documentation of Presence.


Add
Roster roster = connection.getRoster();
System.out.println("No of contacts: " + roster.getEntryCount());
for (RosterEntry entry : roster.getEntries())     
System.out.println("User: " + entry.getUser());
The above code will show the size of your roster and also show the user list

Message msg=new Message(user@gmail.com",Message.Type.chat);
msg.setBody("ssup?");
connection.sendPacket(msg);
Use this to send IM to some user.

presence.setStatus(status);
connection.sendPacket(presence);
Use this to change status.
There are many fun things that you can do with this. You can make your status rotate, write the lyrics of your favourite song line by line, flood, or change your status from busy to available and vice-versa continuously to get a alternating red-green thing.
Do your thing and please share it. Will love to learn from you.

Friday, June 24, 2011

Flashing keyboard LEDs










Here is a simple script to make a keyboard's LED blink
Copy the script below and save it as .wsf
Rem blinking LED :vstsv
<package>
   <job id="vbs">
      <script language="VBScript">
         set WshShell = WScript.CreateObject("WScript.Shell")
           For count=0 to 10
             WshShell.SendKeys "{CAPSLOCK}"
             WScript.Sleep 500
             WshShell.SendKeys "{NUMLOCK}"
             WScript.Sleep 500
             WshShell.SendKeys "{SCROLLLOCK}"
             WScript.Sleep 500
           Next
      </script>
   </job>
</package>
The other alternative for Windows user is to use SendKeys Python module. You can find it here. The page also contains good tutorial and documentation.


import SendKeys
SendKeys.SendKeys("""
    {CAPSLOCK}
    {PAUSE 1}
    {NUMLOCK}
    {PAUSE 1}
    {SCROLLLOCK}
    {PAUSE 1}
""")


JAVA can be used to get the same results.


import java.awt.Toolkit;
import java.awt.event.KeyEvent;


public class SendKeys {


 public boolean numlock(boolean s) {
  Toolkit tool = Toolkit.getDefaultToolkit();
  try {
   tool.setLockingKeyState(KeyEvent.VK_NUM_LOCK, s);
  } catch (Exception e) {
   return false;
  }
  return true;
 }


 public boolean capslock(boolean s) {
  Toolkit tool = Toolkit.getDefaultToolkit();
  try {
   tool.setLockingKeyState(KeyEvent.VK_CAPS_LOCK, s);
  } catch (Exception e) {
   return false;
  }
  return true;
 }


 public boolean scrolllock(boolean s) {
  Toolkit tool = Toolkit.getDefaultToolkit();
  try {
   tool.setLockingKeyState(KeyEvent.VK_SCROLL_LOCK, s);
  } catch (Exception e) {
   return false;
  }
  return true;
 }


 public static void main(String[] args) throws Exception {
  SendKeys flasher = new SendKeys();


  for(int i=0;i<10;i++){
                   flasher.numlock(true);
                  Thread.sleep(500);
                  flasher.numlock(true);
                  Thread.sleep(500);
                  flasher.capslock(true);
                  Thread.sleep(500);
                  flasher.capslock(true);
                  Thread.sleep(500);
                  flasher.scrolllock(true);
                  Thread.sleep(500);
                  flasher.scrolllock(true);
                  Thread.sleep(500);
            }
 }
}