[英]ServerSocket tcp server on pc unreachable by Android phone client
希望有人可以幫忙。 我只是想讓Windows PC服務器和Android手機之間的TCP套接字連接正常工作。 首先,如何確定需要哪個IP地址。 我有ATT UVerse寬帶,我的電腦通過以太網連接到它的盒子。 根據ATT:
https://www.att-services.net/tools/tool-ip-address-hostname.html
我有一個IP地址:162.192.62.91。 根據ipconfig,我還有一個:192.168.1.67。 無論是否在調用之后立即啟動客戶端,兩次都在ServerSocket上調用accept()都超時。 無論如何,我需要哪一個? Netstat准確反映服務器偵聽器的存在,直到服務器代碼終止,然后反映其不存在。 不知道我在做什么錯。 Windows防火牆當前處於關閉狀態。 感謝任何願意看一下的人。 這是Java服務器:
package my.server;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class MyServerSocket {
static ServerSocket server;
static String addr = "192.168.1.67";
static int port = 8080;
InetAddress ia;
private MyServerSocket() throws Exception {
if (addr != null && !addr.isEmpty()) {
ia = InetAddress.getByName(addr); //ipAddress);
System.out.println("18 MSS InetAddress.getByName(" + addr + ") = " + ia);
try {
server = new ServerSocket(port, 1, ia);
System.out.println("20 MSS Nonempty ipAddress: instantiated ServerSocket");
} catch( IOException e) {
System.out.println("22 MSS while instantiating ServerSocket: " + e);
}
}
else {
ia = InetAddress.getLocalHost();
server = new ServerSocket(port, 1, ia);
System.out.print("28 MSS empty ipAddress: MSS new ServerSocket port ");
System.out.print(port);
System.out.println(", addr " + ia);
}
}
private void listen() throws Exception {
String data = null;
Socket client = server.accept(); //Hangs here
String clientAddress = client.getInetAddress().getHostAddress();
System.out.println("35 MSS New connection from " + clientAddress);
BufferedReader in = new BufferedReader(
new InputStreamReader(client.getInputStream()));
while ( (data = in.readLine()) != null ) {
System.out.println("40 MSS Message from " + clientAddress + ": " + data);
}
}
public static void main(String[] args) throws Exception {
MyServerSocket app = new MyServerSocket();
System.out.println("52 MSS instantiated MyServerSocket: " +
" Addr=" + server.getInetAddress() +
" Port=" + server.getLocalPort());
app.listen();
}
}
Android客戶端:
package my.androidsocket;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import android.os.AsyncTask;
import android.util.Log;
public class AndroidClientSocket extends AsyncTask <String, Void, String>{
private static final String LOG_TAG = "ClientSocket";
static InputStream is = null;
private String serverHostOrIPAddr = "192.168.1.67"; //162.192.62.91 (can't assign addr)
//127.0.0.1 (refused) "listening on port 3306 (mysql) for local loopback only"
//192.168.1.67 (times out w an Exception) (from cmd->ipconfig-> ip4 address)
private String serverPortStr = "8080";
@Override
protected String doInBackground(String... params) {
Integer serverPort = Integer.parseInt(serverPortStr);
Log.d(LOG_TAG,"24 ACS connecting to host " + serverHostOrIPAddr +
" on port " + serverPort + ".");
Socket socket = null;
PrintWriter out = null;
BufferedReader in = null;
try {
//hangs, then returns null even when server is listening to the addr/port at startup
socket = new Socket(serverHostOrIPAddr, serverPort);
System.out.print("33 ACS Socket instantiation returned ");
System.out.println(socket);
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String userInput = "a\r\n";
while (true) {
System.out.print("client: ");
//String userInput = stdIn.readLine();
/** Exit on 'q' char sent */
if ("q".equals(userInput)) {
break;
}
out.println(userInput); //out to console
userInput = "";
System.out.println("server: " + in.readLine()); //chars from server
}
} catch (UnknownHostException e) {
Log.d(LOG_TAG,"55 ACS while connecting to host " + serverHostOrIPAddr + ": " + e );
try {
socket.close();
} catch (IOException e1) {
Log.d(LOG_TAG,"59 ACS while closing socket " + e );
}
System.exit(1);
} catch (IOException e) {
Log.d(LOG_TAG,"63 ACS while connecting to host: " + e);
if(socket!=null) {
try {
socket.close();
} catch (IOException e1) {
Log.d(LOG_TAG,"68 ACS while closing socket: " + e);
}
}
System.exit(1);
}
String userInput = "user input...\r\n"; //Fake the user console input
out.println(userInput);
Log.d(LOG_TAG,"76 ACS user input sent to socket output stream: " + userInput);
out.close();
try {
in.close();
} catch (IOException e) {
Log.d(LOG_TAG,"82 ACS IOException closing input stream: " + e);
}
try {
socket.close();
} catch (IOException e) {
Log.d(LOG_TAG,"87 ACS IOException closing socket: " + e);
}
return "null";
}
}
和Android呼叫者:
package my.androidsocket;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AndroidClientSocket acs = new AndroidClientSocket( );
acs.execute();
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("18 MA GET response: " + "null");
}
}
並輸出:
18 MSS InetAddress.getByName(192.168.1.67) = /192.168.1.671
20 MSS Nonempty ipAddress: instantiated ServerSocket: port=8080 bindAddr=/192.168.1.67
您的服務器需要綁定到
連接到LAN路由器的網絡適配器的特定IPv4地址(在您的情況下為192.168.1.67
)。
0.0.0.0
,它綁定到PC連接到的所有本地IPv4網絡。
當電話連接到移動網絡時,它必須連接到由ISP分配的公共Internet IPv4地址(在您的情況下為162.192.62.91
)。
由於您的PC是從LAN路由器獲得Internet連接的,因此您還需要在路由器的配置中啟用端口轉發,以便它可以將入站流量從路由器的公共WAN IP:端口傳遞到服務器所綁定的LAN IP:端口。 您可能會錯過此步驟。
如果您的路由器支持uPNP並且已啟用,則您的服務器代碼可以在需要時動態配置端口轉發(請參閱如何在Java中自動進行端口轉發? )。 否則,您必須直接登錄路由器並根據需要進行靜態配置。
當電話與PC連接到同一LAN時,它可以連接到服務器綁定到的任何LAN IPv4地址。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.