无法正确连接到telnet套接字

时间:2016-05-17 02:32:01

标签: java telnet

我正在尝试连接到telnet服务器,但是当我连接到运行在端口9999上的telnet服务器时,但是当我运行此代码时。它显示欢迎信息,但没有命令有效。

public static void main(String[] args) {
    FileOutputStream fos = null;
    final String server = "erdos.dsm.fordham.edu";
    final int port = 9999;

    Socket sock = null;
     PrintWriter out = null;
    BufferedReader in = null;
    try {
        sock = new Socket(server,port);
        sock.setKeepAlive(true);

        out = new PrintWriter(sock.getOutputStream(),true);
        in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
    } catch (IOException ex) {
        Logger.getLogger(Hangman.class.getName()).log(Level.SEVERE, null, ex);
    }
    try {

        System.out.println(in.readLine());

        Scanner scanner = new Scanner(System.in);
        String s =  scanner.nextLine();
        s = s.toUpperCase();
    while(!s.equals("QUIT"))
    {

        out.print(s);
        out.flush();
        System.out.println(in.read());
         s =  scanner.nextLine();
    }   
        System.out.println("Thanks for playing bye");

    } catch (IOException ex) {
        Logger.getLogger(Hangman.class.getName()).log(Level.SEVERE,null, ex);
    }
}

1 个答案:

答案 0 :(得分:0)

已编辑:请参阅先前版本的第一个版本

令人毛骨悚然,但它确实有效。因为在命令之后我无法找到传入数据的终端,所以我决定添加一个Runnable + Thread读取,并在其中添加一个方法,通过等待100ms(或更多,如果需要)读取在考虑收到整个数据之前。

package base_test;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        FileOutputStream fos = null;
        final String server = "erdos.dsm.fordham.edu";
        final int port = 9999;

        Socket sock = null;
        PrintWriter out = null;
        BufferedReader in = null;
        try {
            sock = new Socket(server, port);
            sock.setKeepAlive(true);

            out = new PrintWriter(sock.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
        } catch (IOException ex) {
            ex.printStackTrace();
            // Logger.getLogger(Hangman.class.getName()).log(Level.SEVERE, null,
            // ex);
        }

        //Start a different that listen to incoming data
        DataReader dr = new DataReader(in);
        Thread dataThread = new Thread(dr);
        dataThread.start();


        try {

            Scanner scanner = new Scanner(System.in);
            String s = "";
            do {
                System.out.println(dr.getLastData());

                s = scanner.nextLine();
                s = s.toUpperCase();
                if (s.isEmpty())
                    continue;

                out.println(s);
            } while (!s.equals("QUIT"));
            System.out.println("Thanks for playing bye");

        } catch (Exception ex) {
            ex.printStackTrace();
            // Logger.getLogger(Hangman.class.getName()).log(Level.SEVERE,null,
            // ex);
        }
    }
}

DataReader类:

package base_test;

import java.io.BufferedReader;
import java.util.LinkedList;

public class DataReader implements Runnable {

    private final int MAX_WAITING_LOOPS = 100;

    private LinkedList<StringBuilder> receivedData = new LinkedList<>();
    private BufferedReader in;
    private boolean isWaiting = true;
    private boolean isReceiving = false;

    public DataReader(BufferedReader in) {
        this.in = in;
        receivedData.push(new StringBuilder());
    }

    @Override
    public void run() {
        do {
            try {
                isWaiting = true;

                char last = (char)in.read();
                StringBuilder sb = receivedData.peek();
                sb.append(last);

                isWaiting = false;
                isReceiving = true;
            } catch(Exception e) {
                e.printStackTrace();
            }

        } while (true);
    }

    public String getLastData() throws InterruptedException {
        int loopCount = 0;

        while (loopCount < MAX_WAITING_LOOPS) {
            if (isReceiving == false || isWaiting == false) {
                //System.out.println("===RESET");
                loopCount = 0;
            } else {
                loopCount++;
            }
            Thread.sleep(1);
        }

        isReceiving = false;

        receivedData.push(new StringBuilder());
        StringBuilder sb = receivedData.pollLast();
        return sb.toString();
    }
}