You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by rw...@apache.org on 2009/04/04 21:46:48 UTC
svn commit: r761995 - in
/commons/proper/net/branches/NET_2_0/src/main/java/examples: ./ cidr/ mail/
telnet/ unix/
Author: rwinston
Date: Sat Apr 4 19:46:47 2009
New Revision: 761995
URL: http://svn.apache.org/viewvc?rev=761995&view=rev
Log:
Refactor examples package
Added:
commons/proper/net/branches/NET_2_0/src/main/java/examples/cidr/
commons/proper/net/branches/NET_2_0/src/main/java/examples/cidr/SubnetUtilsExample.java (with props)
commons/proper/net/branches/NET_2_0/src/main/java/examples/mail/
commons/proper/net/branches/NET_2_0/src/main/java/examples/mail/POP3Mail.java (with props)
commons/proper/net/branches/NET_2_0/src/main/java/examples/mail/SMTPMail.java (with props)
commons/proper/net/branches/NET_2_0/src/main/java/examples/telnet/
commons/proper/net/branches/NET_2_0/src/main/java/examples/telnet/TelnetClientExample.java (with props)
commons/proper/net/branches/NET_2_0/src/main/java/examples/telnet/WeatherTelnet.java (with props)
commons/proper/net/branches/NET_2_0/src/main/java/examples/unix/
commons/proper/net/branches/NET_2_0/src/main/java/examples/unix/chargen.java (with props)
commons/proper/net/branches/NET_2_0/src/main/java/examples/unix/daytime.java (with props)
commons/proper/net/branches/NET_2_0/src/main/java/examples/unix/echo.java (with props)
commons/proper/net/branches/NET_2_0/src/main/java/examples/unix/finger.java (with props)
commons/proper/net/branches/NET_2_0/src/main/java/examples/unix/fwhois.java (with props)
commons/proper/net/branches/NET_2_0/src/main/java/examples/unix/rdate.java (with props)
commons/proper/net/branches/NET_2_0/src/main/java/examples/unix/rexec.java (with props)
commons/proper/net/branches/NET_2_0/src/main/java/examples/unix/rlogin.java (with props)
commons/proper/net/branches/NET_2_0/src/main/java/examples/unix/rshell.java (with props)
Removed:
commons/proper/net/branches/NET_2_0/src/main/java/examples/SubnetUtilsExample.java
commons/proper/net/branches/NET_2_0/src/main/java/examples/TelnetClientExample.java
commons/proper/net/branches/NET_2_0/src/main/java/examples/chargen.java
commons/proper/net/branches/NET_2_0/src/main/java/examples/daytime.java
commons/proper/net/branches/NET_2_0/src/main/java/examples/echo.java
commons/proper/net/branches/NET_2_0/src/main/java/examples/finger.java
commons/proper/net/branches/NET_2_0/src/main/java/examples/fwhois.java
commons/proper/net/branches/NET_2_0/src/main/java/examples/mail.java
commons/proper/net/branches/NET_2_0/src/main/java/examples/messages.java
commons/proper/net/branches/NET_2_0/src/main/java/examples/rdate.java
commons/proper/net/branches/NET_2_0/src/main/java/examples/rexec.java
commons/proper/net/branches/NET_2_0/src/main/java/examples/rlogin.java
commons/proper/net/branches/NET_2_0/src/main/java/examples/rshell.java
commons/proper/net/branches/NET_2_0/src/main/java/examples/weatherTelnet.java
Added: commons/proper/net/branches/NET_2_0/src/main/java/examples/cidr/SubnetUtilsExample.java
URL: http://svn.apache.org/viewvc/commons/proper/net/branches/NET_2_0/src/main/java/examples/cidr/SubnetUtilsExample.java?rev=761995&view=auto
==============================================================================
--- commons/proper/net/branches/NET_2_0/src/main/java/examples/cidr/SubnetUtilsExample.java (added)
+++ commons/proper/net/branches/NET_2_0/src/main/java/examples/cidr/SubnetUtilsExample.java Sat Apr 4 19:46:47 2009
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package examples.cidr;
+
+import java.util.Arrays;
+import java.util.Scanner;
+
+import org.apache.commons.net.util.SubnetUtils;
+import org.apache.commons.net.util.SubnetUtils.SubnetInfo;
+
+/**
+ * Example class that shows how to use the {@link SubnetUtils} class.
+ * @author Rory Winston <rw...@apache.org>
+ *
+ */
+public class SubnetUtilsExample {
+
+ public static void main(String[] args) {
+ String subnet = "192.168.0.3/31";
+ SubnetUtils utils = new SubnetUtils(subnet);
+ SubnetInfo info = utils.getInfo();
+
+ System.out.printf("Subnet Information for %s:\n", subnet);
+ System.out.println("--------------------------------------");
+ System.out.printf("IP Address:\t\t\t%s\t[%s]\n", info.getAddress(),
+ Integer.toBinaryString(info.asInteger(info.getAddress())));
+ System.out.printf("Netmask:\t\t\t%s\t[%s]\n", info.getNetmask(),
+ Integer.toBinaryString(info.asInteger(info.getNetmask())));
+ System.out.printf("CIDR Representation:\t\t%s\n\n", info.getCidrSignature());
+
+ System.out.printf("Supplied IP Address:\t\t%s\n\n", info.getAddress());
+
+ System.out.printf("Network Address:\t\t%s\t[%s]\n", info.getNetworkAddress(),
+ Integer.toBinaryString(info.asInteger(info.getNetworkAddress())));
+ System.out.printf("Broadcast Address:\t\t%s\t[%s]\n", info.getBroadcastAddress(),
+ Integer.toBinaryString(info.asInteger(info.getBroadcastAddress())));
+ System.out.printf("Low Address:\t\t\t%s\t[%s]\n", info.getLowAddress(),
+ Integer.toBinaryString(info.asInteger(info.getLowAddress())));
+ System.out.printf("High Address:\t\t\t%s\t[%s]\n", info.getHighAddress(),
+ Integer.toBinaryString(info.asInteger(info.getHighAddress())));
+
+ System.out.printf("Total usable addresses: \t%d\n", info.getAddressCount());
+ System.out.printf("Address List: %s\n\n", Arrays.toString(info.getAllAddresses()));
+
+ final String prompt ="Enter an IP address (e.g. 192.168.0.10):";
+ System.out.println(prompt);
+ Scanner scanner = new Scanner(System.in);
+ while (scanner.hasNextLine()) {
+ String address = scanner.nextLine();
+ System.out.println("The IP address [" + address + "] is "
+ + (info.isInRange(address) ? "" : "not ")
+ + "within the subnet [" + subnet + "]");
+ System.out.println(prompt);
+ }
+
+ }
+
+}
Propchange: commons/proper/net/branches/NET_2_0/src/main/java/examples/cidr/SubnetUtilsExample.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/proper/net/branches/NET_2_0/src/main/java/examples/mail/POP3Mail.java
URL: http://svn.apache.org/viewvc/commons/proper/net/branches/NET_2_0/src/main/java/examples/mail/POP3Mail.java?rev=761995&view=auto
==============================================================================
--- commons/proper/net/branches/NET_2_0/src/main/java/examples/mail/POP3Mail.java (added)
+++ commons/proper/net/branches/NET_2_0/src/main/java/examples/mail/POP3Mail.java Sat Apr 4 19:46:47 2009
@@ -0,0 +1,142 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package examples.mail;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.Reader;
+import org.apache.commons.net.pop3.POP3Client;
+import org.apache.commons.net.pop3.POP3MessageInfo;
+
+/***
+ * This is an example program demonstrating how to use the POP3Client class.
+ * This program connects to a POP3 server and retrieves the message
+ * headers of all the messages, printing the From: and Subject: header
+ * entries for each message.
+ * <p>
+ * Usage: messages <pop3 server hostname> <username> <password>
+ * <p>
+ ***/
+public final class POP3Mail
+{
+
+ public static final void printMessageInfo(BufferedReader reader, int id)
+ throws IOException
+ {
+ String line, lower, from, subject;
+
+ from = "";
+ subject = "";
+
+ while ((line = reader.readLine()) != null)
+ {
+ lower = line.toLowerCase();
+ if (lower.startsWith("from: "))
+ from = line.substring(6).trim();
+ else if (lower.startsWith("subject: "))
+ subject = line.substring(9).trim();
+ }
+
+ System.out.println(Integer.toString(id) + " From: " + from +
+ " Subject: " + subject);
+ }
+
+ public static final void main(String[] args)
+ {
+ int message;
+ String server, username, password;
+ POP3Client pop3;
+ Reader reader;
+ POP3MessageInfo[] messages;
+
+ if (args.length < 3)
+ {
+ System.err.println(
+ "Usage: messages <pop3 server hostname> <username> <password>");
+ System.exit(1);
+ }
+
+ server = args[0];
+ username = args[1];
+ password = args[2];
+
+ pop3 = new POP3Client();
+ // We want to timeout if a response takes longer than 60 seconds
+ pop3.setDefaultTimeout(60000);
+
+ try
+ {
+ pop3.connect(server);
+ }
+ catch (IOException e)
+ {
+ System.err.println("Could not connect to server.");
+ e.printStackTrace();
+ System.exit(1);
+ }
+
+ try
+ {
+ if (!pop3.login(username, password))
+ {
+ System.err.println("Could not login to server. Check password.");
+ pop3.disconnect();
+ System.exit(1);
+ }
+
+ messages = pop3.listMessages();
+
+ if (messages == null)
+ {
+ System.err.println("Could not retrieve message list.");
+ pop3.disconnect();
+ System.exit(1);
+ }
+ else if (messages.length == 0)
+ {
+ System.out.println("No messages");
+ pop3.logout();
+ pop3.disconnect();
+ System.exit(1);
+ }
+
+ for (message = 0; message < messages.length; message++)
+ {
+ reader = pop3.retrieveMessageTop(messages[message].number, 0);
+
+ if (reader == null)
+ {
+ System.err.println("Could not retrieve message header.");
+ pop3.disconnect();
+ System.exit(1);
+ }
+
+ printMessageInfo(new BufferedReader(reader), messages[message].number);
+ }
+
+ pop3.logout();
+ pop3.disconnect();
+ }
+ catch (IOException e)
+ {
+ e.printStackTrace();
+ System.exit(1);
+ }
+ }
+}
+
Propchange: commons/proper/net/branches/NET_2_0/src/main/java/examples/mail/POP3Mail.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/proper/net/branches/NET_2_0/src/main/java/examples/mail/SMTPMail.java
URL: http://svn.apache.org/viewvc/commons/proper/net/branches/NET_2_0/src/main/java/examples/mail/SMTPMail.java?rev=761995&view=auto
==============================================================================
--- commons/proper/net/branches/NET_2_0/src/main/java/examples/mail/SMTPMail.java (added)
+++ commons/proper/net/branches/NET_2_0/src/main/java/examples/mail/SMTPMail.java Sat Apr 4 19:46:47 2009
@@ -0,0 +1,164 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package examples.mail;
+
+import java.io.BufferedReader;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.PrintWriter;
+import java.io.Writer;
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.List;
+
+import org.apache.commons.net.PrintCommandListener;
+import org.apache.commons.net.io.Util;
+import org.apache.commons.net.smtp.SMTPClient;
+import org.apache.commons.net.smtp.SMTPReply;
+import org.apache.commons.net.smtp.SimpleSMTPHeader;
+
+/***
+ * This is an example program using the SMTP package to send a message
+ * to the specified recipients. It prompts you for header information and
+ * a filename containing the message.
+ * <p>
+ ***/
+
+public final class SMTPMail
+{
+
+ public final static void main(String[] args)
+ {
+ String sender, recipient, subject, filename, server, cc;
+ List<String> ccList = new ArrayList<String>();
+ BufferedReader stdin;
+ FileReader fileReader = null;
+ Writer writer;
+ SimpleSMTPHeader header;
+ SMTPClient client;
+ Enumeration en;
+
+ if (args.length < 1)
+ {
+ System.err.println("Usage: mail smtpserver");
+ System.exit(1);
+ }
+
+ server = args[0];
+
+ stdin = new BufferedReader(new InputStreamReader(System.in));
+
+ try
+ {
+ System.out.print("From: ");
+ System.out.flush();
+
+ sender = stdin.readLine();
+
+ System.out.print("To: ");
+ System.out.flush();
+
+ recipient = stdin.readLine();
+
+ System.out.print("Subject: ");
+ System.out.flush();
+
+ subject = stdin.readLine();
+
+ header = new SimpleSMTPHeader(sender, recipient, subject);
+
+
+ while (true)
+ {
+ System.out.print("CC <enter one address per line, hit enter to end>: ");
+ System.out.flush();
+
+ // Of course you don't want to do this because readLine() may be null
+ cc = stdin.readLine().trim();
+
+ if (cc.length() == 0)
+ break;
+
+ header.addCC(cc);
+ ccList.add(cc);
+ }
+
+ System.out.print("Filename: ");
+ System.out.flush();
+
+ filename = stdin.readLine();
+
+ try
+ {
+ fileReader = new FileReader(filename);
+ }
+ catch (FileNotFoundException e)
+ {
+ System.err.println("File not found. " + e.getMessage());
+ }
+
+ client = new SMTPClient();
+ client.addProtocolCommandListener(new PrintCommandListener(
+ new PrintWriter(System.out)));
+
+ client.connect(server);
+
+ if (!SMTPReply.isPositiveCompletion(client.getReplyCode()))
+ {
+ client.disconnect();
+ System.err.println("SMTP server refused connection.");
+ System.exit(1);
+ }
+
+ client.login();
+
+ client.setSender(sender);
+ client.addRecipient(recipient);
+
+
+
+ for (String recpt : ccList)
+ client.addRecipient(recpt);
+
+ writer = client.sendMessageData();
+
+ if (writer != null)
+ {
+ writer.write(header.toString());
+ Util.copyReader(fileReader, writer);
+ writer.close();
+ client.completePendingCommand();
+ }
+
+ fileReader.close();
+
+ client.logout();
+
+ client.disconnect();
+ }
+ catch (IOException e)
+ {
+ e.printStackTrace();
+ System.exit(1);
+ }
+ }
+}
+
+
Propchange: commons/proper/net/branches/NET_2_0/src/main/java/examples/mail/SMTPMail.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/proper/net/branches/NET_2_0/src/main/java/examples/telnet/TelnetClientExample.java
URL: http://svn.apache.org/viewvc/commons/proper/net/branches/NET_2_0/src/main/java/examples/telnet/TelnetClientExample.java?rev=761995&view=auto
==============================================================================
--- commons/proper/net/branches/NET_2_0/src/main/java/examples/telnet/TelnetClientExample.java (added)
+++ commons/proper/net/branches/NET_2_0/src/main/java/examples/telnet/TelnetClientExample.java Sat Apr 4 19:46:47 2009
@@ -0,0 +1,336 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package examples.telnet;
+
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import org.apache.commons.net.telnet.TelnetClient;
+import org.apache.commons.net.telnet.TelnetNotificationHandler;
+import org.apache.commons.net.telnet.SimpleOptionHandler;
+import org.apache.commons.net.telnet.EchoOptionHandler;
+import org.apache.commons.net.telnet.TerminalTypeOptionHandler;
+import org.apache.commons.net.telnet.SuppressGAOptionHandler;
+import org.apache.commons.net.telnet.InvalidTelnetOptionException;
+import java.util.StringTokenizer;
+
+
+/***
+ * This is a simple example of use of TelnetClient.
+ * An external option handler (SimpleTelnetOptionHandler) is used.
+ * Initial configuration requested by TelnetClient will be:
+ * WILL ECHO, WILL SUPPRESS-GA, DO SUPPRESS-GA.
+ * VT100 terminal type will be subnegotiated.
+ * <p>
+ * Also, use of the sendAYT(), getLocalOptionState(), getRemoteOptionState()
+ * is demonstrated.
+ * When connected, type AYT to send an AYT command to the server and see
+ * the result.
+ * Type OPT to see a report of the state of the first 25 options.
+ * <p>
+ * @author Bruno D'Avanzo
+ ***/
+public class TelnetClientExample implements Runnable, TelnetNotificationHandler
+{
+ static TelnetClient tc = null;
+
+ /***
+ * Main for the TelnetClientExample.
+ ***/
+ public static void main(String[] args) throws IOException
+ {
+ FileOutputStream fout = null;
+
+ if(args.length < 1)
+ {
+ System.err.println("Usage: TelnetClientExample1 <remote-ip> [<remote-port>]");
+ System.exit(1);
+ }
+
+ String remoteip = args[0];
+
+ int remoteport;
+
+ if (args.length > 1)
+ {
+ remoteport = (new Integer(args[1])).intValue();
+ }
+ else
+ {
+ remoteport = 23;
+ }
+
+ try
+ {
+ fout = new FileOutputStream ("spy.log", true);
+ }
+ catch (Exception e)
+ {
+ System.err.println(
+ "Exception while opening the spy file: "
+ + e.getMessage());
+ }
+
+ tc = new TelnetClient();
+
+ TerminalTypeOptionHandler ttopt = new TerminalTypeOptionHandler("VT100", false, false, true, false);
+ EchoOptionHandler echoopt = new EchoOptionHandler(true, false, true, false);
+ SuppressGAOptionHandler gaopt = new SuppressGAOptionHandler(true, true, true, true);
+
+ try
+ {
+ tc.addOptionHandler(ttopt);
+ tc.addOptionHandler(echoopt);
+ tc.addOptionHandler(gaopt);
+ }
+ catch (InvalidTelnetOptionException e)
+ {
+ System.err.println("Error registering option handlers: " + e.getMessage());
+ }
+
+ while (true)
+ {
+ boolean end_loop = false;
+ try
+ {
+ tc.connect(remoteip, remoteport);
+
+
+ Thread reader = new Thread (new TelnetClientExample());
+ tc.registerNotifHandler(new TelnetClientExample());
+ System.out.println("TelnetClientExample");
+ System.out.println("Type AYT to send an AYT telnet command");
+ System.out.println("Type OPT to print a report of status of options (0-24)");
+ System.out.println("Type REGISTER to register a new SimpleOptionHandler");
+ System.out.println("Type UNREGISTER to unregister an OptionHandler");
+ System.out.println("Type SPY to register the spy (connect to port 3333 to spy)");
+ System.out.println("Type UNSPY to stop spying the connection");
+
+ reader.start();
+ OutputStream outstr = tc.getOutputStream();
+
+ byte[] buff = new byte[1024];
+ int ret_read = 0;
+
+ do
+ {
+ try
+ {
+ ret_read = System.in.read(buff);
+ if(ret_read > 0)
+ {
+ if((new String(buff, 0, ret_read)).startsWith("AYT"))
+ {
+ try
+ {
+ System.out.println("Sending AYT");
+
+ System.out.println("AYT response:" + tc.sendAYT(5000));
+ }
+ catch (Exception e)
+ {
+ System.err.println("Exception waiting AYT response: " + e.getMessage());
+ }
+ }
+ else if((new String(buff, 0, ret_read)).startsWith("OPT"))
+ {
+ System.out.println("Status of options:");
+ for(int ii=0; ii<25; ii++)
+ System.out.println("Local Option " + ii + ":" + tc.getLocalOptionState(ii) + " Remote Option " + ii + ":" + tc.getRemoteOptionState(ii));
+ }
+ else if((new String(buff, 0, ret_read)).startsWith("REGISTER"))
+ {
+ StringTokenizer st = new StringTokenizer(new String(buff));
+ try
+ {
+ st.nextToken();
+ int opcode = (new Integer(st.nextToken())).intValue();
+ boolean initlocal = (new Boolean(st.nextToken())).booleanValue();
+ boolean initremote = (new Boolean(st.nextToken())).booleanValue();
+ boolean acceptlocal = (new Boolean(st.nextToken())).booleanValue();
+ boolean acceptremote = (new Boolean(st.nextToken())).booleanValue();
+ SimpleOptionHandler opthand = new SimpleOptionHandler(opcode, initlocal, initremote,
+ acceptlocal, acceptremote);
+ tc.addOptionHandler(opthand);
+ }
+ catch (Exception e)
+ {
+ if(e instanceof InvalidTelnetOptionException)
+ {
+ System.err.println("Error registering option: " + e.getMessage());
+ }
+ else
+ {
+ System.err.println("Invalid REGISTER command.");
+ System.err.println("Use REGISTER optcode initlocal initremote acceptlocal acceptremote");
+ System.err.println("(optcode is an integer.)");
+ System.err.println("(initlocal, initremote, acceptlocal, acceptremote are boolean)");
+ }
+ }
+ }
+ else if((new String(buff, 0, ret_read)).startsWith("UNREGISTER"))
+ {
+ StringTokenizer st = new StringTokenizer(new String(buff));
+ try
+ {
+ st.nextToken();
+ int opcode = (new Integer(st.nextToken())).intValue();
+ tc.deleteOptionHandler(opcode);
+ }
+ catch (Exception e)
+ {
+ if(e instanceof InvalidTelnetOptionException)
+ {
+ System.err.println("Error unregistering option: " + e.getMessage());
+ }
+ else
+ {
+ System.err.println("Invalid UNREGISTER command.");
+ System.err.println("Use UNREGISTER optcode");
+ System.err.println("(optcode is an integer)");
+ }
+ }
+ }
+ else if((new String(buff, 0, ret_read)).startsWith("SPY"))
+ {
+ try
+ {
+ tc.registerSpyStream(fout);
+ }
+ catch (Exception e)
+ {
+ System.err.println("Error registering the spy");
+ }
+ }
+ else if((new String(buff, 0, ret_read)).startsWith("UNSPY"))
+ {
+ tc.stopSpyStream();
+ }
+ else
+ {
+ try
+ {
+ outstr.write(buff, 0 , ret_read);
+ outstr.flush();
+ }
+ catch (Exception e)
+ {
+ end_loop = true;
+ }
+ }
+ }
+ }
+ catch (Exception e)
+ {
+ System.err.println("Exception while reading keyboard:" + e.getMessage());
+ end_loop = true;
+ }
+ }
+ while((ret_read > 0) && (end_loop == false));
+
+ try
+ {
+ tc.disconnect();
+ }
+ catch (Exception e)
+ {
+ System.err.println("Exception while connecting:" + e.getMessage());
+ }
+ }
+ catch (Exception e)
+ {
+ System.err.println("Exception while connecting:" + e.getMessage());
+ System.exit(1);
+ }
+ }
+ }
+
+
+ /***
+ * Callback method called when TelnetClient receives an option
+ * negotiation command.
+ * <p>
+ * @param negotiation_code - type of negotiation command received
+ * (RECEIVED_DO, RECEIVED_DONT, RECEIVED_WILL, RECEIVED_WONT)
+ * <p>
+ * @param option_code - code of the option negotiated
+ * <p>
+ ***/
+ public void receivedNegotiation(int negotiation_code, int option_code)
+ {
+ String command = null;
+ if(negotiation_code == TelnetNotificationHandler.RECEIVED_DO)
+ {
+ command = "DO";
+ }
+ else if(negotiation_code == TelnetNotificationHandler.RECEIVED_DONT)
+ {
+ command = "DONT";
+ }
+ else if(negotiation_code == TelnetNotificationHandler.RECEIVED_WILL)
+ {
+ command = "WILL";
+ }
+ else if(negotiation_code == TelnetNotificationHandler.RECEIVED_WONT)
+ {
+ command = "WONT";
+ }
+ System.out.println("Received " + command + " for option code " + option_code);
+ }
+
+ /***
+ * Reader thread.
+ * Reads lines from the TelnetClient and echoes them
+ * on the screen.
+ ***/
+ public void run()
+ {
+ InputStream instr = tc.getInputStream();
+
+ try
+ {
+ byte[] buff = new byte[1024];
+ int ret_read = 0;
+
+ do
+ {
+ ret_read = instr.read(buff);
+ if(ret_read > 0)
+ {
+ System.out.print(new String(buff, 0, ret_read));
+ }
+ }
+ while (ret_read >= 0);
+ }
+ catch (Exception e)
+ {
+ System.err.println("Exception while reading socket:" + e.getMessage());
+ }
+
+ try
+ {
+ tc.disconnect();
+ }
+ catch (Exception e)
+ {
+ System.err.println("Exception while closing telnet:" + e.getMessage());
+ }
+ }
+}
+
Propchange: commons/proper/net/branches/NET_2_0/src/main/java/examples/telnet/TelnetClientExample.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/proper/net/branches/NET_2_0/src/main/java/examples/telnet/WeatherTelnet.java
URL: http://svn.apache.org/viewvc/commons/proper/net/branches/NET_2_0/src/main/java/examples/telnet/WeatherTelnet.java?rev=761995&view=auto
==============================================================================
--- commons/proper/net/branches/NET_2_0/src/main/java/examples/telnet/WeatherTelnet.java (added)
+++ commons/proper/net/branches/NET_2_0/src/main/java/examples/telnet/WeatherTelnet.java Sat Apr 4 19:46:47 2009
@@ -0,0 +1,76 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package examples.telnet;
+
+import java.io.IOException;
+import org.apache.commons.net.telnet.TelnetClient;
+
+import examples.util.IOUtil;
+
+/***
+ * This is an example of a trivial use of the TelnetClient class.
+ * It connects to the weather server at the University of Michigan,
+ * um-weather.sprl.umich.edu port 3000, and allows the user to interact
+ * with the server via standard input. You could use this example to
+ * connect to any telnet server, but it is obviously not general purpose
+ * because it reads from standard input a line at a time, making it
+ * inconvenient for use with a remote interactive shell. The TelnetClient
+ * class used by itself is mostly intended for automating access to telnet
+ * resources rather than interactive use.
+ * <p>
+ ***/
+
+// This class requires the IOUtil support class!
+public final class WeatherTelnet
+{
+
+ public final static void main(String[] args)
+ {
+ TelnetClient telnet;
+
+ telnet = new TelnetClient();
+
+ try
+ {
+ telnet.connect("rainmaker.wunderground.com", 3000);
+ }
+ catch (IOException e)
+ {
+ e.printStackTrace();
+ System.exit(1);
+ }
+
+ IOUtil.readWrite(telnet.getInputStream(), telnet.getOutputStream(),
+ System.in, System.out);
+
+ try
+ {
+ telnet.disconnect();
+ }
+ catch (IOException e)
+ {
+ e.printStackTrace();
+ System.exit(1);
+ }
+
+ System.exit(0);
+ }
+
+}
+
+
Propchange: commons/proper/net/branches/NET_2_0/src/main/java/examples/telnet/WeatherTelnet.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/proper/net/branches/NET_2_0/src/main/java/examples/unix/chargen.java
URL: http://svn.apache.org/viewvc/commons/proper/net/branches/NET_2_0/src/main/java/examples/unix/chargen.java?rev=761995&view=auto
==============================================================================
--- commons/proper/net/branches/NET_2_0/src/main/java/examples/unix/chargen.java (added)
+++ commons/proper/net/branches/NET_2_0/src/main/java/examples/unix/chargen.java Sat Apr 4 19:46:47 2009
@@ -0,0 +1,156 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package examples.unix;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.InterruptedIOException;
+import java.net.InetAddress;
+import java.net.SocketException;
+
+import org.apache.commons.net.chargen.CharGenTCPClient;
+import org.apache.commons.net.chargen.CharGenUDPClient;
+
+/***
+ * This is an example program demonstrating how to use the CharGenTCPClient
+ * and CharGenUDPClient classes. This program connects to the default
+ * chargen service port of a specified server, then reads 100 lines from
+ * of generated output, writing each line to standard output, and then
+ * closes the connection. The UDP invocation of the program sends 50
+ * datagrams, printing the reply to each.
+ * The default is to use the TCP port. Use the -udp flag to use the UDP
+ * port.
+ * <p>
+ * Usage: chargen [-udp] <hostname>
+ * <p>
+ ***/
+public final class chargen
+{
+
+ public static final void chargenTCP(String host) throws IOException
+ {
+ int lines = 100;
+ String line;
+ CharGenTCPClient client = new CharGenTCPClient();
+ BufferedReader chargenInput;
+
+ // We want to timeout if a response takes longer than 60 seconds
+ client.setDefaultTimeout(60000);
+ client.connect(host);
+ chargenInput =
+ new BufferedReader(new InputStreamReader(client.getInputStream()));
+
+ // We assume the chargen service outputs lines, but it really doesn't
+ // have to, so this code might actually not work if no newlines are
+ // present.
+ while (lines-- > 0)
+ {
+ if ((line = chargenInput.readLine()) == null)
+ break;
+ System.out.println(line);
+ }
+
+ client.disconnect();
+ }
+
+ public static final void chargenUDP(String host) throws IOException
+ {
+ int packets = 50;
+ byte[] data;
+ InetAddress address;
+ CharGenUDPClient client;
+
+ address = InetAddress.getByName(host);
+ client = new CharGenUDPClient();
+
+ client.open();
+ // If we don't receive a return packet within 5 seconds, assume
+ // the packet is lost.
+ client.setSoTimeout(5000);
+
+ while (packets-- > 0)
+ {
+ client.send(address);
+
+ try
+ {
+ data = client.receive();
+ }
+ // Here we catch both SocketException and InterruptedIOException,
+ // because even though the JDK 1.1 docs claim that
+ // InterruptedIOException is thrown on a timeout, it seems
+ // SocketException is also thrown.
+ catch (SocketException e)
+ {
+ // We timed out and assume the packet is lost.
+ System.err.println("SocketException: Timed out and dropped packet");
+ continue;
+ }
+ catch (InterruptedIOException e)
+ {
+ // We timed out and assume the packet is lost.
+ System.err.println(
+ "InterruptedIOException: Timed out and dropped packet");
+ continue;
+ }
+ System.out.write(data);
+ System.out.flush();
+ }
+
+ client.close();
+ }
+
+
+ public static final void main(String[] args)
+ {
+
+ if (args.length == 1)
+ {
+ try
+ {
+ chargenTCP(args[0]);
+ }
+ catch (IOException e)
+ {
+ e.printStackTrace();
+ System.exit(1);
+ }
+ }
+ else if (args.length == 2 && args[0].equals("-udp"))
+ {
+ try
+ {
+ chargenUDP(args[1]);
+ }
+ catch (IOException e)
+ {
+ e.printStackTrace();
+ System.exit(1);
+ }
+ }
+ else
+ {
+ System.err.println("Usage: chargen [-udp] <hostname>");
+ System.exit(1);
+ }
+
+ }
+
+}
+
Propchange: commons/proper/net/branches/NET_2_0/src/main/java/examples/unix/chargen.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/proper/net/branches/NET_2_0/src/main/java/examples/unix/daytime.java
URL: http://svn.apache.org/viewvc/commons/proper/net/branches/NET_2_0/src/main/java/examples/unix/daytime.java?rev=761995&view=auto
==============================================================================
--- commons/proper/net/branches/NET_2_0/src/main/java/examples/unix/daytime.java (added)
+++ commons/proper/net/branches/NET_2_0/src/main/java/examples/unix/daytime.java Sat Apr 4 19:46:47 2009
@@ -0,0 +1,100 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package examples.unix;
+
+import java.io.IOException;
+import java.net.InetAddress;
+
+import org.apache.commons.net.daytime.DaytimeTCPClient;
+import org.apache.commons.net.daytime.DaytimeUDPClient;
+
+/***
+ * This is an example program demonstrating how to use the DaytimeTCP
+ * and DaytimeUDP classes.
+ * This program connects to the default daytime service port of a
+ * specified server, retrieves the daytime, and prints it to standard output.
+ * The default is to use the TCP port. Use the -udp flag to use the UDP
+ * port.
+ * <p>
+ * Usage: daytime [-udp] <hostname>
+ * <p>
+ ***/
+public final class daytime
+{
+
+ public static final void daytimeTCP(String host) throws IOException
+ {
+ DaytimeTCPClient client = new DaytimeTCPClient();
+
+ // We want to timeout if a response takes longer than 60 seconds
+ client.setDefaultTimeout(60000);
+ client.connect(host);
+ System.out.println(client.getTime().trim());
+ client.disconnect();
+ }
+
+ public static final void daytimeUDP(String host) throws IOException
+ {
+ DaytimeUDPClient client = new DaytimeUDPClient();
+
+ // We want to timeout if a response takes longer than 60 seconds
+ client.setDefaultTimeout(60000);
+ client.open();
+ System.out.println(client.getTime(
+ InetAddress.getByName(host)).trim());
+ client.close();
+ }
+
+
+ public static final void main(String[] args)
+ {
+
+ if (args.length == 1)
+ {
+ try
+ {
+ daytimeTCP(args[0]);
+ }
+ catch (IOException e)
+ {
+ e.printStackTrace();
+ System.exit(1);
+ }
+ }
+ else if (args.length == 2 && args[0].equals("-udp"))
+ {
+ try
+ {
+ daytimeUDP(args[1]);
+ }
+ catch (IOException e)
+ {
+ e.printStackTrace();
+ System.exit(1);
+ }
+ }
+ else
+ {
+ System.err.println("Usage: daytime [-udp] <hostname>");
+ System.exit(1);
+ }
+
+ }
+
+}
+
Propchange: commons/proper/net/branches/NET_2_0/src/main/java/examples/unix/daytime.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/proper/net/branches/NET_2_0/src/main/java/examples/unix/echo.java
URL: http://svn.apache.org/viewvc/commons/proper/net/branches/NET_2_0/src/main/java/examples/unix/echo.java?rev=761995&view=auto
==============================================================================
--- commons/proper/net/branches/NET_2_0/src/main/java/examples/unix/echo.java (added)
+++ commons/proper/net/branches/NET_2_0/src/main/java/examples/unix/echo.java Sat Apr 4 19:46:47 2009
@@ -0,0 +1,169 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package examples.unix;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.InterruptedIOException;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
+import java.net.InetAddress;
+import java.net.SocketException;
+
+import org.apache.commons.net.echo.EchoTCPClient;
+import org.apache.commons.net.echo.EchoUDPClient;
+
+/***
+ * This is an example program demonstrating how to use the EchoTCPClient
+ * and EchoUDPClient classes. This program connects to the default echo
+ * service port of a specified server, then reads lines from standard
+ * input, writing them to the echo server, and then printing the echo.
+ * The default is to use the TCP port. Use the -udp flag to use the UDP
+ * port.
+ * <p>
+ * Usage: echo [-udp] <hostname>
+ * <p>
+ ***/
+public final class echo
+{
+
+ public static final void echoTCP(String host) throws IOException
+ {
+ EchoTCPClient client = new EchoTCPClient();
+ BufferedReader input, echoInput;
+ PrintWriter echoOutput;
+ String line;
+
+ // We want to timeout if a response takes longer than 60 seconds
+ client.setDefaultTimeout(60000);
+ client.connect(host);
+ System.out.println("Connected to " + host + ".");
+ input = new BufferedReader(new InputStreamReader(System.in));
+ echoOutput =
+ new PrintWriter(new OutputStreamWriter(client.getOutputStream()), true);
+ echoInput =
+ new BufferedReader(new InputStreamReader(client.getInputStream()));
+
+ while ((line = input.readLine()) != null)
+ {
+ echoOutput.println(line);
+ System.out.println(echoInput.readLine());
+ }
+
+ client.disconnect();
+ }
+
+ public static final void echoUDP(String host) throws IOException
+ {
+ int length, count;
+ byte[] data;
+ String line;
+ BufferedReader input;
+ InetAddress address;
+ EchoUDPClient client;
+
+ input = new BufferedReader(new InputStreamReader(System.in));
+ address = InetAddress.getByName(host);
+ client = new EchoUDPClient();
+
+ client.open();
+ // If we don't receive an echo within 5 seconds, assume the packet is lost.
+ client.setSoTimeout(5000);
+ System.out.println("Ready to echo to " + host + ".");
+
+ // Remember, there are no guarantees about the ordering of returned
+ // UDP packets, so there is a chance the output may be jumbled.
+ while ((line = input.readLine()) != null)
+ {
+ data = line.getBytes();
+ client.send(data, address);
+ count = 0;
+ do
+ {
+ try
+ {
+ length = client.receive(data);
+ }
+ // Here we catch both SocketException and InterruptedIOException,
+ // because even though the JDK 1.1 docs claim that
+ // InterruptedIOException is thrown on a timeout, it seems
+ // SocketException is also thrown.
+ catch (SocketException e)
+ {
+ // We timed out and assume the packet is lost.
+ System.err.println(
+ "SocketException: Timed out and dropped packet");
+ break;
+ }
+ catch (InterruptedIOException e)
+ {
+ // We timed out and assume the packet is lost.
+ System.err.println(
+ "InterruptedIOException: Timed out and dropped packet");
+ break;
+ }
+ System.out.print(new String(data, 0, length));
+ count += length;
+ }
+ while (count < data.length);
+
+ System.out.println();
+ }
+
+ client.close();
+ }
+
+
+ public static final void main(String[] args)
+ {
+
+ if (args.length == 1)
+ {
+ try
+ {
+ echoTCP(args[0]);
+ }
+ catch (IOException e)
+ {
+ e.printStackTrace();
+ System.exit(1);
+ }
+ }
+ else if (args.length == 2 && args[0].equals("-udp"))
+ {
+ try
+ {
+ echoUDP(args[1]);
+ }
+ catch (IOException e)
+ {
+ e.printStackTrace();
+ System.exit(1);
+ }
+ }
+ else
+ {
+ System.err.println("Usage: echo [-udp] <hostname>");
+ System.exit(1);
+ }
+
+ }
+
+}
+
Propchange: commons/proper/net/branches/NET_2_0/src/main/java/examples/unix/echo.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/proper/net/branches/NET_2_0/src/main/java/examples/unix/finger.java
URL: http://svn.apache.org/viewvc/commons/proper/net/branches/NET_2_0/src/main/java/examples/unix/finger.java?rev=761995&view=auto
==============================================================================
--- commons/proper/net/branches/NET_2_0/src/main/java/examples/unix/finger.java (added)
+++ commons/proper/net/branches/NET_2_0/src/main/java/examples/unix/finger.java Sat Apr 4 19:46:47 2009
@@ -0,0 +1,149 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package examples.unix;
+
+import java.io.IOException;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+
+import org.apache.commons.net.finger.FingerClient;
+
+/***
+ * This is an example of how you would implement the finger command
+ * in Java using NetComponents. The Java version is much shorter.
+ * But keep in mind that the Unix finger command reads all sorts of
+ * local files to output local finger information. This program only
+ * queries the finger daemon.
+ * <p>
+ * The -l flag is used to request long output from the server.
+ * <p>
+ ***/
+public final class finger
+{
+
+ public static final void main(String[] args)
+ {
+ boolean longOutput = false;
+ int arg = 0, index;
+ String handle, host;
+ FingerClient finger;
+ InetAddress address = null;
+
+ // Get flags. If an invalid flag is present, exit with usage message.
+ while (arg < args.length && args[arg].startsWith("-"))
+ {
+ if (args[arg].equals("-l"))
+ longOutput = true;
+ else
+ {
+ System.err.println("usage: finger [-l] [[[handle][@<server>]] ...]");
+ System.exit(1);
+ }
+ ++arg;
+ }
+
+
+ finger = new FingerClient();
+ // We want to timeout if a response takes longer than 60 seconds
+ finger.setDefaultTimeout(60000);
+
+ if (arg >= args.length)
+ {
+ // Finger local host
+
+ try
+ {
+ address = InetAddress.getLocalHost();
+ }
+ catch (UnknownHostException e)
+ {
+ System.err.println("Error unknown host: " + e.getMessage());
+ System.exit(1);
+ }
+
+ try
+ {
+ finger.connect(address);
+ System.out.print(finger.query(longOutput));
+ finger.disconnect();
+ }
+ catch (IOException e)
+ {
+ System.err.println("Error I/O exception: " + e.getMessage());
+ System.exit(1);
+ }
+
+ return ;
+ }
+
+ // Finger each argument
+ while (arg < args.length)
+ {
+
+ index = args[arg].lastIndexOf("@");
+
+ if (index == -1)
+ {
+ handle = args[arg];
+ try
+ {
+ address = InetAddress.getLocalHost();
+ }
+ catch (UnknownHostException e)
+ {
+ System.err.println("Error unknown host: " + e.getMessage());
+ System.exit(1);
+ }
+ }
+ else
+ {
+ handle = args[arg].substring(0, index);
+ host = args[arg].substring(index + 1);
+
+ try
+ {
+ address = InetAddress.getByName(host);
+ }
+ catch (UnknownHostException e)
+ {
+ System.err.println("Error unknown host: " + e.getMessage());
+ System.exit(1);
+ }
+ }
+
+ System.out.println("[" + address.getHostName() + "]");
+
+ try
+ {
+ finger.connect(address);
+ System.out.print(finger.query(longOutput, handle));
+ finger.disconnect();
+ }
+ catch (IOException e)
+ {
+ System.err.println("Error I/O exception: " + e.getMessage());
+ System.exit(1);
+ }
+
+ ++arg;
+ if (arg != args.length)
+ System.out.print("\n");
+ }
+ }
+}
+
Propchange: commons/proper/net/branches/NET_2_0/src/main/java/examples/unix/finger.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/proper/net/branches/NET_2_0/src/main/java/examples/unix/fwhois.java
URL: http://svn.apache.org/viewvc/commons/proper/net/branches/NET_2_0/src/main/java/examples/unix/fwhois.java?rev=761995&view=auto
==============================================================================
--- commons/proper/net/branches/NET_2_0/src/main/java/examples/unix/fwhois.java (added)
+++ commons/proper/net/branches/NET_2_0/src/main/java/examples/unix/fwhois.java Sat Apr 4 19:46:47 2009
@@ -0,0 +1,89 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package examples.unix;
+
+import java.io.IOException;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+
+import org.apache.commons.net.whois.WhoisClient;
+
+/***
+ * This is an example of how you would implement the Linux fwhois command
+ * in Java using NetComponents. The Java version is much shorter.
+ * <p>
+ ***/
+public final class fwhois
+{
+
+ public static final void main(String[] args)
+ {
+ int index;
+ String handle, host;
+ InetAddress address = null;
+ WhoisClient whois;
+
+ if (args.length != 1)
+ {
+ System.err.println("usage: fwhois handle[@<server>]");
+ System.exit(1);
+ }
+
+ index = args[0].lastIndexOf("@");
+
+ whois = new WhoisClient();
+ // We want to timeout if a response takes longer than 60 seconds
+ whois.setDefaultTimeout(60000);
+
+ if (index == -1)
+ {
+ handle = args[0];
+ host = WhoisClient.DEFAULT_HOST;
+ }
+ else
+ {
+ handle = args[0].substring(0, index);
+ host = args[0].substring(index + 1);
+ }
+
+ try
+ {
+ address = InetAddress.getByName(host);
+ }
+ catch (UnknownHostException e)
+ {
+ System.err.println("Error unknown host: " + e.getMessage());
+ System.exit(1);
+ }
+
+ System.out.println("[" + address.getHostName() + "]");
+
+ try
+ {
+ whois.connect(address);
+ System.out.print(whois.query(handle));
+ whois.disconnect();
+ }
+ catch (IOException e)
+ {
+ System.err.println("Error I/O exception: " + e.getMessage());
+ System.exit(1);
+ }
+ }
+
+}
Propchange: commons/proper/net/branches/NET_2_0/src/main/java/examples/unix/fwhois.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/proper/net/branches/NET_2_0/src/main/java/examples/unix/rdate.java
URL: http://svn.apache.org/viewvc/commons/proper/net/branches/NET_2_0/src/main/java/examples/unix/rdate.java?rev=761995&view=auto
==============================================================================
--- commons/proper/net/branches/NET_2_0/src/main/java/examples/unix/rdate.java (added)
+++ commons/proper/net/branches/NET_2_0/src/main/java/examples/unix/rdate.java Sat Apr 4 19:46:47 2009
@@ -0,0 +1,102 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package examples.unix;
+
+import java.io.IOException;
+import java.net.InetAddress;
+
+import org.apache.commons.net.time.TimeTCPClient;
+import org.apache.commons.net.time.TimeUDPClient;
+
+/***
+ * This is an example program demonstrating how to use the TimeTCPClient
+ * and TimeUDPClient classes. It's very similar to the simple Unix rdate
+ * command. This program connects to the default time service port of a
+ * specified server, retrieves the time, and prints it to standard output.
+ * The default is to use the TCP port. Use the -udp flag to use the UDP
+ * port. You can test this program by using the NIST time server at
+ * 132.163.135.130 (warning: the IP address may change).
+ * <p>
+ * Usage: rdate [-udp] <hostname>
+ * <p>
+ * <p>
+ * @author Daniel F. Savarese
+ ***/
+public final class rdate
+{
+
+ public static final void timeTCP(String host) throws IOException
+ {
+ TimeTCPClient client = new TimeTCPClient();
+
+ // We want to timeout if a response takes longer than 60 seconds
+ client.setDefaultTimeout(60000);
+ client.connect(host);
+ System.out.println(client.getDate().toString());
+ client.disconnect();
+ }
+
+ public static final void timeUDP(String host) throws IOException
+ {
+ TimeUDPClient client = new TimeUDPClient();
+
+ // We want to timeout if a response takes longer than 60 seconds
+ client.setDefaultTimeout(60000);
+ client.open();
+ System.out.println(client.getDate(InetAddress.getByName(host)).toString());
+ client.close();
+ }
+
+
+ public static final void main(String[] args)
+ {
+
+ if (args.length == 1)
+ {
+ try
+ {
+ timeTCP(args[0]);
+ }
+ catch (IOException e)
+ {
+ e.printStackTrace();
+ System.exit(1);
+ }
+ }
+ else if (args.length == 2 && args[0].equals("-udp"))
+ {
+ try
+ {
+ timeUDP(args[1]);
+ }
+ catch (IOException e)
+ {
+ e.printStackTrace();
+ System.exit(1);
+ }
+ }
+ else
+ {
+ System.err.println("Usage: rdate [-udp] <hostname>");
+ System.exit(1);
+ }
+
+ }
+
+}
+
Propchange: commons/proper/net/branches/NET_2_0/src/main/java/examples/unix/rdate.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/proper/net/branches/NET_2_0/src/main/java/examples/unix/rexec.java
URL: http://svn.apache.org/viewvc/commons/proper/net/branches/NET_2_0/src/main/java/examples/unix/rexec.java?rev=761995&view=auto
==============================================================================
--- commons/proper/net/branches/NET_2_0/src/main/java/examples/unix/rexec.java (added)
+++ commons/proper/net/branches/NET_2_0/src/main/java/examples/unix/rexec.java Sat Apr 4 19:46:47 2009
@@ -0,0 +1,109 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package examples.unix;
+
+import java.io.IOException;
+import org.apache.commons.net.bsd.RExecClient;
+
+import examples.util.IOUtil;
+
+/***
+ * This is an example program demonstrating how to use the RExecClient class.
+ * This program connects to an rexec server and requests that the
+ * given command be executed on the server. It then reads input from stdin
+ * (this will be line buffered on most systems, so don't expect character
+ * at a time interactivity), passing it to the remote process and writes
+ * the process stdout and stderr to local stdout.
+ * <p>
+ * Example: java rexec myhost myusername mypassword "ps -aux"
+ * <p>
+ * Usage: rexec <hostname> <username> <password> <command>
+ * <p>
+ ***/
+
+// This class requires the IOUtil support class!
+public final class rexec
+{
+
+ public static final void main(String[] args)
+ {
+ String server, username, password, command;
+ RExecClient client;
+
+ if (args.length != 4)
+ {
+ System.err.println(
+ "Usage: rexec <hostname> <username> <password> <command>");
+ System.exit(1);
+ return ; // so compiler can do proper flow control analysis
+ }
+
+ client = new RExecClient();
+
+ server = args[0];
+ username = args[1];
+ password = args[2];
+ command = args[3];
+
+ try
+ {
+ client.connect(server);
+ }
+ catch (IOException e)
+ {
+ System.err.println("Could not connect to server.");
+ e.printStackTrace();
+ System.exit(1);
+ }
+
+ try
+ {
+ client.rexec(username, password, command);
+ }
+ catch (IOException e)
+ {
+ try
+ {
+ client.disconnect();
+ }
+ catch (IOException f)
+ {}
+ e.printStackTrace();
+ System.err.println("Could not execute command.");
+ System.exit(1);
+ }
+
+
+ IOUtil.readWrite(client.getInputStream(), client.getOutputStream(),
+ System.in, System.out);
+
+ try
+ {
+ client.disconnect();
+ }
+ catch (IOException e)
+ {
+ e.printStackTrace();
+ System.exit(1);
+ }
+
+ System.exit(0);
+ }
+
+}
+
Propchange: commons/proper/net/branches/NET_2_0/src/main/java/examples/unix/rexec.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/proper/net/branches/NET_2_0/src/main/java/examples/unix/rlogin.java
URL: http://svn.apache.org/viewvc/commons/proper/net/branches/NET_2_0/src/main/java/examples/unix/rlogin.java?rev=761995&view=auto
==============================================================================
--- commons/proper/net/branches/NET_2_0/src/main/java/examples/unix/rlogin.java (added)
+++ commons/proper/net/branches/NET_2_0/src/main/java/examples/unix/rlogin.java Sat Apr 4 19:46:47 2009
@@ -0,0 +1,120 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package examples.unix;
+
+import java.io.IOException;
+import org.apache.commons.net.bsd.RLoginClient;
+
+import examples.util.IOUtil;
+
+/***
+ * This is an example program demonstrating how to use the RLoginClient
+ * class. This program connects to an rlogin daemon and begins to
+ * interactively read input from stdin (this will be line buffered on most
+ * systems, so don't expect character at a time interactivity), passing it
+ * to the remote login process and writing the remote stdout and stderr
+ * to local stdout. If you don't have .rhosts or hosts.equiv files set up,
+ * the rlogin daemon will prompt you for a password.
+ * <p>
+ * On Unix systems you will not be able to use the rshell capability
+ * unless the process runs as root since only root can bind port addresses
+ * lower than 1024.
+ * <p>
+ * JVM's using green threads will likely have problems if the rlogin daemon
+ * requests a password. This program is merely a demonstration and is
+ * not suitable for use as an application, especially given that it relies
+ * on line buffered input from System.in. The best way to run this example
+ * is probably from a Win95 dos box into a Unix host.
+ * <p>
+ * Example: java rlogin myhost localusername remoteusername vt100
+ * <p>
+ * Usage: rlogin <hostname> <localuser> <remoteuser> <terminal>
+ * <p>
+ ***/
+
+// This class requires the IOUtil support class!
+public final class rlogin
+{
+
+ public static final void main(String[] args)
+ {
+ String server, localuser, remoteuser, terminal;
+ RLoginClient client;
+
+ if (args.length != 4)
+ {
+ System.err.println(
+ "Usage: rlogin <hostname> <localuser> <remoteuser> <terminal>");
+ System.exit(1);
+ return ; // so compiler can do proper flow control analysis
+ }
+
+ client = new RLoginClient();
+
+ server = args[0];
+ localuser = args[1];
+ remoteuser = args[2];
+ terminal = args[3];
+
+ try
+ {
+ client.connect(server);
+ }
+ catch (IOException e)
+ {
+ System.err.println("Could not connect to server.");
+ e.printStackTrace();
+ System.exit(1);
+ }
+
+ try
+ {
+ client.rlogin(localuser, remoteuser, terminal);
+ }
+ catch (IOException e)
+ {
+ try
+ {
+ client.disconnect();
+ }
+ catch (IOException f)
+ {}
+ e.printStackTrace();
+ System.err.println("rlogin authentication failed.");
+ System.exit(1);
+ }
+
+
+ IOUtil.readWrite(client.getInputStream(), client.getOutputStream(),
+ System.in, System.out);
+
+ try
+ {
+ client.disconnect();
+ }
+ catch (IOException e)
+ {
+ e.printStackTrace();
+ System.exit(1);
+ }
+
+ System.exit(0);
+ }
+
+}
+
Propchange: commons/proper/net/branches/NET_2_0/src/main/java/examples/unix/rlogin.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/proper/net/branches/NET_2_0/src/main/java/examples/unix/rshell.java
URL: http://svn.apache.org/viewvc/commons/proper/net/branches/NET_2_0/src/main/java/examples/unix/rshell.java?rev=761995&view=auto
==============================================================================
--- commons/proper/net/branches/NET_2_0/src/main/java/examples/unix/rshell.java (added)
+++ commons/proper/net/branches/NET_2_0/src/main/java/examples/unix/rshell.java Sat Apr 4 19:46:47 2009
@@ -0,0 +1,113 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package examples.unix;
+
+import java.io.IOException;
+import org.apache.commons.net.bsd.RCommandClient;
+
+import examples.util.IOUtil;
+
+/***
+ * This is an example program demonstrating how to use the RCommandClient
+ * class. This program connects to an rshell daemon and requests that the
+ * given command be executed on the server. It then reads input from stdin
+ * (this will be line buffered on most systems, so don't expect character
+ * at a time interactivity), passing it to the remote process and writes
+ * the process stdout and stderr to local stdout.
+ * <p>
+ * On Unix systems you will not be able to use the rshell capability
+ * unless the process runs as root since only root can bind port addresses
+ * lower than 1024.
+ * <p>
+ * Example: java rshell myhost localusername remoteusername "ps -aux"
+ * <p>
+ * Usage: rshell <hostname> <localuser> <remoteuser> <command>
+ * <p>
+ ***/
+
+// This class requires the IOUtil support class!
+public final class rshell
+{
+
+ public static final void main(String[] args)
+ {
+ String server, localuser, remoteuser, command;
+ RCommandClient client;
+
+ if (args.length != 4)
+ {
+ System.err.println(
+ "Usage: rshell <hostname> <localuser> <remoteuser> <command>");
+ System.exit(1);
+ return ; // so compiler can do proper flow control analysis
+ }
+
+ client = new RCommandClient();
+
+ server = args[0];
+ localuser = args[1];
+ remoteuser = args[2];
+ command = args[3];
+
+ try
+ {
+ client.connect(server);
+ }
+ catch (IOException e)
+ {
+ System.err.println("Could not connect to server.");
+ e.printStackTrace();
+ System.exit(1);
+ }
+
+ try
+ {
+ client.rcommand(localuser, remoteuser, command);
+ }
+ catch (IOException e)
+ {
+ try
+ {
+ client.disconnect();
+ }
+ catch (IOException f)
+ {}
+ e.printStackTrace();
+ System.err.println("Could not execute command.");
+ System.exit(1);
+ }
+
+
+ IOUtil.readWrite(client.getInputStream(), client.getOutputStream(),
+ System.in, System.out);
+
+ try
+ {
+ client.disconnect();
+ }
+ catch (IOException e)
+ {
+ e.printStackTrace();
+ System.exit(1);
+ }
+
+ System.exit(0);
+ }
+
+}
+
Propchange: commons/proper/net/branches/NET_2_0/src/main/java/examples/unix/rshell.java
------------------------------------------------------------------------------
svn:eol-style = native