有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java程序只发送一次消息

我已经在这个脚本上工作了一段时间,并且遇到了这个问题,我还没有开始工作。基本上,我启动一个循环并连接到一个插座。一旦连接,我输入的每个字符串都应该发送到我设置的ip/端口。然而,它对于我发送的第一个字符串来说是完美的,但是在那之后,它将不再发送我输入的字符串

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) 个答案

  1. # 1 楼答案

    我给你举了一个例子,说明你想做什么。要点是,无需围绕现有流创建新流

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