当前位置: 代码迷 >> Android >> 在Android里启动一个ServerSocket,在Android外使用client连接报错,该如何处理
  详细解决方案

在Android里启动一个ServerSocket,在Android外使用client连接报错,该如何处理

热度:108   发布时间:2016-05-01 21:34:09.0
在Android里启动一个ServerSocket,在Android外使用client连接报错
各位好

我在android里启动了一个ServerSocket监听10000端口,又在本地的另一非Android工程中,想启动client来给Server发消息。

但是报出了以下错误:

(使用127.0.0.1 port:10000)
java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:529)
at java.net.Socket.connect(Socket.java:478)
at java.net.Socket.<init>(Socket.java:375)
at java.net.Socket.<init>(Socket.java:189)
at com.socket.SocketClient.<init>(SocketClient.java:11)
at com.socket.SocketClient.main(SocketClient.java:39)
Exception in thread "main" java.lang.NullPointerException
at com.socket.SocketClient.sendMsg(SocketClient.java:21)
at com.socket.SocketClient.main(SocketClient.java:40)

(使用10.0.2.2 port:10000)
java.net.NoRouteToHostException: No route to host
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:529)
at java.net.Socket.connect(Socket.java:478)
at java.net.Socket.<init>(Socket.java:375)
at java.net.Socket.<init>(Socket.java:189)
at com.socket.SocketClient.<init>(SocketClient.java:11)
at com.socket.SocketClient.main(SocketClient.java:39)
Exception in thread "main" java.lang.NullPointerException
at com.socket.SocketClient.sendMsg(SocketClient.java:21)
at com.socket.SocketClient.main(SocketClient.java:40)

求大大帮忙分析下原因是什么呢?
先谢谢了

------解决方案--------------------
权限?
------解决方案--------------------
好像android模拟器的IP是10.0.0.2,不是127.0.0.1
------解决方案--------------------
不管你作为localhost还是server,IP地址改变了?不要想得太复杂了,其实就是两个IP地址通讯,不管哪一方是服务器都一样
  相关解决方案