繁体   English   中英

Android套接字连接丢失?

[英]Android Socket Connection loss?

我在玩Sockets,编写了一个测试应用程序,可将文本消息发送到PC上运行的服务器,并让服务器显示这些消息。 我用Java编写了一个类似的客户端来对其进行测试,并且它可以正常工作。 但是现在在Android中,似乎可以建立与服务器的连接,但是它只能接收第一条消息,而后续消息将不会显示。 我以为,也许线程只执行一次,但事实并非如此,因为logcat可以识别按钮的按下。

Android的

public class MainActivity extends AppCompatActivity {

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

        final EditText enter = (EditText) findViewById(R.id.editText1);
        final Button button = (Button) findViewById(R.id.button);
        final String fromUser = null;
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                new Thread(new Runnable(){
                    @Override
                    public void run(){
                        PrintWriter out = null;
                        BufferedReader in = null;
                        Socket clientSocket = null;
                        try {
                            clientSocket = new Socket("192.168.178.41", 5959);
                            out = new PrintWriter(clientSocket.getOutputStream(), true);
                            in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
                        } catch (IOException e) {
                            e.printStackTrace();
                            Log.d("---","cant make a connection");
                        }
                        String fromUser = enter.getText().toString();
                        if (fromUser != null) {
                            System.out.println("Client: " + fromUser);
                            out.println(fromUser);
                        }
                    }

                }).start();
            }
        });
    }
}

服务器

public class Server {

public static void main(String[] args) {
        System.out.println("Starting server!");
        PrintWriter out = null;
        BufferedReader in = null;
        try{
             ServerSocket serverSocket = new ServerSocket(5959);
             Socket clientSocket = serverSocket.accept();
             out = new PrintWriter(clientSocket.getOutputStream(), true);
             in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
             System.out.println("Established a connection with a user!");
        } catch (IOException e) {
             e.printStackTrace();
        }
        String inputLine;
        String outputLine = "Hey Client from Server!";

        out.println(outputLine);

        try {
        while ((inputLine = in.readLine()) != null) {
            out.println(inputLine);
            System.out.println("Client: " + inputLine);
            if (inputLine.equals("quit"))
                break;
        }
        } catch (IOException e) {
        e.printStackTrace();
        }
    }   

}

我不知道是什么原因引起的,但是肯定是一个愚蠢的错误...

  1. 您的客户端正在建立一个连接,并且每次单击仅发送一条消息。
  2. 您的服务器仅接受一个连接。
  3. 您的客户端永远不会关闭套接字。 相反,它正在泄漏它。
  4. 因此,您的服务器永远不会脱离其读取循环。
  5. 即使这样做,也不会因为(2)而接受新客户。
  6. 由于(1),它在循环内将永远不会收到多条消息。

简而言之,您的代码毫无意义。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM