Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 程序只发送一次消息_Java_Sockets - Fatal编程技术网

Java 程序只发送一次消息

Java 程序只发送一次消息,java,sockets,Java,Sockets,我已经在这个脚本上工作了一段时间,并且遇到了这个问题,我还没有开始工作。基本上,我启动一个循环并连接到一个插座。一旦连接,我输入的每个字符串都应该发送到我设置的ip/端口。然而,它对于我发送的第一个字符串来说是完美的,但是在那之后,它将不再发送我输入的字符串 package keylog; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import jav

我已经在这个脚本上工作了一段时间,并且遇到了这个问题,我还没有开始工作。基本上,我启动一个循环并连接到一个插座。一旦连接,我输入的每个字符串都应该发送到我设置的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和端口

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

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

}

为什么每次都要创建一个新的BufferedReader和PrintWriter?当will
BufferedReader==“quit”
时,这没有任何意义。@WalterM因为程序没有完成,所以我把它用作while循环。我一次只关注一个问题:)。不过我把它改成了whileone循环。IP和端口的输入值是多少?@Sabir Khan这些值是:127.0.0.1(字符串)和5056(int)。。。它在第一次工作时确实有效,但在那之后什么也没有:(@Sabir Khan 55056 my bad我来自哪里,我们称之为spoonfeeding@VinceEmigh有点,但这基本上是他已经在做的事情,只是顺序不对。最好是指导这个人,而不是给他们一些他们可以复制和粘贴到IDE中的东西。特别是因为提问者是新来的,这可能会让他对这个网站如何Works。他可以告诉他的朋友StackOverflow会给他们发代码,而这个网站已经有足够多的人了。答案是复制粘贴内容,而没有详细解释问题所在,这往往会伤害用户community@Vince也许吧,不过你来阻止我保留这个想法是很方便的s给了我一种更有效的编码方式。我不会简单地复制粘贴。@Sellex没有人阻止任何人。他的答案仍然有效,没有人投反对票,你得到了你需要的答案。这只是一个建议。更好的答案往往会吸引更多的赞成票