Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
227 views
in Technique[技术] by (71.8m points)

java - android open socket and send commands

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 and get data back.

Any ideas what i need to google to help on this? I know how to do it in PHP (using fputs, fgets, and fsockopen).

any help would be brill.

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...