Results 1 to 3 of 3
- 08-08-2012, 05:46 PM #1
Member
- Join Date
- Aug 2012
- Posts
- 2
- Rep Power
- 0
keyListener sending keystrokes over network
Hi, I'm trying to write a program that records keystrokes and sends them over the network to another program to be displayed.
The problem is that it only works for a single character and afterwards I either get a broken pipe error in the console or nothing at all. How would I get the logging end to keep sending keystrokes and the receiving end to keep getting and displaying them. Loops don't help, at least the ones iv tried but my experience in Java is still at low level and I cant seem to figure this out.
Key logging code:
Key display code:Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; import java.net.*; public class KeyLog extends JFrame implements KeyListener { //private File keys; //private FileWriter writer; //private BufferedWriter buffer; private int key; //private int kn = 0; //private int[] keyarray; private Socket socket; //private ObjectInputStream input; private ObjectOutputStream output; public static void main(String[] args) { new KeyLog(); } public KeyLog() { this.addKeyListener(this); this.setSize(0,0); this.setVisible(true); try { socket = new Socket("192.168.0.13", 25564); JOptionPane.showMessageDialog(null, "Connected to Server"); output = new ObjectOutputStream(socket.getOutputStream()); output.flush(); }catch(UnknownHostException e) { e.printStackTrace(); }catch(IOException e) { e.printStackTrace(); } } public void keyPressed(KeyEvent e) { System.out.println(e.getKeyCode()); key = e.getKeyCode(); KeySend((char)key); } public void keyReleased(KeyEvent e) { } public void keyTyped(KeyEvent e) { } public void KeySend(char capd_key) { try { output.writeObject(capd_key); output.flush(); JOptionPane.showMessageDialog(null, "Char sent"); }catch(IOException e) { e.printStackTrace(); } } }
Java Code:import java.net.*; import java.io.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class KeyRec { private static ObjectInputStream input; //private static ObjectOutputStream output; private static ServerSocket server; private static Socket socket; private static Object charset; //private static int chn = 0; private static String charstring; public static void main(String[] args) { try { server = new ServerSocket(25564, 2); socket = server.accept(); JOptionPane.showMessageDialog(null, "Connection to client established."); input = new ObjectInputStream(socket.getInputStream()); charset = input.readObject(); charstring = charset.toString(); JOptionPane.showMessageDialog(null, charstring); }catch(IOException e) { e.printStackTrace(); }catch(ClassNotFoundException e) { e.printStackTrace(); } } }Last edited by thricehex; 08-08-2012 at 05:48 PM. Reason: had to edit code
-
Re: keyListener sending keystrokes over network
You may have a Swing threading issue as you appear to be doing all on the same thread. Consider doing the socket business in a background thread.
Also, to help others better understand your code, please read up on and abide by Java naming conventions: class names start with capital letter, variable and method names with a lower-case letter. Also please improve on your code indentation as it is all over the place making it very hard to follow your logic. Consider indenting 3 spaces and having your indentations be uniform and consistent.
- 08-08-2012, 06:53 PM #3
Member
- Join Date
- Aug 2012
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Sending files over network
By TerraViper-5 in forum NetworkingReplies: 0Last Post: 04-15-2011, 08:28 PM -
sending object through network/socket
By skandalouz in forum NetworkingReplies: 1Last Post: 12-24-2009, 07:34 AM -
Sending a key over network
By cst in forum NetworkingReplies: 0Last Post: 11-04-2009, 01:19 PM -
sending file over network
By qwerty in forum NetworkingReplies: 6Last Post: 04-25-2009, 01:55 AM -
Sending .GIF files over the network
By nadia in forum NetworkingReplies: 8Last Post: 12-30-2008, 11:57 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks