【问题标题】:Client(Java) not connecting to server(Kotlin) Android studio, what is wrong?客户端(Java)未连接到服务器(Kotlin)Android Studio,出了什么问题?
【发布时间】:2020-09-30 11:08:30
【问题描述】:

客户端代码可以 100% 工作,因为我使用它连接到其他人制作的服务器。现在我尝试制作自己的服务器。有谁知道为什么这不起作用?我在服务器应用程序中没有收到任何错误,但它永远不会进入第二个线程。

客户端代码:

public void connectToServer(){ (used in some activity)
    new AsyncTask<Void, Void, Void>() {
        @Override
        protected Void doInBackground(Void... voids) {
            try {
                socket = new Socket("192.168.0.1", 9999);
                send = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
                get = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            }catch (Exception ex){
                Log.e("connection", ex.toString());
            }
            return null;
        }
    }.execute();
}

服务器代码:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        findViewById<Button>(R.id.buttonStartServer).setOnClickListener {
            Log.e("Button", "Apasat")
            val server = ServerSocket(9999, 10, InetAddress.getByName("0.0.0.0"))
            thread {
                run {
                    Log.e("first run check server", server.inetAddress.hostAddress)
                    Log.e("first run", "waiting for clients")
                    while (true) {
                        val client = server.accept()

                        Log.e("first run", "got a client")
                        thread {
                            run {
                                Log.e("second run", "waiting for messages")
                                val reader = Scanner(client.getInputStream())
                                val writer: OutputStream = client.getOutputStream()
                                while(true) {
                                    try {
                                        val text = reader.nextLine()
                                        writer.write((text + '\n').toByteArray(Charset.defaultCharset()))
                                    }catch (ex: Exception){
                                        Log.e("client" + client.inetAddress.hostAddress, ex.toString())
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

【问题讨论】:

  • 您能说出您的客户端在哪种设备上运行吗?服务器呢?
  • 192.168.0.1 是本地 IP 地址,因此除非您的服务器实际上运行在尝试连接到它的同一 Android 设备上,否则这种方法将不起作用。
  • 我在 2 个不同的 android 设备模拟器中同时运行
  • 抛出什么异常?
  • 运行客户端代码时,它只是在运行一段时间后显示“econnrefused(连接被拒绝)”,但我发现它需要做些什么才能连接。

标签: java android sockets kotlin serversocket


【解决方案1】:

代码运行良好,但因为我的服务器也在 android 上运行,所以我需要设置服务器设备模拟器,如下所示: https://developer.android.com/studio/run/emulator-networking#connecting

有人问如何在这里设置: Communication between two Android emulators 他解释了步骤。

【讨论】:

    猜你喜欢
    • 2020-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-19
    • 1970-01-01
    • 2011-09-21
    相关资源
    最近更新 更多