android open socket and send commands

后端 未结 1 1882
粉色の甜心
粉色の甜心 2020-12-10 00:00

Hay Guys, I\'m new to Android but heres what i want to do.

I want to beable to open a connect to a server using a given IP and PORT, then send commands to the server

相关标签:
1条回答
  • 2020-12-10 01:01

    Use the java.net classes. Below is a simple example using DatagramSockets:

    String cmd("my command");
        try {
            InetSocketAddress address = new InetSocketAddress("10.1.1.1", 12350);
            DatagramPacket request = new DatagramPacket(cmd.getBytes(), cmd.length(), address);
            DatagramSocket socket = new DatagramSocket();
            socket.send(request);
        } catch (SocketException e) {
    
            ...
            }
        } catch (IOException e) {
    
           ...
            }
        }
    

    Other Java samples can be found here:

    0 讨论(0)
提交回复
热议问题