【发布时间】:2014-03-01 22:18:09
【问题描述】:
我已经为client 和server 编写了代码,它们将通过wifi 连接,client 将发送string 到server,但问题是即使我认为我已经实现了代码正确它似乎不起作用。当我在android device 上运行代码时,即使logcat 也没有显示任何错误。有人可以帮忙吗?
这是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");
}
});
}
}
}
是的,我已经提供了所需的所有权限。请帮忙。我被困了好几天了。
【问题讨论】:
标签: android sockets wifi serversocket wifimanager