我正在使用套接字开发一个安卓应用程序。但Socket soc=new Socket(ip,port)
总是给我一个SocketTimeoutException
。我在java项目中使用socket,没关系。但不是在android项目中。
public static void main(String[] args) {
try{
Socket soc=new Socket("192.168.177.103",8888);
}catch(Exception e){
e.printStackTrace();
}
}
这没关系,但不是在 android 项目中。为什么?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mmmm);
new Thread(){
public void run(){
try{
Socket soc=new Socket("192.168.177.103",8888);
}catch(Exception e){
e.printStackTrace();
}
}
}.start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_mmmm, menu);
return true;
}
有人能告诉我为什么吗?非常感谢。在android中使用socket时的异常详情:
java.net.SocketTimeoutException: Connection timed out
at org.apache.harmony.luni.platform.OSNetworkSystem.connect(Native Method)
at dalvik.system.BlockGuard$WrappedNetworkSystem.connect(BlockGuard.java:361)
at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:204)
at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
at java.net.Socket.startupSocket(Socket.java:732)
at java.net.Socket.tryAllAddresses(Socket.java:152)
at java.net.Socket.<init>(Socket.java:211)
at java.net.Socket.<init>(Socket.java:178)
at com.example.mmmmm.MMMMActivity$1.run(MMMMActivity.java:26)