views:

708

answers:

2

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

+1  A: 

You will need to use a Java socket. Here are examples of using TCP sockets in Java. Here are examples of using datagram sockets in Java.

I strongly encourage you to spend some time learning Java before diving into Java socket programming, let alone on Android.

CommonsWare
+2  A: 

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:

RickNotFred
I've managed to connect to a server using Socket(), but i cant send any commands. Any ideas?
dotty