Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: UDP Port Scanner For Computer Security Module

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default UDP Port Scanner For Computer Security Module

    Greetings all.

    I need to implement a UDP port scanner for a university security module but I seem to be having issues. I have checked that everything has a value and that nothing is null. The address has the value it should and the port numbers are those that exist.

    I have included the error that I get as well as the code snippet that seems to be the problem.

    -- ERROR --
    Exception in thread "main" java.lang.NullPointerException: null address || null buffer
    at java.net.PlainDatagramSocketImpl.send(Native Method)
    at java.net.DatagramSocket.send(Unknown Source)
    at PortScanner.scanUDP(PortScanner.java:116)
    at PortScanner.scan(PortScanner.java:142)
    at PS.main(PS.java:30)

    -- CODE SNIPPET -- PortScanner.java
    ...
    	private ArrayList<String> udpServiceNames = null;
    	private ArrayList<Integer> udpPortNumbers = null;
    	private String IP = null;
    ...
    //Initialization of udpServiceNames  and udpPortNumbers from a file.
    //Setting of IP to "127.0.0.1"
    ...
    		File udpOut = new File("UDP_Ports_Status.txt");
    		FileOutputStream uout = new FileOutputStream(udpOut);
    		DatagramSocket sock = new DatagramSocket();
    		for(int i = 0; i < udpServiceNames.size(); i++)
    		{
    			System.out.print(".");
    			InetAddress addr = null;
    			try
    			{
    				addr = InetAddress.getByName(IP);
    			}
    			catch(UnknownHostException ex)
    			{
    				System.out.println("Passed original IP name conversion but failed now. Terminating....");
    				System.exit(1);
    			}
    			byte[] buff = new byte[256];
    			DatagramPacket pack = new DatagramPacket(buff, 256, addr, udpPortNumbers.get(i).intValue());
    			sock.setSoTimeout(500);
    			boolean received = false;
    			int sendCnt = 0;
    			while(!received && sendCnt < 3)
    			{
                                    //Error refers to the sending of the packet.
    				sock.send(pack);
    				pack = new DatagramPacket(buff, 256);
    				try
    				{
    					sock.receive(pack);
    					received = true;
    					break;
    				}
    				catch(Exception ex)
    				{
    				}
    				sendCnt++;
    			}
    			String str;
    			if(!received)
    				str = udpPortNumbers.get(i).intValue() + "::" + udpServiceNames.get(i) + "::CLOSED\r\n";
    			else
    				str = udpPortNumbers.get(i).intValue() + "::" + udpServiceNames.get(i) + "\r\n";
    			uout.write(str.getBytes());
    		}
    ...


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,069
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: UDP Port Scanner For Computer Security Module

    java.lang.NullPointerException: null address || null buffer
    at java.net.PlainDatagramSocketImpl.send(Native Method)
    at java.net.DatagramSocket.send(Unknown Source)
    at PortScanner.scanUDP(PortScanner.java:116)
    What is going on at statement 116? It looks like a call to the send method. Are any of its arguments null?

    Add a println to show what the values are each time around the loop.

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: UDP Port Scanner For Computer Security Module

    Quote Originally Posted by MetalPaw View Post
    I have checked that everything has a value and that nothing is null. The address has the value it should and the port numbers are those that exist.
    I have already done that with all arguments that make up the DatagramPacket...

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,069
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: UDP Port Scanner For Computer Security Module

    What is going on at statement 116?
    What are the values of the variables used in that statement?

  5. #5
    Junior Member
    Join Date
    Aug 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: UDP Port Scanner For Computer Security Module

    There is a comment in the code to say where the error is, ie where line 116 is. It is when I attempt to send a DatagramPacket through a DatagramSocket.

    sock.send(pack);

    Where sock = new DatagramSocket(); and pack = new DatagramPacket(buff, 256, addr, udpPortNumbers.get(i).intValue());
    buff is a byte array of length 256 which is created though the elemenets are not initialized. addr is InetAddress.getByName(IP); where IP is a string representing the IP address. udpPortNumbers is an ArrayList of Integers.

    I have check to make sure that they all have valid values and nothing is null, as the error suggests.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,069
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: UDP Port Scanner For Computer Security Module

    If none of the variables are ever null, then I don't understand where the error message comes from.
    I noticed you do NOT have a printStackTrace call in the catch block. You should ALWAYS put one there to print out any error that happens.

    Do you print out anything in the loop to show that you are looping?
    Last edited by Norm; August 24th, 2011 at 03:37 PM.

  7. #7
    Junior Member
    Join Date
    Aug 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: UDP Port Scanner For Computer Security Module

    *facepalm* It's freaking staring at me....

    Yes, I was outputing a fullstop to show looping but it never got further than the first one.

    Problem is here:

    			while(!received && sendCnt < 3)
    			{
    				sock.send(pack);
    				pack = new DatagramPacket(buff, 256);
    				...
    			}

    The pack that is supposed to send it different to the pack that is supposed to be recieved, but I'm simply overwriting it, so duh, the stuff is null. I only check for non-nulls BEFORE the while loop, but inside the while loop the stuff is obviously deleted....

    Bleg.

    Thanks for the help anyways. Once again, PBKC.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,069
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: UDP Port Scanner For Computer Security Module

    That was exactly what I was asking you to show by using print statements in the loop. It would have shown you that the second time around the loop the value of pack had changed.
    Glad you found it finally.

  9. #9
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: UDP Port Scanner For Computer Security Module

    Print the value of sendCnt inside the while loop and you'll be able to work out exactly what's going wrong.
    edit: *Splosh* missed the boat!

Similar Threads

  1. Relay data from remote port to local port
    By chegers in forum Java Networking
    Replies: 0
    Last Post: November 7th, 2010, 11:46 AM
  2. sign XML with WS-Security
    By splendes in forum Java SE APIs
    Replies: 1
    Last Post: October 6th, 2010, 08:49 AM
  3. Security in Server
    By madhu_sushmi in forum Java Servlet
    Replies: 6
    Last Post: May 17th, 2010, 02:07 PM
  4. How to write registration module
    By speedzojie in forum Java Theory & Questions
    Replies: 3
    Last Post: May 10th, 2010, 03:03 AM
  5. Jar File Security
    By Symbols in forum Java Theory & Questions
    Replies: 1
    Last Post: February 28th, 2010, 09:48 PM