2014-02-05 239 views
1

我已经写代码,clientserver将连接在wificlient将派stringserver,但问题是,即使我想我已经正确地实现它的代码不似乎工作。即使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"); 
       } 

      }); 
    }   
    } 



} 

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

回答

0

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

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

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

+0

谢谢。关于IP的观点是对的。 – user3205157