程序只发送一次消息

时间:2015-12-28 05:00:21

标签: java sockets

我一直在研究这个脚本一段时间,并且遇到了这个我无法工作的问题。基本上我开始循环并连接到套接字。连接后,我输入的每个字符串都应该发送到我设置的ip / port。但是,它适用于我发送的第一个字符串,但之后它将不再发送我输入的字符串。

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

}
}

此程序仅发送一次输入的消息。此程序的目的是让某人放入他们想要与之通信的人的IP和端口。

1 个答案:

答案 0 :(得分:0)

我汇总了你要做的事情的一个例子。要点是,不需要围绕现有的Stream创建新的流。

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

}
相关问题