android客户端与javatcp

android客户端与javatcp,java,android,sockets,tcp,java-server,Java,Android,Sockets,Tcp,Java Server,它似乎适用于前进按钮,但不适用于后退按钮。 使用pi4j前后移动机器人,但对其进行注释以进行测试 第一个命令起作用了,我觉得这与服务器有关,而不是android应用程序 服务器java代码 import java.io.BufferedReader; import java.io.DataInputStream; import java.io.DataOutputStream; import

它似乎适用于前进按钮,但不适用于后退按钮。 使用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();
                     }
                }
               }
              }
             }
            }
客户端android

        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();
                       }
                      }



          }

         }


         }
package com.example.client;
导入java.io.DataInputStream;
导入java.io.DataOutputStream;
导入java.io.IOException;
导入java.net.Socket;
导入java.net.UnknownHostException;
导入android.app.Activity;
导入android.content.Context;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.TextView;
公共类控件扩展活动实现OnClickListener{
编辑文本输出;
布尔flegmove=false;
//TextView textIn;
字符串comand=“”;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.conroller);
textOut=(EditText)findViewById(R.id.textOut);
Button Button前进=(Button)findViewById(R.id.B前进);
buttonforward.setOnClickListener(此);
Button buttonback=(Button)findViewById(R.id.bback);
setOnClickListener(这个);
Button buttonleft=(Button)findViewById(R.id.bleft);
buttonleft.setOnClickListener(此);
按钮按钮右=(按钮)findViewById(R.id.bright);
buttonright.setOnClickListener(此);
}
公共void onClick(视图v)
{
开关(v.getId())
{
案例R.id.B前面:
{
comand=“前进”;
弗莱格莫夫=真;
新建task1().execute(comand);
打破
}
案例R.id.bback:
{
comand=“返回”;
弗莱格莫夫=真;
新建task1().execute(comand);
打破
}
案例R.id.bright:
{
comand=“右”;
弗莱格莫夫=真;
新建task1().execute(comand);
打破
}
案例R.id.bleft:
{
comand=“左”;
弗莱格莫夫=真;
新建task1().execute(comand);
打破
}
}
如果(flegmove==true)
{
comand=comand.toString();
System.out.println(“comand”+comand);
//新建task1().execute(comand);
}
}
私有类task1扩展了异步任务
{
套接字=空;
字符串答案=”;
DataOutputStream DataOutputStream=null;
DataInputStream DataInputStream=null;
受保护的字符串doInBackground(字符串…参数)
{
尝试
{
插座=新插座(“192.168.1.12”,80);
dataOutputStream=新的dataOutputStream(socket.getOutputStream());
dataInputStream=新的dataInputStream(socket.getInputStream());
/
dataOutputStream.writeUTF(comand);
系统输出打印项次(“comand”);
answer=dataInputStream.readUTF();
} 
捕获(未知后异常e)
{
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常)
{
//TODO自动生成的捕捉块
e、 printStackTrace();
}
返回答案;
}
受保护的void onPostExecute(字符串结果){
如果(dataOutputStream!=null){
试一试{
dataOutputStream.close();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
如果(dataInputStream!=null){
试一试{
dataInputStream.close();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
}
}
}

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