简体   繁体   English

程序仅发送一次消息

[英]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. 连接后,我输入的每个字符串都应发送到我设置的ip /端口。 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. 该程序的目的是让某人输入要与之通信的人的ip和端口。

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);
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM