我正在开发将连接到我的电脑的Android客户端。 虽然我在模拟器上测试它,但它工作正常。 但是,当我在真实设备上尝试它时,我得到套接字超时异常。
以下是相关的客户代码
public static final String SERVER_IP = "192.168.1.105"; //your computer IP address
public static final int SERVER_PORT = 500;
...
try {
//here you must put your computer's IP address.
InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
Log.e("TCP Client", "C: Connecting...");
//create a socket to make the connection with the server
Socket socket = null;
try {
//Here i am geting the exception
socket = new Socket(serverAddr, SERVER_PORT);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
这是服务器代码
public class FileServer {
public static void main (String [] args ) throws Exception {
// create socket
final int SERVER_PORT = 500;
System.out.println("Open port "+ SERVER_PORT);
ServerSocket servsock = new ServerSocket(SERVER_PORT);
final String FILE_NAME ="C:\\myImages\\Movie-Line.CSV";
File myFile = new File (FILE_NAME);
System.out.println("File to send "+ FILE_NAME);
while (true) {
System.out.println("Waiting...");
Socket sock = servsock.accept();
System.out.println("Accepted connection : " + sock);
OutputStream os = sock.getOutputStream();
new FileServer().send(os,myFile);
sock.close();
}
}
public void send(OutputStream os,File myFile) throws Exception{
FileInputStream fis = new FileInputStream(myFile);
BufferedInputStream bis = new BufferedInputStream(fis);
int counter;
byte[] buffer = new byte[1024];
while((counter = bis.read(buffer))!=-1)
os.write(buffer,0,counter);
os.flush();
bis.close();
这是我的日志
11-23 23:50:20.327: E/TCP Client(3788): C: Connecting...
11-23 23:50:57.061: W/System.err(3788): java.net.SocketTimeoutException: Connection timed out
11-23 23:50:57.092: W/System.err(3788): at org.apache.harmony.luni.platform.OSNetworkSystem.connect(Native Method)
11-23 23:50:57.092: W/System.err(3788): at dalvik.system.BlockGuard$WrappedNetworkSystem.connect(BlockGuard.java:357)
11-23 23:50:57.092: W/System.err(3788): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:204)
11-23 23:50:57.100: W/System.err(3788): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
11-23 23:50:57.100: W/System.err(3788): at java.net.Socket.startupSocket(Socket.java:705)
11-23 23:50:57.108: W/System.err(3788): at java.net.Socket.<init>(Socket.java:263)
11-23 23:50:57.108: W/System.err(3788): at com.example.atcpclient.TcpClient.run(TcpClient.java:122)
11-23 23:50:57.116: W/System.err(3788): at com.example.atcpclient.ClientActivity$ConnectTask.doInBackground(ClientActivity.java:167)
11-23 23:50:57.116: W/System.err(3788): at com.example.atcpclient.ClientActivity$ConnectTask.doInBackground(ClientActivity.java:1)
11-23 23:50:57.124: W/System.err(3788): at android.os.AsyncTask$2.call(AsyncTask.java:185)
11-23 23:50:57.124: W/System.err(3788): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
11-23 23:50:57.124: W/System.err(3788): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
11-23 23:50:57.132: W/System.err(3788): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
11-23 23:50:57.132: W/System.err(3788): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
11-23 23:50:57.139: W/System.err(3788): at java.lang.Thread.run(Thread.java:1019)