Android Open Source - android-gpio-controller Socket Client






From Project

Back to project page android-gpio-controller.

License

The source code is released under:

GNU General Public License

If you think the Android project android-gpio-controller listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package io.github.petermuller.raspberrypigpiocontroller;
//from  w w w  . j  av a2s.co m
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * Created by Peter on 1/1/2015.
 */
public class SocketClient {

    /*
    * Class Members
    *
    * s - socket connection
    * out - output writer
    * in - input reader
    * */
    Socket s;
    PrintWriter out;
    BufferedReader in;

    public SocketClient() {}
    public SocketClient(String dest, int port) throws UnknownHostException, IOException{
        char[] reply = new char[10];
        s = new Socket(dest,port);
        out = new PrintWriter(s.getOutputStream(),true);
        in = new BufferedReader(new InputStreamReader(s.getInputStream()));
        out.print("Testing!");
        in.read(reply);
        //if (!(reply.toString() == "yes!!")){
        //    throw new IOException("Connection not established");
        //} //Else success!
    }

    public void sendMessage(String message){
        if (message.length() > 10){ //Avoid overflowing the server buffer.
            message = message.substring(0,9);
        } else {
            while (message.length() < 10){
                message += ' ';
            }
        } //Message is now exactly 10 characters long
        out.print(message);
    }
}




Java Source Code List

io.github.petermuller.raspberrypigpiocontroller.ApplicationTest.java
io.github.petermuller.raspberrypigpiocontroller.InputHandler.java
io.github.petermuller.raspberrypigpiocontroller.MainActivity.java
io.github.petermuller.raspberrypigpiocontroller.OutputHandler.java
io.github.petermuller.raspberrypigpiocontroller.PinHandler.java
io.github.petermuller.raspberrypigpiocontroller.SocketClient.java