2013-05-15 85 views
1

我是新来的android,java和套接字编程,所以当然我试图合并所有三个!套接字超时问题:Android客户端从PC服务器读取数据

我正在创建一个基于桌面的java服务器,它将简单地发送一个短的字符串到android应用程序(客户端)。

我有Android应用程序运行良好,它发送字符串到服务器读取没有问题。

我有服务器运行,它收到的字符串没有问题。

但是,每当我尝试读取从服务器返回的字符串时,应用程序(客户端)都会有套接字超时。我使用相同的代码,所以我不明白这个问题。

反正这里的代码:

//SERVER// 

import java.io.*; 
import java.net.*; 
import java.util.*; 

public class GpsServer { 

    ServerSocket serversocket = null; 
    Socket socket = null; 

    public GpsServer() 
    { 
     try 
     { 
      serversocket = new ServerSocket(8189); 
     } 
     catch (UnknownHostException unhe) 
     { 
      System.out.println("UnknownHostException: " + unhe.getMessage()); 
     } 
     catch (InterruptedIOException intioe) 
     { 
      System.out.println("Timeout while attempting to establish socket connection."); 
     } catch (IOException ioe) 
     { 
      System.out.println("IOException: " + ioe.getMessage()); 
     } 
    } 

    public void refreshServer() { 

     try 
     { 
      socket = serversocket.accept(); 

      InputStreamReader inputstreamreader = new InputStreamReader(socket.getInputStream()); 
      BufferedReader bufferedreader = new BufferedReader(inputstreamreader); 
      PrintWriter printwriter = new PrintWriter(socket.getOutputStream(),true); 

      System.out.println("socket read successful"); 
      printwriter.println("Send Bye to disconnect."); 

      String lineread = ""; 
       boolean done = false; 
       while (((lineread = bufferedreader.readLine()) != null) && (!done)){ 
       System.out.println("Received from Client: " + lineread); 
       printwriter.println("You sent: " + lineread); 
       if (lineread.compareToIgnoreCase("Bye") == 0) done = true; 
       } 

      System.out.println("Closing connection"); 

      socket.close(); 
      bufferedreader.close(); 
      inputstreamreader.close(); 
      printwriter.close(); 
     } 
     catch (UnknownHostException unhe) 
     { 
      System.out.println("UnknownHostException: " + unhe.getMessage()); 
     } 
     catch (InterruptedIOException intioe) 
     { 
      System.out 
        .println("Timeout while attempting to establish socket connection."); 
     } 
     catch (IOException ioe) 
     { 
      System.out.println("IOException: " + ioe.getMessage()); 
     } 
    } 

    public void nullify() {  
     try 
     { 
      socket.close(); 
      serversocket.close(); 
     } 
     catch (IOException ioe) 
     { 
      System.out.println("IOException: " + ioe.getMessage()); 
     } 
    } 
} 

和客户端...提前

//CLIENT 

import java.io.*; 
import java.net.*; 
import java.util.*; 

public class GpsClient { 

    public String lastMessage; 
    Socket socket = null; 

    String serverurl; 
    int serverport; 

    public GpsClient() { 
     lastMessage = ""; 

     serverport = 8189; 
     serverurl = "192.168.10.4"; 

     try 
     { 
      socket = new Socket(serverurl, serverport); 
      socket.setSoTimeout(10000); 
     } 
     catch (UnknownHostException unhe) 
     { 
      System.out.println("UnknownHostException: " + unhe.getMessage()); 
     } 
     catch (InterruptedIOException intioe) 
     { 
      System.out.println("Timeout while attempting to establish socket connection."); 
     } 
     catch (IOException ioe) 
     { 
      System.out.println("IOException: " + ioe.getMessage()); 
     } 
    } 

    public void retrieveNew() { 

     try { 
      socket = new Socket(serverurl, serverport); 
      socket.setSoTimeout(10000); 

      lastMessage = "connected!"; 

      InputStreamReader inputstreamreader = new InputStreamReader(socket.getInputStream()); 
      BufferedReader bufferedreader = new BufferedReader(inputstreamreader); 

      PrintWriter printwriter = new PrintWriter(socket.getOutputStream(),true); 
      printwriter.println("Request"); 

      lastMessage = "Request sent"; 

//   Get error when I uncomment this block, i.e. try to read the response from the server 
//   "Timeout while attempting to establish socket connection." 

//   String lineread = ""; 
//   while ((lineread = bufferedreader.readLine()) != null) { 
//    System.out.println("Received from Server: " + lineread); 
//    lastMessage = "Received from Server: " + lineread; 
//   } 

      lastMessage = "closing connection!"; 
      bufferedreader.close(); 
      inputstreamreader.close(); 
      printwriter.close(); 
      socket.close(); 

     } 
     catch (UnknownHostException unhe) 
     { 
      System.out.println("UnknownHostException: " + unhe.getMessage()); 
     } 
     catch (InterruptedIOException intioe) 
     { 
      System.out.println("Timeout while attempting to establish socket connection."); 
     } 
     catch (IOException ioe) 
     { 
      System.out.println("IOException: " + ioe.getMessage()); 
     } 
     finally 
     { 
      try 
      { 
       socket.close(); 
      } 
      catch (IOException ioe) 
      { 
       System.out.println("IOException: " + ioe.getMessage()); 
      } 
     } 
    } 

    public void nullify() { 
     try 
     { 
      PrintWriter printwriter = new PrintWriter(socket.getOutputStream(),true); 
      printwriter.println("Bye"); 
      printwriter.close(); 
      socket.close(); 
     } 
     catch (IOException ioe) 
     { 
      System.out.println("IOException: " + ioe.getMessage()); 
     } 
    } 
} 

谢谢!

乔希

+1

您的信息都是错误的。所有消息“尝试建立套接字连接时超时”都不正确。第一个甚至不可能发生;第二个是接受或从连接读取的超时;第三个是读取超时。一般来说,你不应该用你自己编写的文本来替代异常消息。事实上,你应该捕获'SocketTimeoutException,''不是'InterruptedIOException.'还要在完成任何读取之前打印'套接字读取成功'。尝试清理所有这些并重新测试。目前你的程序只是对你说谎。 – EJP

回答

0

除了在我的评论指出的所有问题,要创建在客户端两个插座,你的服务器只写入处理一个连接。所以它写到第一个,并试图读取它,同时你的客户正在写第二个,并试图从中读取。

当您修复后,您将遇到死锁,因为双方都试图从对方读取。

另外,您不应该在网络上使用PrintWriterPrintStream,因为它们吞噬您需要了解的异常。使用BufferedWriter

+0

就像我说的,我对套接字编程非常陌生。大部分代码都是我在网上找到的一段代码,没有一个能够工作。你听起来像你知道你的东西EJP,你介意给我一个非常基本的套接字代码工作位的例子,我可以建立,因为到目前为止谷歌并不是我的朋友:(PS我清理了catch例外和使用像你说的缓冲写作者。仍然没有骰子:( – Joewsh

0

尝试在服务器消息的末尾添加一些内容,如ETX或类似<end>的内容或将消息包装在<msg>...</msg>中。

然后在Android的while循环中检查是否收到它而不是检查(lineread = bufferedreader.readLine()) != null

相关问题