Oi pessoal. Boa noite. Estou tentando me conectar ao um servidor telnet (telehack.com: 23) para enviar e receber informações, porém, não estou conseguindo. Até o momento, estou apenas recebendo informações na primeira conexão, mas quando envio um comando não estou recebendo. Alguém poderia me ajudar? Abaixo o código:
import java.net.Socket;
import java.util.Scanner;
public class AbrirConexao {
public AbrirConexao() throws Exception {
System.out.println("Informe o ip");
Scanner ip = new Scanner(System.in);
String AddressIp = ip.nextLine();
System.out.println("Informe a porta");
Scanner porta = new Scanner(System.in);
int protocoloPorta = porta.nextInt();
Socket socket = new Socket(AddressIp, protocoloPorta);
System.out.println("Conectado..");
Thread enviar = new Thread(new EnviarDados(socket));
Thread recebe = new Thread(new ReceberDados(socket));
enviar.start();
recebe.start();
enviar.join();
ip.close();
porta.close();
System.out.println("fim");
socket.close();
System.out.println("fim");
}
}
import java.net.Socket;
import java.util.Scanner;
public class ReceberDados implements Runnable {
private Socket socket;
public ReceberDados(Socket socket) {
this.socket = socket;
}
public void recebe() throws Exception {
System.out.println("recebendo");
Scanner recebe = new Scanner(socket.getInputStream());
while (recebe.hasNextLine()) {
String linha = recebe.nextLine();
System.out.println(linha);
}
recebe.close();
System.out.println("fim");
}
@Override
public void run() {
try {
recebe();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;
public class EnviarDados implements Runnable {
private Socket socket;
public EnviarDados(Socket socket) {
this.socket = socket;
}
public void enviar() throws Exception {
PrintStream enviar = new PrintStream(socket.getOutputStream());
Scanner teclado = new Scanner(System.in);
while (teclado.hasNextLine()) {
String linha = teclado.nextLine();
enviar.println(linha);
enviar.flush();
System.out.println("enviado\n");
}
teclado.close();
}
@Override
public void run() {
try {
enviar();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
public class TesteConexao {
public static void main(String[] args) throws Exception {
new AbrirConexao();
}
}