java socket asc,TCP套接字上的Ascii

这是一个关于如何通过TCP进行ASCII消息传递的示例代码。程序首先创建一个Socket连接到主机'taranis'的7号端口,然后使用PrintWriter发送数据,并通过BufferedReader读取回应。它持续从标准输入读取用户输入,将其发送到服务器并打印出服务器的回显。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Anyone could pass me an example of sending Ascii msg over TCP?(couldnt find example on the net)

thanks,

ray.

解决方案

Here's an example of writing to and reading from an echoing server.

A simplified excerpt:

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());

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值