希望有人可以提供帮助。我只是想让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
答案 0 :(得分:0)
您的服务器需要绑定到任何一个
连接到LAN路由器(在您的情况下为192.168.1.67
)中的网络适配器的特定IPv4地址。
0.0.0.0
,绑定到PC连接到的所有本地IPv4网络。
电话连接到移动网络后,必须连接到ISP分配的公共Internet IPv4地址(在您的情况下为162.192.62.91
)。
由于您的PC是从LAN路由器获得Internet连接的,因此您还需要在路由器的配置中启用端口转发,以便它可以将入站流量从路由器的公共WAN IP:端口传递到服务器所在的LAN IP:端口。势必。 您可能错过了这一步。
如果路由器支持uPNP并且已启用,则您的服务器代码可以在需要时动态配置端口转发(请参见How to port forward automatically in java?)。否则,您必须直接登录路由器并根据需要进行静态配置。
当电话与PC连接到同一LAN时,它可以连接到服务器绑定到的任何LAN IPv4地址。