有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

安卓客户端和java tcp

它似乎适用于前进按钮,但不适用于后退按钮。 使用pi4j前后移动机器人,但对其进行注释以进行测试 第一个命令起作用了,我觉得这与服务器有关,而不是安卓应用程序
服务器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 安卓.app.Activity;
        import 安卓.content.Context;
        import 安卓.os.AsyncTask;
        import 安卓.os.Bundle;
        import 安卓.view.View;
        import 安卓.view.View.OnClickListener;
        import 安卓.widget.Button;
        import 安卓.widget.EditText;
        import 安卓.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) 个答案

  1. # 1 楼答案

    尝试使用jsch ssh连接代替套接字连接