I need some way to detect mouse/keyboard activity on Linux. I need to record this activity and send this record to my android tablet using tcp socket. I m running this program in terminal and it is showing error Exception in thread "main"java.lang.UnsupportedClassVersionError: Mouse : Unsupported major.minor version 51.0..any help????
import java.awt.HeadlessException;
import java.awt.MouseInfo;
import java.awt.Point;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Timer;
import java.util.TimerTask;
public class Mouse {
public static void main(String[] args) throws InterruptedException {
Point p, prev_p;
p = MouseInfo.getPointerInfo().getLocation();
DatagramSocket socket = null;
try {
socket = new DatagramSocket(8988);
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
InetAddress addr = null;
try {
addr = InetAddress.getByName("107.108.203.204");
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
File file = new File("/sys/kernel/debug/usb/usbmon/6u");
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(file));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
System.err
.println("To fix the error Run as root or Change ownership of the file to the user who runs this program");
}
String line, s = null;
try {
while ((line = br.readLine()) != null) {
prev_p = p;
p = MouseInfo.getPointerInfo().getLocation();
String[] arr = line.split(" ");
if (arr.length == 8)
s = arr[7];
System.out.println(s+" "+Integer.parseInt(s.substring(2,4),16));
byte[] buffer = s.getBytes();
DatagramPacket pak = new DatagramPacket(buffer, buffer.length,
addr, 8988);
try {
socket.send(pak);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I shall not go into the basics of telling you how to use a tcp socket, that is simple enough.
However the basics of your question is that you will need to open and constantly read the /dev/input/by-id/yourmouseorkeyboardnamehere file. Reading this file will cause your program to block until there is a keyboard/mouse input (depending on if you read the keyboard or mouse file) then you will be able to read data representing what data came from the keyboard or mouse.
It should from there be fairly easy to send this data over a tcp socket to your tablet, you can learn to do that from any sockets tutorial on the Internet.
If you have any questions or need more detail please comment bellow.
Related
I have a csv file that my class with the code below is trying to read that data to use in my project, but when i run the project, I have a NullPointerException like the csv file is null, but he isn't. The path of the csv file is right and apparently I don't have any error in my class too, what I am missing?
package main.java.br.com.quantumfinance.selecaoEstagio.leitor;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class LeitorDeArquivo {
public List<String> lerArquivo() {
try (InputStream resourceAsStream = LeitorDeArquivo.class.getResourceAsStream("/acoes.csv")) {
// Leitura do arquivo.
BufferedReader br = new BufferedReader(new InputStreamReader(resourceAsStream));
// ignora a primeira linha
br.readLine();
List<String> cotacoes = new ArrayList<>();
String linha;
while ((linha = br.readLine()) != null) {
cotacoes.add(linha);
}
return cotacoes;
} catch (FileNotFoundException e) {
System.out.println("Arquivo n�o encontrado.");
e.printStackTrace();
} catch (IOException e) {
System.out.println("Erro de IO.");
e.printStackTrace();
}
throw new RuntimeException("Erro na leitura do arquivo, consulte o console para maiores detalhes.");
}
}
This is the error that I receive when trying to run the project
Exception in thread "main" java.lang.NullPointerException
at java.base/java.io.Reader.<init>(Reader.java:168)
at java.base/java.io.InputStreamReader.<init>(InputStreamReader.java:76)
at main.java.br.com.quantumfinance.selecaoEstagio.leitor.LeitorDeArquivo.lerArquivo(LeitorDeArquivo.java:17)
at main.java.Questoes.main(Questoes.java:26)
This is the project structure
I want to write a program that keeps what I've written in my file previously, and continually adds to it, instead of erasing it all every time I run the program.
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.lang.Math;
class Movie_Ratings_2 {
public static void main(String[] args) {
Scanner n = new Scanner (System.in);
String fileName = "output.txt";
String x = n.nextLine();
try {
PrintWriter outputStream = new PrintWriter(fileName);
outputStream.println(x);
outputStream.close();
outputStream.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I have created a Server Socket and enabled it to listen to incoming streams.But after enabling the connection it should display a dialog Box showing message "Server Started" ,but it does not appear . I have noticed that after enabling the socket no code after that works. I have tried searching a lot about this but seem to find no suitable answer.Here is my code:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Server
{
public Server(int i1) throws Exception{
ServerSocket MySock=new ServerSocket(i1);//opening server socket
Socket Sock=MySock.accept();//listening to client enabled
JOptionPane.showMessageDialog(null, "Server Started");
}
public static void main(String[] args) {
try {
new Server(2005);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
The problem is that ServerSocket.accept() is blocks until a connection is made..
So the JOptionPane.showMessageDialog(...) will not be executed until someone is connecting to the serversocket.
Here is a solution that handles the ServerSocket in a separate thread
import java.io.IOException;
import java.net.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.swing.*;
public class Server
{
public Server(int i1) throws Exception{
Runnable serverTask = () -> {
try {
ServerSocket MySock=new ServerSocket(i1);//opening server socket
while (true) {
Socket Sock=MySock.accept();//listening to client enabled
System.out.println("Accept from " + Sock.getInetAddress());
}
} catch (IOException e) {
System.err.println("Accept failed.");
}
};
ExecutorService executorService = Executors.newSingleThreadExecutor();
executorService.submit(serverTask);
JOptionPane.showMessageDialog(null, "Server Started");
}
public static void main(String[] args) {
try {
new Server(2005);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I try to send a message from server to a client, after client receives the message, it sends back a message to the server and so on. The problem is with receiving the message in python. The loop it's stuck there.
import socket
HOST = "localhost"
PORT = 9999
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print('Socket created')
try:
s.bind((HOST, PORT))
except socket.error as err:
print('Bind failed. Error Code : ' .format(err))
s.listen(10)
print("Socket Listening")
conn, addr = s.accept()
while(True):
conn.send(bytes("Message"+"\r\n",'UTF-8'))
print("Message sent")
data = conn.recv(1024)
print(data.decode(encoding='UTF-8'))
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class Main {
static Thread sent;
static Thread receive;
static Socket socket;
public static void main(String args[]){
try {
socket = new Socket("localhost",9999);
} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
sent = new Thread(new Runnable() {
#Override
public void run() {
try {
BufferedReader stdIn =new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
while(true){
System.out.println("Trying to read...");
String in = stdIn.readLine();
System.out.println(in);
out.print("Try"+"\r\n");
System.out.println("Message sent");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
sent.start();
try {
sent.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
The Python code is fine. The problem is that calling out.print in the Java code does not necessarily cause your message to be sent through the socket immediately. Add
out.flush();
immediately after
out.print("Try"+"\r\n");
to force the message to be sent through the socket. (flush "flushes" through the stream any data that has not yet been sent.) The Python should then be able to receive it correctly.
I'm trying to open a stream to a file on my PC and I'm trying to do it via URL (I know,It's just for leraning purposes)
this is what I'm doing:
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLConnection;
public class URLTyper {
public static void main(String[] args) {
InputStream in=null;
try {
URL url=new URL("file://127.0.0.1/c:/haxlogs.txt");
// in=url.openStream();
URLConnection conn=url.openConnection();
conn.connect();
in=conn.getInputStream();
while (true) {
int read=in.read();
if (read==-1) break;
System.out.write(read);
}
}
catch (SocketTimeoutException e){
System.out.println("timed out");
}
catch (MalformedURLException e) {
System.out.println("URL not valid");
}
catch (IOException e) {
System.out.println("unable to get data");
}
}
}
It exits throwing an IOException ("unable to access data").. why is it not working? shouldn't it get to the file like an ordinary InputStream?
thanks