Re: Help wiith input/output of jar
On Aug 12, 6:20 pm, bH <bherbs...@hotmail.com> wrote:
On Aug 10, 11:01 pm, "John B. Matthews" <nos...@nospam.invalid> wrote:
In article
<077030bb-eb70-49b5-a33f-905904ed0...@z31g2000yqd.googlegroups.com>, =
bH <bherbs...@hotmail.com> wrote:
[...]
Finally, you wanted information about telnet and port in
a Windows XP. My internet wanderings suggest that it is
but with specifics to ports, security and firewalls.
In the interim, I stumbled across a machine running Vista, and I
thought I'd give it a try. Enabling telnet turned out to be an
obscure, tedious administrative task. Moreover, it proved easier to
comandeer an open port that to finesse the firewall.
When I finally got the programs to run with frames, I
placed them in jars. And now each can be used as a click
on it to demonstrate EchoServer and EchoClient.
Capital idea! You probably finished before me. :-)
[...]
--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>
Hi John,
The following is my effort to revise your code to
my EchoServer. I do not understand how to write a
thread in a similar manner that you did. Yet I did
use some of lines that you have written.
It is necessary in this situation to have the
EchoServer to be running when the EchoClient
is started.
Thanks again for your efforts.
bH
import java.awt.*;
import java.io.PrintWriter;
import java.net.*;
import javax.swing.*;
import java.nio.*;
import java.nio.channels.*;
import java.util.*;
import java.io.IOException;
// This is a hands off server:
// I left out any refernce to a button
// I didn't need it, in my estimation.
public class EchoServerJMv1 implements Runnable {
private static final int PORT = 4444;
private final JTextField tf = new JTextField(25);;
private final JTextArea ta = new JTextArea(15, 25);;
private volatile PrintWriter out;
private Scanner in;
private Thread listener;
public static int DEFAULT_PORT = 4444;
public static int port = DEFAULT_PORT;
public EchoServerJMv1() {
JFrame f = new JFrame("Echo Server");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new JScrollPane(ta), BorderLayout.CENTER);
f.setLocation(300, 300);
f.pack();
f.setVisible(true);
ta.setLineWrap(true);
ta.setWrapStyleWord(true);
display("Using LocalHost and port " + PORT);
display("Listening for connections");
listener = new Thread(this, "Listener");
}
public void start() {
display("Came thru 'start()' to just print a 'hello'");
listener.start();
}
@Override
public void run() {
ServerSocketChannel serverChannel;
Selector selector;
try {
serverChannel = ServerSocketChannel.open();
ServerSocket ss = serverChannel.socket();
InetSocketAddress address = new InetSocketAddress(port);
ss.bind(address);
serverChannel.configureBlocking(false);
selector = Selector.open();
serverChannel.register(selector, SelectionKey.OP_ACCEPT);
}
catch (IOException ex) {
ex.printStackTrace();
return;
}
while (true) {
try {
selector.select();
}
catch (IOException ex) {
ex.printStackTrace();
break;
}
Set readyKeys = selector.selectedKeys();
Iterator iterator = readyKeys.iterator();
while (iterator.hasNext()) {
SelectionKey key = (SelectionKey) iterator.next();
iterator.remove();
try {
if (key.isAcceptable()) {
ServerSocketChannel server =
(ServerSocketChannel ) key.channel();
SocketChannel client = server.accept();
display("Accepted connection from " + client);
client.configureBlocking(false);
SelectionKey clientKey = client.register(select=
or,
SelectionKey.OP_WRITE | SelectionKey.OP_READ);
ByteBuffer buffer = ByteBuffer.allocate(100);
clientKey.attach(buffer);
}
if (key.isReadable()) {
SocketChannel client = (SocketChannel) key.chan=
nel();
ByteBuffer output = (ByteBuffer) key.attachment=
();
client.read(output);
}
if (key.isWritable()) {
SocketChannel client =
(SocketChannel) key.channel();
ByteBuffer output = (ByteBuffer) key.attachment=
();
output.flip();
client.write(output);
output.compact();
}
}
catch (IOException ex) {
key.cancel();
try {
key.channel().close();
}
catch (IOException cex) {}
}
}
}
}
private void display(String s) {
ta.append(s + "\u23CE\n");
ta.setCaretPosition(ta.getDocument().getLength());
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new EchoServerJMv1().start();
}
});
}
}- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -
Hi John,
The following is my effort to revise your code to
my EchoClient. I do not understand how to write a
thread in a similar manner that you did. Yet I did
use some of lines that you have written.
It is necessary in this situation to have the
EchoServer to be running when the EchoClient
is started.
Thanks again for your efforts.
bH
import java.awt.*;
import java.awt.event.*;
import java.io.PrintWriter;
import java.net.*;
import javax.swing.*;
import java.io.*;
import javax.swing.*;
public class EchoClientJMv1 implements ActionListener,
Runnable {
private static final int PORT = 4444;
private final JTextField tf = new JTextField(25);;
private final JTextArea ta = new JTextArea(15, 25);;
private final JButton send = new JButton("Send");
private volatile PrintWriter out;
private Thread listener;
private String s = "";
public EchoClientJMv1() {
JFrame f = new JFrame("Echo Server");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getRootPane().setDefaultButton(send);
f.add(tf, BorderLayout.NORTH);
f.add(new JScrollPane(ta), BorderLayout.CENTER);
f.add(send, BorderLayout.SOUTH);
f.setLocation(300, 300);
f.pack();
f.setVisible(true);
send.addActionListener(this);
ta.setLineWrap(true);
ta.setWrapStyleWord(true);
display("Please use LocalHost to port " + PORT);
listener = new Thread(this, "Listener");
}
public void start() {
display("Came thru 'start()' to just print a 'hello'");
listener.start();
}
@Override
public void actionPerformed(ActionEvent ae) {
try {
String host = "localhost";
Socket t = new Socket(host, PORT);
BufferedReader in
= new BufferedReader(new
InputStreamReader(t.getInputStream()));
PrintWriter out
= new PrintWriter(new
OutputStreamWriter(t.getOutputStream()));
String s = tf.getText();
display("Sent: " + s);
out.println(s); //to server
out.flush();
tf.setText("");// clear the input tf
String str = in.readLine();
if (str != null) {
display("Received: "+ str + " \n"); // from server
}
} catch (Exception e) {
display("Error: " + e);
}
}
@Override
public void run() {
display("Came thru 'run()' to just print a 'hello'");
}
private void display(String s) {
ta.append(s + "\u23CE\n");
ta.setCaretPosition(ta.getDocument().getLength());
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new EchoClientJMv1().start();
}
});
}
}