Kotlin : client tcp

Hello,

I have a server and i would like send a message from my smartphone. this is my code :
class TCPClient() {

    var serverIP: String = "10.0.0.1"
    var serverPort: Int = 15000

    var out: PrintWriter? = null


    fun run(): Unit {
        var serverAddr = InetAddress.getByName(serverIP)
        var socket = Socket(serverAddr, serverPort)
        //out = PrintWriter(BufferedWriter(OutputStreamWriter(socket.getOutputStream())), true)
    }

    fun sendMessage(message :String){
        if(out != null && !out!!.checkError()){
            out!!.println(message);
            out!!.flush();
        }
    }
}

the problem is when I put the line var socket = Socket(serverAddr, serverPort) my application crash and I don’t know why. Do you have an idea ?

thanks for your help

What kind of error do you get? If you look at the documentation there are multiple reasons why Socket could fail.

https://docs.oracle.com/javase/8/docs/api/java/net/Socket.html#Socket-java.net.InetAddress-int-

Thank you for your answer,

the terminal return :
E/AndroidRuntime: FATAL EXCEPTION: Thread-7
Process: com.example.tbvv1148.myapplication, PID: 19173
java.net.SocketException: Permission denied
at java.net.Socket.createImpl(Socket.java:454)
at java.net.Socket.(Socket.java:423)
at java.net.Socket.(Socket.java:243)
at com.example.tbvv1148.myapplication.TCPClient.run(MainActivity.kt:71)
at com.example.tbvv1148.myapplication.MainActivity$t$1.invoke(MainActivity.kt:25)
at com.example.tbvv1148.myapplication.MainActivity$t$1.invoke(MainActivity.kt:18)
at kotlin.concurrent.ThreadsKt$thread$thread$1.run(Thread.kt:18)
02-16 13:14:40.818 19173-19173/? E/ViewRootImpl@b87189c[MainActivity]: ViewRootImpl #2 Surface is not valid.
There is a permission denied but i do not understand what is it

So this is going to be a bit longer. I’m going to also explain how I found this so you can do this yourself the next time you run into a problem like this.

After a bit of googling (“java socket Exception Permission denied”) I found out that for some reason the security manager did allow you to connect to that socket, obviously :wink:

I found someone with the same problem: java.net.SocketException: Permission denied: listen failed — oracle-tech

So after that I was starting to look at some documentation for the responsible parts:
Security Manager, check Permission, the socket permission class

That let me to the permission in the jdk page with the part about socket permissions.

Another thing you should look at are android permissions. I guess you are running on android based on your Exception. Android apps as far as I know need to request permissions for a lot of operations so I would look into that:

Those are the links I found with just a few google searches. I am not an android developer so I can’t really help you more, but I think those links should give you a good starting point. If you find some tutorials, they might help as well. Just be sure you don’t follow them blindly, because security is always a hard thing to get right and should not be taken lightly, but than I don’t know what you are trying to do :smile:
One of the hardest thing in programming is too learn how to google. It sounds stupid but knowing which keywords to use to find the right answers is extremely helpful. IMO asking on forums should be the step once you can no longer find an answer. (and btw, most questions have already been asked and answered :stuck_out_tongue_winking_eye:)

2 Likes

Have You solved this problem?

This is a Android problem. You must add “Internet Permission” in your app.