1
我已经写代码,client
和server
将连接在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");
}
});
}
}
}
是的,我已经提供了所需的所有权限。请帮忙。我坚持了这几天。
谢谢。关于IP的观点是对的。 – user3205157