TCP 套接字上的 Ascii

2023-12-12

任何人都可以给我一个通过 TCP 发送 Ascii 消息的示例?(在网上找不到示例)

thanks,

ray.


这是一个例子写入和读取回显服务器。

简化摘录:

    Socket echoSocket = null;
    PrintWriter out = null;
    BufferedReader in = null;

    try {
        echoSocket = new Socket("taranis", 7);
        out = new PrintWriter(echoSocket.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader(
                                    echoSocket.getInputStream()));
    } catch (UnknownHostException e) {
        System.err.println("Don't know about host: taranis.");
        System.exit(1);
    } catch (IOException e) {
        System.err.println("Couldn't get I/O for "
                           + "the connection to: taranis.");
        System.exit(1);
    }

BufferedReader stdIn = new BufferedReader(
                               new InputStreamReader(System.in));
String userInput;

while ((userInput = stdIn.readLine()) != null) {
    out.println(userInput);
    System.out.println("echo: " + in.readLine());
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

TCP 套接字上的 Ascii 的相关文章

随机推荐