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



我是android、java和socket编程的新手,所以我当然会尝试合并这三种编程!

我正在创建一个基于桌面的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());
        }
    }
}

提前感谢!

Josh

除了我的评论中提到的所有问题外,您正在客户端中创建两个套接字,而您的服务器只用于处理一个连接。所以它会写信给第一个,并试图从中阅读,而你的客户正在写信给第二个,并尝试从中阅读。

当你解决了这个问题,你就会陷入僵局,因为双方都在努力相互解读。

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

试着在服务器消息的末尾放一些东西,比如ETX或<end>,或者用<msg>...</msg>包装消息。

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

最新更新