Uable正确连接到telnet套接字
问题描述:
我试图连接到telnet服务器,但是当我连接到运行在端口9999上的telnet服务器时,但是当我运行此代码时。它显示欢迎消息,但没有命令的作品。Uable正确连接到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);
}
}
答
编辑:请参阅第一个版本通过回答以前的版本
这是令人毛骨悚然,但很有效。因为在命令后我无法找到传入数据的终止符,所以我决定添加一个Runnable +线程,并读取其中的一个方法,并在考虑等待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();
}
}
由于它工作得很好,但不读例如整行,如果我键入帮它只是将返回第一线 –
呀。我知道,因为在第一行和其他行之间有一行跳过。你编码的服务器部分? –
Telnet中的行结束符是'\ r \ n',而不是'\ n'。你不是测试'readLine()'返回null,并且对空行的测试似乎毫无意义。 – EJP