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 6 of 6

Thread: Creating a ServerSocket - Trying to save logs from a Wireless Linksys Router

  1. #1
    Member vanDarg's Avatar
    Join Date
    Jan 2011
    Location
    Chicago
    Posts
    65
    My Mood
    Mellow
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default Creating a ServerSocket - Trying to save logs from a Wireless Linksys Router

    Hello friends, I have written a program which opens up a socket on port 514. This is to accept and save logs from my wireless WAP54G router. I would like to eventually be able to use a buffered reader so I can stream the input into log files locally, but I can't seem to establish a connection. The connection eventually times out, and the program exits. Here is what I have:

    import java.io.*;
    import java.net.*;
     
    public class KnockServer {
     
    	public static void main(String args[]) {
     
    		KnockServer server = new KnockServer();
    		server.mainLoop();
    	}
     
    	public void mainLoop() {
     
    		try {
    			ServerSocket server = new ServerSocket(514);
    			server.setSoTimeout(100000);
    			System.out.println("Success!");
    			while (true) {
    				Socket connection = server.accept();
    				BufferedInputStream in = new BufferedInputStream(connection.getInputStream());
    				InputStreamReader inputReader = new InputStreamReader(in);
    				StringBuffer process = new StringBuffer();
     
    				connection.close();
    				server.close();
    			}
    		}
    		catch (IOException e) {
    			System.out.println("Couldn't!");
    			System.err.println(e.getMessage());
     
    		}
    		catch (SecurityException e) {
    			System.err.println(e.getMessage());
    		}
    	}
    }

    Things to note: I am running Eclipse on Ubuntu. I am running the program with super user privileges. I have another program that somebody else has written in C, which opens up port 514 that does establish a connection, and I am able to see what the router is sending.

    If anybody notices anything wrong with what I have written and could please give me a direction I would appreciate it. Also, if anybody else has any suggestions of what else it could be related to, that would be helpful as well.
    Last edited by vanDarg; April 1st, 2011 at 05:50 PM.
    "Everything should be made as simple as possible, but not simpler."
    Asking Questions for Dummies | The Java Tutorials | Java Coding Styling Guide


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Creating a ServerSocket - Trying to save logs from a Wireless Linksys Router

    Any exceptions raised? The timeout value is set, so if nothing is accepted by the ServerSocket during the timeout period a SocketTimeoutException should be thrown.

  3. #3
    Member vanDarg's Avatar
    Join Date
    Jan 2011
    Location
    Chicago
    Posts
    65
    My Mood
    Mellow
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default Re: Creating a ServerSocket - Trying to save logs from a Wireless Linksys Router

    Sorry, I forgot to list the exception. After it times out, I do get a timed out exception:

    Success!
    Couldn't!
    Accept timed out
    "Everything should be made as simple as possible, but not simpler."
    Asking Questions for Dummies | The Java Tutorials | Java Coding Styling Guide

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Creating a ServerSocket - Trying to save logs from a Wireless Linksys Router

    There's your problem then - seems like nothing is coming from the other side. Are you sure that port is being used during the duration of the program execution?

  5. The Following User Says Thank You to copeg For This Useful Post:

    vanDarg (April 4th, 2011)

  6. #5
    Member vanDarg's Avatar
    Join Date
    Jan 2011
    Location
    Chicago
    Posts
    65
    My Mood
    Mellow
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default Re: Creating a ServerSocket - Trying to save logs from a Wireless Linksys Router

    Yeah it definitely is, I have a similar program written in C and it receives information and displays it in my terminal and log files. When I run this other program all I have to do is disconnect and reconnect to my network to get something sent from the router. When I do this in the program I am writing in Java nothing happens. This is why I am confused.
    Last edited by vanDarg; April 2nd, 2011 at 02:32 PM.
    "Everything should be made as simple as possible, but not simpler."
    Asking Questions for Dummies | The Java Tutorials | Java Coding Styling Guide

  7. #6
    Member vanDarg's Avatar
    Join Date
    Jan 2011
    Location
    Chicago
    Posts
    65
    My Mood
    Mellow
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default Re: Creating a ServerSocket - Trying to save logs from a Wireless Linksys Router

    Thanks for your help, I was able to come up with a solution. The router was sending UDP packets, not TCP packets. Further, it was timing out because UDP differs from TCP in that UDP does not create a direct connection between the two, rather it sends data out into the network whether or not there is a receiving computer. This link explains the difference very well if anyone is interested. I implemented the DatagramSocket class which is designed for UDP and my program works great. Thanks again for the input, and here is the solution:

    import java.io.*;
    import java.net.*;
     
    public class KnockServer {
     
    	public static void main(String args[]) throws IOException {
     
    		KnockServer server = new KnockServer();
    		server.mainLoop();
    	}
     
    	public void mainLoop() throws IOException {
     
    		DatagramSocket aSocket = null;
    		try {
    			aSocket = new DatagramSocket(514);
     
    			System.out.println("Success, opened port 514...");
     
    			byte[] buf = new byte[256];
     
    			while (true) {
    				DatagramPacket packet = new DatagramPacket(buf, buf.length);
    				aSocket.receive(packet);
    				System.out.println("Recieved a packet...");
    				String sentence = new String(packet.getData());
    				System.out.println(sentence);
    			}
    		}
    		catch (IOException e) {
    			System.err.println(e.getMessage());
     
    		}
    		catch (SecurityException e) {
    			System.err.println(e.getMessage());
    		}
    		finally {
    			aSocket.close();
    		}
     
    	}
    }
    "Everything should be made as simple as possible, but not simpler."
    Asking Questions for Dummies | The Java Tutorials | Java Coding Styling Guide

  8. The Following User Says Thank You to vanDarg For This Useful Post:

    copeg (April 3rd, 2011)

Similar Threads

  1. Creating a serverSocket while sonnecting to a socket on the same port
    By vortexnl in forum Java Theory & Questions
    Replies: 0
    Last Post: February 14th, 2011, 01:33 PM
  2. Tracking raw wireless data
    By gnome in forum Java Networking
    Replies: 1
    Last Post: January 1st, 2011, 04:44 PM
  3. [SOLVED] client-server-router problem
    By KrisTheSavage in forum Java Networking
    Replies: 2
    Last Post: August 23rd, 2010, 05:49 AM
  4. Unable to redirect all the eclipse console logs to buffer
    By skrishnapradeep in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 23rd, 2010, 11:03 PM
  5. access cisco router with java programme
    By vigneswara in forum Java Theory & Questions
    Replies: 1
    Last Post: May 11th, 2010, 01:36 AM