|
-
Jul 18th, 2007, 06:52 PM
#1
Thread Starter
Hyperactive Member
Sending String to a Terminal
Here is what Im trying to do:
I created an app that creates special queries. How can I have my java app automatically send those created queries directly to a terminal window? or it can directly connect to the ftp server and send the query and return output. Is this possible?
-
Jul 18th, 2007, 07:47 PM
#2
Re: Sending String to a Terminal
It's just a simple client server application.
You need a "Socket" Connection between the 2 terminals.
You will need to use the classes:
java.net.ServerSocket
java.net.Socket
To establish the client-server connection
You'll also need the classes:
java.io.ObjectInputStream
java.io.ObjectOutputStream
to transfer data between both terminals
I've seen an example to create a simple messenger using simple TCP/IP connection between 2 computers and the Client-Server approach for some reason. try searching the web, I guess you'll find more useful samples
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Jul 19th, 2007, 12:13 PM
#3
Thread Starter
Hyperactive Member
Re: Sending String to a Terminal
To use this do I need to mess with router settings? or is it good to go as is?
-
Jul 19th, 2007, 06:14 PM
#4
Re: Sending String to a Terminal
Not necessarily. It depends on whether your current configuration, I don't think I can answer this question for you. But a simple test should do it
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Jul 20th, 2007, 02:08 PM
#5
Thread Starter
Hyperactive Member
Re: Sending String to a Terminal
Here is the code im working with right now: (Im leaving the serverName blank for security purposes)
Code:
String serverName = "";
int portNumber = 22;
Socket server = null;
try
{
server = new Socket(serverName, portNumber);
InputStream input = server.getInputStream();
int byteCount;
byte inputBuffer[] = new byte[1024];
while ((byteCount = input.read(inputBuffer, 0, 1024)) != -1)
{
String stuff = new String(inputBuffer, 0, 0, byteCount);
System.out.println("Received: " + stuff);
}
} // end try
catch (Exception e) { System.out.println(e.toString()); }
System.out.println("done");
And here is the output im getting:
Received: SSH-2.0-OpenSSH_3.9p1
done
and it takes about 2 minutes to go from the first line to 'done'. Any idea on whats going on? How can I send a command? I need to login some how too, not sure how to do that. Thanks
-
Jul 20th, 2007, 05:18 PM
#6
Re: Sending String to a Terminal
Code:
int portNumber = 22;
Are you sure this port number is not being used by another service? this might be the reason for a slow data transfer
Code:
new String(inputBuffer, 0, 0, byteCount);
Don't use deprecated methods
Code:
System.out.println("Received: " + stuff);
If more than 1 KB is being read, the word "Received" will be printed over and over
try using the following code:
Code:
ObjectInputStream str=new ObjectInputStream(input);
System.out.println(str.readUTF());
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|