这是我的manifest文件
<uses-permission android:name="android.permission.INTERNET" />
这里面设置了,网络访问权限。
activity中的代码,当点击连接按钮时执行下面的代码。
if (isConnecting) {
try {
if (socket != null && clientThread != null) {
socket.close();
// clientThread.interrupt();
}
} catch (IOException e) {
e.printStackTrace();
}
connectBtn.setText("上线");
isConnecting = false;
} else {
clientThread = new Thread(myRunnable); // build a client thread
clientThread.start();
connectBtn.setText("下线");
isConnecting = true;
}
}
这是Runnable中的 代码
private Runnable myRunnable = new Runnable() {
@Override
public void run() {
Log.d("tag", "myRunnable");
ObjectInputStream ois = null;
String sIP = "192.168.1.138";
int port = 54679;
try {
socket = new Socket(sIP, port);
socket.setSoTimeout(5000);
Message threadMsg = new Message();
threadMsg.what =1;
/*
handleMsg = "myRunnable";
handler.sendMessage(threadMsg);*/
handleMsg = "已连接\n";
handler.sendMessage(threadMsg);
ois = new ObjectInputStream(socket.getInputStream());
Log.d("tag", "socket is started");
} catch (Exception e1) {
Message threadMsg = new Message();
threadMsg.what =1;
handleMsg = "连接异常:" + e1.getMessage();
handler.sendMessage(threadMsg);
isConnecting = false;
e1.printStackTrace();
}
while (isConnecting) {
try {
message = (MessageBean) ois.readObject();
} catch (Exception e) {
e.printStackTrace();
}
Message threadMsg = new Message();
threadMsg.what =1;
handleMsg = "发送人:" + message.getSender() + " 消息:"
+ message.getMsg();
handler.sendMessage(threadMsg);
}
}
};
我都检查了N多遍,都不行啊!!!!!!!!!!!!!!!!!!!!!!!总是提示 timed out!!
真的不知道怎么检查下去了,求救啊!!!!
android socket internet权限
------解决方案--------------------
用手机运行的 还是用模拟器,模拟器可能需要转TCP端口,高版本的Android不需要,
然后确认 192.168.1.138 这个是通的。
------解决方案--------------------
用java代码直接测试,看看能不能连接成功