2

我有一个简单的连接活动:

package com.example.conn08;

import ...;

public class MainActivity extends Activity
{
    public static Socket clientSocket;
    public static DataOutputStream outToServer;
    public static PrintWriter outTest;
    public static BufferedReader inToServer;

使用 PrintWriter outTest 我测试服务器的可用性:如果用户没有 Internet,或者服务器不工作,我使用 Boolean shouldContinue 暂停 Thread。

    private Thread mThread;
    private final Object lock = new Object();
    private Boolean shouldContinue = true;


    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mThread = new Thread(new Runnable() 
        {
            public void run() 
            {
                while (true) 
                {
                    synchronized(lock)
                    {
                        try 
                        {
                            lock.wait(); // lock the Thread
                        } 
                        catch (InterruptedException e) 
                        {
                            e.printStackTrace();
                        }
                    }
                    while (shouldContinue) 
                    {
                        try 
                        {
                            final String data = inToServer.readLine();
                            if (data != null)
                            {
                                Log.v("data", data);
                                runOnUiThread(new Runnable()
                                {
                                    @Override
                                    public void run()
                                    {
                                        String put[] = data.split("#");
                                        //Data parsing
                                    }
                                });
                            }
                        } 
                        catch (IOException e)
                        {
                            e.printStackTrace();
                        }

检查服务器的可用性:

                        try {
                            if(clientSocket.getInputStream().read() == -1)
                            {
                                Log.v("Connection: ", "lost");
                            }
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
            }
        });
        mThread.start(); 

        Connect();
    }
    public void Connect()
    {
        shouldContinue = true;
        try
        {
            clientSocket = new Socket();
            clientSocket.connect(new InetSocketAddress("localhost", 15780), 30000);

            outToServer = new DataOutputStream(clientSocket.getOutputStream());
            inToServer = new BufferedReader(
            new InputStreamReader(clientSocket.getInputStream()));
            outTest = new PrintWriter(new BufferedWriter(
            new OutputStreamWriter(clientSocket.getOutputStream())), true);

            synchronized(lock) 
            {
                lock.notify();
            }

            sendUTF("3#kokoko"); //send the message!
        } 
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
    public static void sendUTF(String str)
    {
        try
        {
            byte[] buf = str.getBytes("UTF-8");
            outToServer.write(buf, 0, buf.length);
            outToServer.writeBytes("\n");
            outToServer.flush();
        } 
        catch (IOException e)
        {
            e.printStackTrace();
            outServ.setText("Нет соединения!");
        }
    }
}

问题在于,当我使用

if(clientSocket.getInputStream().read() == -1)

并将数据发送到服务器,如下所示:

sendUTF("3#kokoko");

一切都很好,但如果我使用它,在服务器上我会看到这样的消息“#kokoko” - 我丢失了消息的第一个字符并且我的套接字被压碎了!请帮帮我

4

0 回答 0