在Windows / Linux的问题与Applet的客户机/服务器的Socket [英] Windows/Linux issue with Client/Server Socket with Applet

查看:209
本文介绍了在Windows / Linux的问题与Applet的客户机/服务器的Socket的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在socket编程和线程新。我会很高兴,如果有人能帮助我。
我目前工作的一个多客户端服务器的问题,在每一个新的客户端连接都有自己的线程和它的小程序。这里是当它断开时,关闭客户端的线程code片段。

I'm new at socket programming and threads. I'd be happy if anyone can help me out. I currently working on a multi-client server problem where each new client connection gets its own thread and its an applet. here is a code snippet of when to close the thread of a client when it disconnects.

   String inputMessage; //message stored here
   BufferedReader in = new BufferedReader(
                new InputStreamReader(socket.getInputStream()));
   while((inputMessage = in.readLine()) != null){
            //Update message buffer with message that client has typed 
            buffer.insertMessage(inputMessage);

    }
    // Close things
        in.close();
        socket.close();

所以,当一个空从BufferedReader中读取,就会退出while循环。
我的问题是这样的完美的作品在linux。当X是在applet的角落pressed时,得到的BufferedReader空和线程终止优雅。

So when a null is read from the BufferedReader, it exits the while loop. My issue is this works perfectly in linux. When x is pressed in the corner of the applet, the bufferedReader gets a null and the thread terminates gracefully.

当我尝试这样做在Windows中,我接收到一个SocketException:连接重置

When I tried this in windows, I get a SocketException: Connection reset

    java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(Unknown Source)
    at java.net.SocketInputStream.read(Unknown Source)
    at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
    at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
    at sun.nio.cs.StreamDecoder.read(Unknown Source)
    at java.io.InputStreamReader.read(Unknown Source)
    at java.io.BufferedReader.fill(Unknown Source)
    at java.io.BufferedReader.readLine(Unknown Source)
    at java.io.BufferedReader.readLine(Unknown Source)

请问当小程序被关闭Windows和Linux做不同的事情或者是我的code

Does windows and linux do something different when the applet is closed or is it my code

推荐答案

扫描仪尝试和检查 hasNextLine()获得前 nextLine()

样code:

Scanner scanner = new Scanner(new InputStreamReader(socket.getInputStream()));
while (scanner.hasNextLine()) {
    System.out.println(scanner.nextLine());
}

这篇关于在Windows / Linux的问题与Applet的客户机/服务器的Socket的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆