简体   繁体   中英

Android Socket Error

I'm making server in linux. and client is android. android client and linux server is under the same local network. but linux sever can't catch client's connection. what is problem with my code?

(Client)

package com.example.hello;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import java.net.InetAddress;
import java.net.InetAddress;
import java.net.InetAddress;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.LinkedList;
public class MainActivity extends ActionBarActivity {
    private Button Btn_Connect;

     private static final int ERROR = -1;
     private static final int DATA = 2;
     private static final String ERROR_KEY = "_error";
     private static final String DATA_KEY = "_data";
     private static int port = 8888;
     LinkedList<SocketClient> threadList;



    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        threadList = new LinkedList<MainActivity.SocketClient>();
        Btn_Connect = (Button) findViewById(R.id.Btn_cennect);



        Btn_Connect.setOnClickListener(new OnClickListener() {
             Socket socket = null;  
            @Override
            public void onClick(View v) {
                //Toast.makeText(getApplicationContext(), "asasdfsdf", 100).show();
                Log.d("msg", "park0");

                SocketClient client = new SocketClient("192.168.171.129");
                threadList.add(client);
                client.start();

                Intent intent = new Intent(MainActivity.this, Connected.class);
                startActivity(intent);



            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    class SocketClient extends Thread{
        boolean threadAlive;
        String ip;
        public SocketClient(String ip){
               threadAlive = true;
               this.ip = ip;
        }

        @Override
        public void run() {
               Socket socket = null;
               InputStream inputStream = null;
               BufferedInputStream bufStream = null;
               try {
                      socket = new Socket(ip,port);
                      inputStream = socket.getInputStream();
                  //    bufStream = new BufferedInputStream(inputStream);
                      int len = 0;
                      byte[] readBytes = new byte[1000];
                      while( (len = bufStream.read(readBytes)) != -1 ){
                      //      Message msg = Message.obtain();
                        //    msg.what = DATA;
                          //  Bundle bundle = new Bundle();
              //              bundle.putString(DATA_KEY, new String(readBytes, 0, len));
                //            msg.setData(bundle);
                          //  uiHandler.sendMessage(msg);
                      }

               } catch (Exception e) {
              //        Message msg = Message.obtain();
                //      msg.what = ERROR;
              //        Bundle bundle = new Bundle();
                //      bundle.putString(ERROR_KEY, e.toString());
                  //    msg.setData(bundle);
                    //  uiHandler.sendMessage(msg);
               } finally{
                      try {
                            bufStream.close();
                            inputStream.close();
                            socket.close();
                      } catch (IOException e) {
//                            Message msg = Message.obtain();
//                            msg.what = ERROR;
//                            Bundle bundle = new Bundle();
//                            bundle.putString(ERROR_KEY, e.toString());
//                            msg.setData(bundle);
//                            uiHandler.sendMessage(msg);     
                      }
               }
        }
  }



}
public class ChatApplication extends Application {

    private Socket mSocket;
    {
        try {
            mSocket = IO.socket(Constants.CHAT_SERVER_URL);
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
    }

    public Socket getSocket() {
        return mSocket;
    }
}

this will return socket.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM