Java 聊天客户端传输时丢失的文本
我有一个Java客户机/服务器聊天应用程序,在建立连接后,收件人只收到大约四分之一的数据。有什么问题吗?下面是一个打印屏幕,显示了具体发生的情况: 从套接字读取的代码:Java 聊天客户端传输时丢失的文本,java,sockets,Java,Sockets,我有一个Java客户机/服务器聊天应用程序,在建立连接后,收件人只收到大约四分之一的数据。有什么问题吗?下面是一个打印屏幕,显示了具体发生的情况: 从套接字读取的代码: public void somethingElse(){ try { if(in.readLine() != null){ messageBufferIn = in.readLine(); System.out.println(in.r
public void somethingElse(){
try {
if(in.readLine() != null){
messageBufferIn = in.readLine();
System.out.println(in.readLine());
chat.append(recipient + ": " + messageBufferIn + "\n");
}
} catch (IOException e) {
e.printStackTrace();
}
}
运行上述方法的线程的代码:
public class chatListener extends Thread{
static main main = new main();
//static Thread mainThread = new Thread(main);
public void run(){
while(main.isConnected == true){
main.somethingElse();
}
}
}
一旦建立连接,上述线程就会运行
感谢您的帮助每次您打电话进来。readLine,扫描仪会向下移动到下一行;你不能一直叫它几次,因为它会跳过你根本没用过的那几行。尝试此操作以替换某些内容()
之前,您曾调用过一次in.readLine来检查它是否为null,然后保存了下一行,然后打印了下一行。因此(失败-成功-失败|失败-成功-失败等)=只有消息2+5显示效果非常好!我不敢相信我的错误如此简单!也谢谢你帮我理解我的错误!是的!很高兴能帮忙!:)
public void somethingElse(){
try {
String line;//Added a variable to store the current line to; readLine is
//dynamic, it returns the next line each call, so if we store to a variable,
//we only call it once, and hold that value
if((line = in.readLine()) != null){// (line = in.readLine()) != null is shorthand to store readLine to line, and then check if that returned value is null or not
System.out.println(line);//Print out the line
chat.append(recipient + ": " + line + "\n");//Append it
}
} catch (IOException e) {
e.printStackTrace();
}
}