如何通过 telnet 或任何其他程序将字符串数据发送到套接字连接?

How to send string data to socket connection via telnet or any other program?

我正在尝试通过 telnet 向套接字连接发送和接收字符串数据,但我无法在 telnet 中键入或查看任何内容 window。我可以通过 telnet 连接到服务器,但无法发送字符串数据。

是否有任何其他替代方法通过套接字连接发送字符串数据。

Telnet,除非它协商相反的参数,否则 "remote echo" 意味着您不会看到您键入的任何内容,除非服务器回显它。

很多人使用术语 "Telnet",但实际上它是一个原始套接字连接,在连接时不进行配置协商。

如果您从键盘以外的文件或来源发送数据(甚至经常从键盘发送),您最好使用 socket 或 [=11= 这样的程序] (netcat) 不尝试对数据流进行任何处理,因此提供简单的 8 位干净连接。

对于这两个问题,您可以简单地从文件重定向 stdin 或通过管道将字符串回显给它们。

我有一个与许多 telnet 客户端通信的服务器示例。

您必须使用 class DataInputStreamDataOutputStream 您必须使用 A Class Implements Runnable 来建立多个会话 您必须使用 ServerSocket Class.

很好,这是调用 SocketServerExample class 的主要代码:

import java.net.*;
import java.io.*;
import socketserverexample.ThreadServer;

 /**
 *
 * @author JuanLuisHiciano
 */
public class SocketServerExample {

public static void main(String args[]) throws InterruptedException {

    ServerSocket mi_servicio = null;
    String linea_recibida;
    DataInputStream entrada = null;
    DataOutputStream salida = null;
    Socket socket_conectado = null;

    try {
        mi_servicio = new ServerSocket(2017);
    }
    catch (IOException excepcion) {
        System.out.println(excepcion);
    }
    try {

        int n=1;
        while(n<2){
            socket_conectado = mi_servicio.accept();
            System.out.println("Un cliente se a conectado "+socket_conectado.getPort());
            entrada= new DataInputStream(socket_conectado.getInputStream());
            String nombre = entrada.readUTF();
            // Se instancia una clase para atender al cliente y se lanza en
            // un hilo aparte.
            Runnable nuevoCliente = new ThreadServer(nombre, socket_conectado); //Input and Output data Channels 

            Thread hilo = new Thread(nuevoCliente);
            hilo.start();      

        }

        salida.writeUTF("Fin de la conexion....");


        salida.close();
        entrada.close();
        socket_conectado.close();
    }
    catch (IOException excepcion) {
        System.out.println(excepcion);
    }
}

}

好的,这个 运行 带有 UTP 端口 (2017) 的主服务器并将会话传递给其他线程以接收新连接。

很好,下面是 class 调用的 ThreadServer 的代码:

import java.net.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author JuanLuisHiciano
 */
 public class ThreadServer implements Runnable{

DataInputStream entrada;
DataOutputStream salida;
Socket socket_conectado = null;
String linea_recibida;
String cliente;

 ThreadServer(String cliente,Socket socket) {
    socket_conectado = socket;
    this.cliente=cliente;

}





@Override
public void run() {
    int n=0;
   while(n<3){
       try {
            salida = new DataOutputStream(socket_conectado.getOutputStream());
            entrada = new DataInputStream(socket_conectado.getInputStream());
            //System.out.println("Confirmando Conexion al cliente .....");
            salida.writeUTF("Conexion Exitosa\n");
            salida.writeUTF("Puede compartir un mensaje : ");
            //recepcion de mensaje
            linea_recibida = entrada.readUTF();
            System.out.println(cliente+" dice: "+linea_recibida); 
            System.out.println(socket_conectado.getPort());
            n++;
       } catch (IOException ex) {
           Logger.getLogger(ThreadServer.class.getName()).log(Level.SEVERE, null, ex);
       }


   }
}

}

使用此代码,您可以与每个连接的客户端交谈。

再见,多米尼加共和共和国

我希望你能为这段代码服务。