套接字超时问题:android客户端从pc服务器读取数据

时间:2013-05-15 01:22:50

标签: java android sockets

我是android,java和socket编程的新手,所以当然我正试图合并这三个!

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

我的Android应用程序正常运行,它将字符串发送到服务器,这些字符串被读取没有任何问题。

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

每当我尝试读取从服务器返回的字符串时,app(客户端)都有一个套接字超时。我使用相同的代码,所以我无法理解这个问题。

无论如何这里是代码:

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

提前致谢!

约什

2 个答案:

答案 0 :(得分:0)

除了我的评论中提到的所有问题之外,您在客户端中创建了两个套接字,并且您的服务器仅编写为处理一个连接。所以它写入第一个并尝试从中读取,同时您的客户端正在写入第二个并尝试从中读取。

当你解决问题时,你会遇到僵局,因为双方都试图互相阅读。

此外,您不应在网络上使用PrintWriterPrintStream,因为它们会吞下您需要了解的例外情况。使用BufferedWriter

答案 1 :(得分:0)

尝试在服务器消息的末尾放置一些内容,例如ETX<end>之类的内容,或者将消息包装在<msg>...</msg>中。

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