Sellex Sellex - 4 years ago 110
Java Question

Program only sends message once

I have been working on this script for awhile and have been running into this issue that I haven't been able to get working. Basically I start a loop and connect to a socket. Once connected, each string I enter should send to the ip / port I set. However, it works perfect for the first string I send, but after that it won't send anymore strings I type.

package keylog;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.time.LocalDateTime;
import java.time.chrono.ChronoLocalDateTime;
import java.util.Scanner;

public class keylogger {
public static PrintWriter out = null;
public static BufferedReader in = null;
public static Socket hackee;
private static final ChronoLocalDateTime ChronoLocalDateTime = null;
private static final String BufferedReader = null;
private Scanner keyboard;
public static ChronoLocalDateTime getTime() {
return ChronoLocalDateTime;
}
public String getLine()
{
keyboard = new Scanner(System.in);
return keyboard.nextLine();
}
public static void connectToSocket(String ip, int port) throws IOException
{

hackee = new Socket(ip, port);

}
public static boolean getStatus()
{
if(BufferedReader == "quit")
{
return false;
}
else
{
return true;
}
}
public void sendData(String s)
{
try {
out = new PrintWriter(hackee.getOutputStream(), true);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
in = new BufferedReader(new InputStreamReader(hackee.getInputStream()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

out.println(s);



package keylog;
import java.io.IOException;
import java.util.Scanner;


public class keylogging {

/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Scanner keyboard = new Scanner(System.in);
keylogger time = new keylogger();
System.out.println("Enter the IP address of your target.");
String ip = keyboard.nextLine();
System.out.println("Enter the targetted port");
int port = keyboard.nextInt();
time.connectToSocket(ip, port);
while(keylogger.getStatus())
{
String toSend = time.getLine();
System.out.println();
time.sendData(toSend);
}
System.out.println(keylogger.getTime());

}
}


This program only sends the message typed in once. The purpose of this program is for someone to put in the ip and port of the person they want to communicate with.

Answer Source

I put together an example of what you are trying to do. Main point, no need to create a new Stream around the existing one.

public static void main(String[] args) throws IOException {
    Scanner kb = new Scanner(System.in);

    System.out.print("Enter IP Address: ");
    final String ip = kb.nextLine();
    System.out.print("Enter Port: ");
    final int port = kb.nextInt();

    //open socket
    final Socket socket = new Socket(ip, port);

    //create reader/writer only once
    final BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    final PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

    //new thread just to read from socket
    new Thread(){
        public void run() {
            String line;
            try {
                while ((line = in.readLine()) != null) {
                    System.out.println(line);
                }

                //remote has closed socket. quit
                System.exit(0); 
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }.start();

    //get keyboard input and send
    while (true) {
        String line = kb.nextLine();
        if (line.equalsIgnoreCase("exit"))
            System.exit(0); //user has typed in "exit". quit

        //send line to remote
        out.println(line);
    }

}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download