Client

mail@pastecode.io avatar
unknown
kotlin
2 years ago
892 B
6
Indexable
Never
fun startAsClient(serverIP: String,serverPort: Int = SERVERPORT) {
        val settings = applicationContext.getSharedPreferences(PREFSNAME, 0)
        val username = settings.getString("username", " ")

        thread {
            try {
                val newsocket = Socket()
                newsocket.connect(InetSocketAddress(serverIP,serverPort),5000)
                Log.i("DEBUG", "Connected")
                var reader = BufferedReader(InputStreamReader(newsocket.getInputStream()))
                val writer = PrintWriter(newsocket.getOutputStream())

                val data = JSONObject()
                data.put("Username", username)
                writer.write(data.toString())
                Log.i("DEBUG", "Sent data")
                writer.flush()

            } catch (_: Exception) {
                return@thread
            }
        }
    }