I would try my best to explain everything.

**DESCRIPTION:** working on an android app written in kotlin that behaves as a server side and Python program that works as a client both runs on same computer and try to send and receive messages from each other.I'm using a text view to display messages that i'm receiving from PC(python).

**Problem:** whenever i try to run client program either it gives me timeout error if i use **client\_socket.connect(('10.0.2.2',5000))** or it gives me **error:61 connection refused**
if i try to connect it with ip address **client\_socket.connect(('127.0.0.1',5000))**

**What i have Tried:** I have written both the programs written below but don't know whats wrong whether its IP address that i'm using wrong or something else.And also i have used IP address for emulator 10.0.2.15 but nothing worked.

**server**

```
package com.example.soundsource

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.widget.Button
import android.widget.TextView
import java.net.ServerSocket
import java.net.Socket
import java.io.*

class MainActivity : AppCompatActivity() {

    private lateinit var textView:TextView

    companion object{
        const val COMMUNICATIONPORT = 6000
        private lateinit var serversocket:ServerSocket
        private lateinit var serverThread:Thread
        private lateinit var updateConversationHandler:Handler
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val sendButton:Button = findViewById(R.id.send_button)
        val showLocation = findViewById(R.id.show_location) as? Button
        showLocation?.setOnClickListener {
            val intent = Intent(this,SoundLocation::class.java)
            startActivity(intent)
        }
        textView = findViewById(R.id.text_view)
        sendButton.setOnClickListener{
            serverThread = Thread(ServerThread())
            serverThread.start()
        }
    }

    class ServerThread:Runnable{
        override fun run() {
            var socket: Socket
            try {
                serversocket = ServerSocket(COMMUNICATIONPORT)

            } catch (e:IOException) {
                e.printStackTrace()
            }

            while (!Thread.currentThread().isInterrupted) {

                try {

                    socket = serversocket.accept()

                    val commThread = CommunicationThread(socket)
                    Thread(commThread).start()

                } catch (e: IOException) {
                    e.printStackTrace()
                }

            }
        }
    }

    class CommunicationThread(private val clientSocket: Socket) : Runnable {

        private var input: BufferedReader? = null
        private var output:PrintWriter? = null

        init {

            try {

                this.input = BufferedReader(InputStreamReader(this.clientSocket.getInputStream()))
                this.output = PrintWriter(clientSocket.getOutputStream())

            } catch (e: IOException) {
                e.printStackTrace()
            }

        }

        override fun run() {

            while (!Thread.currentThread().isInterrupted) {

                try {
                    output!!.write("Thanks for connecting with me.....")
                    val read = input!!.readLine()

                    updateConversationHandler.post(MainActivity().UpdateUIThread(read))

                } catch (e: IOException) {
                    e.printStackTrace()
                }

            }
        }

    }

    internal inner class UpdateUIThread(private val msg: String) : Runnable {

        override fun run() {
            val message =  "Client Says: $msg \n"
            textView.text = message
        }
    }

}

```

**client**

```
import socket

def main():
    client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    print("creating client socket.....")
    client_socket.connect(('10.0.2.2', 6000))
    print("successfully connected.....")
    while True:
        data = client_socket.recv(1024).decode()
        print("server sent something.....\n", data)
        print("you are about to.....")
        client_socket.send("hey server....".encode())

main()

```