Results 1 to 1 of 1
Thread: DataOutputStream
- 10-02-2011, 07:27 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 8
- Rep Power
- 0
DataOutputStream
I have a Server / Client relationship that I've built. I have a problem with the client. The server works great. Simple DNS lookup. I have it set so I can telnet into it and it connects fine and immediately returns "Connected to" + serverName. Typically localhost. That's the first line. The second line is "Escape character is '^]'". The server then waits for the ipName such as "www.java-forums.org" then does the lookup returns the IP address and then disconnects the client.
The part I'm having problems with is the client. The usage of the client iswhere the first argument is the server to connect to and the second argument is the address to lookup.Java Code:$java DNSClient 127.0.0.1 www.java-forums.org
Java Code:import java.net.*; import java.io.*; public class DNSClient { // the default port public static final int PORT = 6052; // this could be replaced with an IP address or IP name public static final String host = "localhost"; public static void main(String[] args) throws java.io.IOException { if (args.length == 0) System.out.println("No Server Connection. Please provide Server Name."); else if (args.length == 1){ System.out.println("IP Name has not been specified."); System.out.println("Please input address like 'www.google.com'"); } else if (args.length == 2){ Socket clientConn = null; BufferedReader fromServer = null; DataOutputStream toServer = null; // 1. read the IPName from args array String serverName = args[0]; String ipName = args[1]; String ipAddy; try { // 2. open a socket connection to default port clientConn = new Socket(serverName, PORT); // 3. Write the ipName to the socket toServer = new DataOutputStream(clientConn.getOutputStream()); toServer.write(ipName.getBytes()); // 4. read IP address from the socket fromServer = new BufferedReader(new InputStreamReader(clientConn.getInputStream())); ipAddy = fromServer.readLine(); // 5. display the IP address System.out.println(ipAddy); } catch (java.io.IOException ioe) { System.err.println(ioe); } finally { // let's close streams and sockets if (clientConn != null) clientConn.close(); } } else System.out.println("You have specified too many arguments."); } }
So my output obviously returns the first line which is connected to localhost instead of returning the IP Address of the 2nd parameter being sent.
Am I not using DataOutputStream to send the 2nd argument correctly or should is there a way to ignore the first two lines returned from the server? and then pass the address so it can be returned to the command line?
thank you,
-blaqkout
Similar Threads
-
weird character in DataOutputStream file
By digitalmalayan in forum New To JavaReplies: 9Last Post: 06-25-2011, 04:50 PM -
Question about DataOutputStream
By Pojahn_M in forum New To JavaReplies: 10Last Post: 06-22-2011, 09:52 AM -
Dataoutputstream
By saranyabaskaran in forum New To JavaReplies: 6Last Post: 02-12-2011, 02:26 AM -
How to use DataOutputStream and DataInputStream with PipedWriter and PipedReader
By Java Tip in forum java.ioReplies: 0Last Post: 06-26-2008, 07:34 PM -
File I/O with DataOutputStream
By Tzaphiel in forum New To JavaReplies: 0Last Post: 12-16-2007, 09:39 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks