无法连接wifi套接字连接

时间:2014-02-05 12:59:44

标签: android sockets wifi serversocket wifimanager

我已经为clientserver编写了代码,这些代码将通过wifi连接,client会将string发送给server,但问题是即使我认为我已经正确实现了代码它似乎不起作用。在logcat上运行代码时,即使android device也没有显示任何错误。有人可以帮忙吗? 这是client代码。

public class ControlActivity extends Activity {
WifiManager wifi=null;
WifiInfo winfo=null;
Socket ssocket=null;
String sstr=null;
String ipadd=null;
Integer ip;
TextView text;
Integer k=0;
Handler handler;
Boolean pdown=false;
InetAddress seraddr;



public String intToIp(int i) {

       return ( i & 0xFF) + "." + ((i >> 8 ) & 0xFF) + "." + ((i >> 16 ) & 0xFF) + "." + ((i >> 24 ) & 0xFF );
    }


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_control);
    wifi=(WifiManager)getSystemService(Context.WIFI_SERVICE);
    winfo = wifi.getConnectionInfo();
    ip = winfo.getIpAddress();
    ipadd = intToIp(ip);
    try {
        seraddr = InetAddress.getByName(ipadd);
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }
    text = (TextView)findViewById(R.id.textView1);
    text.setText(ipadd);


    handler = new Handler(){
        @Override
        public void handleMessage(Message msg1){
            Bundle bundle = msg1.getData();
            String str = bundle.getString("key");
            text.setText(str);
        }
    };



    ImageButton butup = (ImageButton)findViewById(R.id.buttonup);
    butup.setOnTouchListener(new View.OnTouchListener() {
        private Thread touchthread;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch(event.getAction()){
            case MotionEvent.ACTION_DOWN:
                if(pdown==false)
                    pdown=true;
                sstr = "up";
                this.touchthread = new Thread(new OnTouchClientThread());
                this.touchthread.start();
                break;

            case MotionEvent.ACTION_UP:
                pdown=false;

            }
            return true;
        }
    });


        ImageButton butdown = (ImageButton)findViewById(R.id.buttondown);
        butdown.setOnTouchListener(new View.OnTouchListener() {
            private Thread touchthread;

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch(event.getAction()){
                case MotionEvent.ACTION_DOWN:
                    if(pdown==false)
                        pdown=true;
                    sstr = "down";
                    this.touchthread = new Thread(new OnTouchClientThread());
                    this.touchthread.start();
                    break;

                case MotionEvent.ACTION_UP:
                    pdown=false;

                }
                return true;
            }
        });
        }
public class OnTouchClientThread implements Runnable{

    @Override
    public void run() {
        try {
            Message msg = handler.obtainMessage();
            Bundle bundle = new Bundle();
            ssocket = new Socket(seraddr,5479);
            PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(ssocket.getOutputStream())),true);
            while(pdown==true){
                k=k+1;
                bundle.putString("key",k.toString());
                msg.setData(bundle);
                handler.sendMessage(msg);
                out.println(sstr);

            }
            out.flush();
            out.close();
            ssocket.close();

    } catch (UnknownHostException e) {
            e.printStackTrace();
    } catch (IOException e) {
            e.printStackTrace();
    }
    }
}

这是服务器

public class FInterActivity extends Activity {

WifiManager wifi=null;
Socket ssocket=null;
String sstr=null;
Thread recithrd;
TextView testing;
ServerSocket ss;
Handler handler;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_finter);
    wifi=(WifiManager)getSystemService(Context.WIFI_SERVICE);
    testing = (TextView)findViewById(R.id.testtext);
    testing.setText("Starting thread");
    this.recithrd = new Thread(new ServerThread());
    this.recithrd.start();

    handler = new Handler(){
        @Override
        public void handleMessage(Message msg1){
            Bundle bundle = msg1.getData();
            String str = bundle.getString("key");
            testing.setText(str);
        }
    };

}

public class ServerThread implements Runnable{

    @Override
    public void run() {
        try {
            Message msg = handler.obtainMessage();
            Bundle bundle = new Bundle();
            ss = new ServerSocket(5479);
            ssocket = ss.accept();
            while(true){
                    BufferedReader input = new BufferedReader(new InputStreamReader(ssocket.getInputStream()));
                    sstr=null;
                    sstr = input.readLine();
                    if(sstr!=null){
                    bundle.putString("key", sstr);
                    msg.setData(bundle);
                    handler.sendMessage(msg);
                    }
            }


    } catch (UnknownHostException e) {
            e.printStackTrace();
            handler.post(new Runnable(){
                @Override
                public void run() {
                    testing.setText("unknown host");

                }

            });
    } catch (IOException e) {
            e.printStackTrace();
            handler.post(new Runnable(){
                @Override
                public void run() {
                    testing.setText("Io exception");
                }

            });
    }           
    }



}

是的,我已经提供了所需的所有权限。请帮忙。我被困在这几天了。

1 个答案:

答案 0 :(得分:0)

您的代码似乎在多个地方出错。

1)客户端似乎正在获取自己的地址,将其置于seraddr中,并尝试连接到自身!当然服务器有不同的地址,除非你正在进行本地连接,在这种情况下你应该只使用127.0.0.1。

2)在服务器端,您只接受一次()处理连接并退出线程。这意味着您的服务器将只接受一个连接而不再接受。这是你想要的吗?