- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要进行“静态”聊天,当我的客户端说“PAPO”时,我的服务器需要打印 PAPO 并将 PEPO 发送到客户端打印。但我在服务器上的 readLine() 处遇到问题,只需在此行停止即可。
import java.net.*;
import java.io.*;
public class Servidor {
public static void main(String[] args) {
try {
ServerSocket server = new ServerSocket(6543);
do {
Socket s = server.accept();
System.out.println("Servidor escutando...");
BufferedReader entrada = new BufferedReader(
new InputStreamReader(s.getInputStream()));
PrintWriter saida = new PrintWriter(s.getOutputStream());
System.out.println(entrada.readLine());
saida.write("PEPO");
System.out.flush();
entrada.close();
saida.close();
s.close();
} while (true);
} catch (UnknownHostException ex) {
System.out.println("Host desconhecido");
} catch (IOException ex) {
System.out.println("Erro na conexao: " + ex.getMessage());
}
}
}
客户:
import java.net.*;
import java.io.*;
public class Cliente {
public static void main(String[] args) {
try {
Socket s = new Socket("localhost", 6543);
do {
BufferedReader entrada = new BufferedReader(
new InputStreamReader(s.getInputStream()));
PrintWriter saida = new PrintWriter(s.getOutputStream());
saida.write("PAPO");
System.out.println(entrada.readLine());
entrada.close();
saida.close();
s.close();
} while (true);
} catch (UnknownHostException ex) {
System.out.println("Host desconhecido");
} catch (IOException ex) {
System.out.println("Erro na conexao: " + ex.getMessage());
}
}
}
最佳答案
看看您从客户那里写的内容:
saida.write("PAPO");
没有换行符,因此服务器不知道同一行中是否有更多文本。另外,因为您还没有刷新写入器,所以可能实际上没有发送任何数据。如果你只是将其更改为:
saida.write("PAPO\n");
saida.flush();
我怀疑你会发现它有效。
但是,我强烈建议您在使用 InputStreamReader
和 OutputStreamWriter
时指定编码,而不仅仅是使用平台默认值。如果您控制两端,UTF-8 通常是一个不错的选择。
关于java - 对 readline socket java 感到震惊,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22921402/
本文,我们将一起学习,使用纯 CSS,实现如下所示的动画效果: 上面的动画效果,非常有意思,核心有两点: 小球随机做 X、Y 方向的直线运动,并且能够实现碰撞到边界的时候,
作者:京东零售 付伟 1. 前言 大家好,当你点开这篇文章的时候也许心想是哪个 XX 小编混到这里,先不要着急扔臭鸡蛋,本文是一篇标准(正经)的问题复盘文章。好了,一行MD5居然让小伙伴
好吧,我也做了回标题党,像我这么细心的同学,怎么可能让服务器被入侵呢? 其实是这样的,昨天我和一个朋友聊天,他说他自己有一台云服务器运行了 Redis 数据库,有一天突然发现数据库里的数据全没了,
theme: fancy highlight: arta
我是一名优秀的程序员,十分优秀!