1

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

提前致谢!

乔什

4

2 回答 2

0

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

当你解决这个问题时,你会遇到死锁,因为双方都试图互相阅读。

此外,您不应该使用PrintWriterPrintStream通过网络,因为它们会吞下您需要了解的异常。使用BufferedWriter.

于 2013-05-15T02:29:33.333 回答
0

尝试在服务器消息的末尾添加一些东西,例如ETX或类似的东西,<end>或者将消息包装在<msg>...</msg>.

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

于 2015-08-11T11:43:07.973 回答