Sokets in the android through USB



  • When a client is in the Android through usb, a problem has arisen on the mouse - no sket is being created.

    // socket =(Socket) new Socket("178.57.226.134",1511); 
    

    Through LogCat, it can be seen that the code for the creation of is not met. What's the problem? Whether it's connected to any additional conditions in the maiden. In the address 127.0.0.1, localhost I tried. In this type of code, ip static ip. It's not working!

    The emulsion option is not appropriate because it is not possible to launch it because I work on the nuke and its processor does not support virtualization.

    Code:

    public class MainActivity extends Activity {
    
    private static final String TAG = "test";
    
    public String host;
    public int PORT = 1511;
    private Button button;
    private Button connect;
    private TextView textView;
    private EditText editText;
    private Socket socket;
    public InetAddress address;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG, "OnCreate");
    
        button = (Button) findViewById(R.id.button);
        connect = (Button) findViewById(R.id.button2);
        textView = (TextView) findViewById(R.id.textView);
        editText = (EditText) findViewById(R.id.editText);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "OnCreate onClick"); 
                new SendMessage().execute(editText.getText().toString());
                Log.d(TAG, "OnCreate onClick SendMessage");
            }
        });
        connect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "OnCreate befor connect onClick 58");
                new ConnectSocket().execute();
                Log.d(TAG, "OnCreate after connect onClick 60 ");
            }
        });
    }
    
    class ConnectSocket extends AsyncTask<Void, String, String> {
    
        @Override
        protected String doInBackground(Void... params) {
            try {
                address = InetAddress.getByName("localhost");
                Log.d(TAG, "ConnectSocket doInBackground start 71");
    
                socket =(Socket) new Socket("178.57.226.134",1511);
                Log.d(TAG, socket.toString());
                Log.d(TAG, "ConnectSocket doInBackground socket created 73 ");
    
                if(socket.isConnected()) {
                    Log.d(TAG, "connected");
                    return "connected";
                }
            } catch (IOException e) {
             Log.d(TAG, "fail");
                e.printStackTrace();
              }
            return null;
        }
    
        /**Override
        protected void onPostExecute(String result) {
            if(result.equals("connected")) {
                connect.setVisibility(View.GONE);
            }
            super.onPostExecute(result);
        }
        */
    }
    
    class SendMessage extends AsyncTask<String, String, String> {
    
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }
    
        @Override
        protected String doInBackground(String... params) {
           String st = (String)  new String("Text");
           String date = (String) new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date());
            while(true) {
                try {
                    PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "utf-8")), true);
                    out.println(" { " + params[0] + " }");
                    InputStreamReader streamReader = new InputStreamReader(socket.getInputStream(), "utf-8");
                    BufferedReader reader = new BufferedReader(streamReader);
                    st = reader.readLine();
                    Log.d(TAG, st);
                } catch (Exception e) {
                    Log.d(TAG, "Error ", e);
                  }
                return st;
            }
        }         
    
        @Override
        protected void onPostExecute(String result) {
            textView.setText(result);
            super.onPostExecute(result);
        }
    }
    
    @Override
    public void onStop() {
        try {
            if(socket != null) {
                socket.close();
                Log.e(TAG, "disconnected");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        super.onStop();
    }
    

    }

    There's no juice. The programme stops at this point. Then he makes a mistake, but the proga keeps working.



  • 127.0.0.1 and localhost - it's a device. So you're not gonna get involved.

    178.57.226.134 - The static outside ip of your car? I understand. You can't connect.

    If you need to connect to your dysfunction. 5111 Portu, you'll be on it in the network, see the ip on the local network. And get him in. It's on condition that Android and nuke are in one subset.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2