android客户端和java tcp

时间:2014-01-12 19:47:13

标签: java android sockets tcp java-server

它似乎适用于前进按钮,但不适用于后退按钮。 使用pi4j前后移动机器人,但评论他们进行测试 第一个命令工作,我觉得它与服务器更多,然后Android应用程序
服务器java代码

                import java.io.BufferedReader;
            import java.io.DataInputStream;
            import java.io.DataOutputStream;
            import java.io.IOException;
            import java.io.InputStreamReader;
            import java.net.ServerSocket;
            import java.net.Socket;
            import java.util.Scanner;
            import com.pi4j.io.gpio.GpioController;
            import com.pi4j.io.gpio.GpioFactory;
            import com.pi4j.io.gpio.GpioPinDigitalOutput;
            import com.pi4j.io.gpio.PinState;
            import com.pi4j.io.gpio.RaspiPin;
            import com.pi4j.io.gpio.GpioController;
            import com.pi4j.io.gpio.GpioFactory;
            import com.pi4j.io.gpio.GpioPinDigitalOutput;
            import com.pi4j.io.gpio.PinPullResistance;
            import com.pi4j.io.gpio.PinState;
            import com.pi4j.io.gpio.RaspiPin;


            public class Server {
                String input="ggg"; 

             public static void main(String[] args)throws InterruptedException
             {
              ServerSocket serverSocket = null;
              Socket socket = null;
              DataInputStream dataInputStream = null;
              DataOutputStream dataOutputStream = null;

              try 
              {
               serverSocket = new ServerSocket(80);
               System.out.println("Listening :80");

              } 
              catch (IOException e) 
              {
               // TODO Auto-generated catch block
               e.printStackTrace();
              }

              while(true)
              {
               try 
               {
                socket = serverSocket.accept();
                dataInputStream = new DataInputStream(socket.getInputStream());
                dataOutputStream = new DataOutputStream(socket.getOutputStream());

                System.out.println("ip: " + socket.getInetAddress());
                //System.out.println("message: " + dataInputStream.readUTF());

                        if(dataInputStream.readUTF().contains("forward"))
                        {
                                                                 Runtime r = Runtime.getRuntime();
                             Process p = null;
                             String commands="ls";
                             //boolean Light=false;


                                   // provision gpio pin #01 as an output pin and turn on
                                    try
                                    {
                                        //Light=true;
                                         p = r.exec(commands);
                                         BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
                                         String str = "";

                                         while ((str = br.readLine()) != null)
                                         {




                                            // create gpio controller
                                            System.out.println(str);
                                         }
                                        p.waitFor();
                                        br.close();
                                        //p.waitFor();
                                    } 
                            catch (Exception e)
                                    {
                                    System.out.println("Exception "+e.getMessage());
                                    System.err.println("Exception "+e.getMessage());
                                    }
                                            }

                        else if(dataInputStream.readUTF().contains("back"))
                        {
                                   //Runtime rt=Runtime.getRuntime();
                            //Process pr=rt.exec(cmd);
                             Runtime r = Runtime.getRuntime();
                             Process p = null;
                             String commands="dir";



                                    try
                                    {

                                         p = r.exec(commands);
                                         BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
                                         String str = "";

                                         while ((str = br.readLine()) != null)
                                         {





                                            System.out.println(str);
                                         }
                                        p.waitFor();
                                        br.close();
                                        //p.waitFor();
                                    } 
                            catch (Exception e)
                                    {
                                    System.out.println("Exception "+e.getMessage());
                                    System.err.println("Exception "+e.getMessage());
                                    }


                }
               catch (IOException e)
               {
                // TODO Auto-generated catch block
                e.printStackTrace();
               }
               finally
               {

                if( socket!= null)
                {
                     try {
                      socket.close();
                        } 
                        catch (IOException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                        }
                }

                if( dataInputStream!= null)
                {
                     try
                     {
                      dataInputStream.close();
                     } 
                    catch (IOException e) 
                    {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                     }
                }

                if( dataOutputStream!= null)
                {
                     try 
                     {
                      dataOutputStream.close();
                     } 
                     catch (IOException e) 
                     {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                     }
                }
               }
              }
             }
            }

客户端安卓

        package com.example.client;


        import java.io.DataInputStream;
        import java.io.DataOutputStream;
        import java.io.IOException;
        import java.net.Socket;
        import java.net.UnknownHostException;

        import android.app.Activity;
        import android.content.Context;
        import android.os.AsyncTask;
        import android.os.Bundle;
        import android.view.View;
        import android.view.View.OnClickListener;
        import android.widget.Button;
        import android.widget.EditText;
        import android.widget.TextView;

        public class controll extends Activity implements  OnClickListener {

        EditText textOut;
        boolean flegmove=false;
        //TextView textIn;
        String comand="";

         /** Called when the activity is first created. */
         @Override
         public void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             setContentView(R.layout.conroller);

             textOut = (EditText)findViewById(R.id.textout);
             Button buttonforward = (Button)findViewById(R.id.bforward);
             buttonforward.setOnClickListener(this);
             Button buttonback = (Button)findViewById(R.id.bback);
             buttonback.setOnClickListener(this);
             Button buttonleft = (Button)findViewById(R.id.bleft);
             buttonleft.setOnClickListener(this);
             Button buttonright = (Button)findViewById(R.id.bright);
             buttonright.setOnClickListener(this);






         }
         public void onClick(View v)
         {
            switch(v.getId())
            {
                case R.id.bforward:
                {

                    comand="forward";
                    flegmove=true;
                    new task1().execute(comand);
                    break;

                 }
                case R.id.bback:
                 {
                    comand="back";
                    flegmove=true;
                    new task1().execute(comand);
                    break;

                 }
                case R.id.bright:
                 {
                    comand="right";
                    flegmove=true;
                    new task1().execute(comand);
                    break; 

                 }
                case R.id.bleft:
                 {
                    comand="left";
                    flegmove=true;
                    new task1().execute(comand); 
                    break;

                 }

         }
             if(flegmove==true)
             {
                 comand=comand.toString();
                 System.out.println("comand"+comand);
                 //new task1().execute(comand); 
             }
         }


          private class task1 extends AsyncTask<String, Void,String> 
         {

             Socket socket = null;
             String answer="";
             DataOutputStream dataOutputStream = null;
             DataInputStream dataInputStream = null;
            protected String doInBackground(String...params) 
            {

                try 
                {
                      socket = new Socket("192.168.1.12", 80);
                      dataOutputStream = new DataOutputStream(socket.getOutputStream());
                      dataInputStream = new DataInputStream(socket.getInputStream());
                     /
                      dataOutputStream.writeUTF(comand);
                      System.out.println("comand");
                      answer=dataInputStream.readUTF();
                 } 
                catch (UnknownHostException e) 
                {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                 } catch (IOException e)
                 {
                      // TODO Auto-generated catch block
                      e.printStackTrace();

                 }


                return  answer;


            }

            protected void onPostExecute(String result) {

                      if (dataOutputStream != null){
                       try {
                        dataOutputStream.close();
                       } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                       }
                      }

                      if (dataInputStream != null){
                       try {
                        dataInputStream.close();
                       } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                       }
                      }



          }

         }


         }

1 个答案:

答案 0 :(得分:-1)

尝试使用jsch ssh connexion而不是socket connexion