如何从 Android Tcp 客户端套接字连接到具有 Public IP 的 Java TCP 服务器套接字?
How to connect to a Java TCP server socket having Public IP From Android Tcp Client Socket?
我的工作地点有两个真实IP地址220.xx.xxx.xxx。我试图制作一个简单的 java tcp 服务器程序和 android tcp 客户端程序。 android 客户端在以下情况下工作正常:
- Server and emulator program in the same pc.
- Server in one pc having real IP address emulator is in another pc with a real ip address.
- Server and emulator both within under private network within same pc or different pc or device.
在以下情况下不起作用:
- client is a smart phone having wifi or 3g network, and server have a real IP address on different network.
- client is an emulator running on wifi connected device, server have a real ip address.
那么,如何从 Android Tcp 客户端套接字连接到具有 Public IP 的 Java TCP 服务器套接字?我不认为粘贴代码在这里真的是必要的。但是,如果有人问我可以提供。
根据我在互联网上的搜索,有人提供了在服务器端配置路由器的建议。这在我看来有点烦人。如果我 运行 一个网络服务器,它可以从任何地方访问。
此处包含的代码:
服务器端:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInput;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.*;
public class Server {
private static ServerSocket serverSocket;
private static Socket clientSocket;
private static InputStreamReader inputStreamReader;
private static BufferedReader bufferedReader;
private static OutputStreamWriter outputStreamWriter;
private static BufferedWriter bufferedWriter;
private static PrintWriter printWriter;
private static String message;
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
System.out.println(InetAddress.getLocalHost());
serverSocket = new ServerSocket(4444); // Server socket
} catch (IOException e) {
System.out.println("Could not listen on port: 4444");
}
System.out.println("Server started. Listening to the port 4444");
while (true) {
try {
clientSocket = serverSocket.accept(); // accept the client connection
System.out.println("connection initiated");
DataInputStream din = new DataInputStream(clientSocket.getInputStream());
DataOutputStream dout= new DataOutputStream(clientSocket.getOutputStream());
String msg = din.readUTF();
System.out.println(msg);
dout.writeUTF("hello from server");
System.out.println("sent to client");
clientSocket.close();
} catch (IOException ex) {
System.out.println("Problem in message reading "+ex);
}
}
}
}
客户端:
package com.example.client;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
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.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView textResponse;
EditText editTextAddress, editTextPort;
Button buttonConnect, buttonClear;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextAddress = (EditText)findViewById(R.id.address);
editTextPort = (EditText)findViewById(R.id.port);
buttonConnect = (Button)findViewById(R.id.connect);
buttonClear = (Button)findViewById(R.id.clear);
textResponse = (TextView)findViewById(R.id.response);
buttonConnect.setOnClickListener(buttonConnectOnClickListener);
buttonClear.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
textResponse.setText("");
}});
}
OnClickListener buttonConnectOnClickListener =
new OnClickListener(){
@Override
public void onClick(View arg0) {
MyClientTask myClientTask = new MyClientTask(
editTextAddress.getText().toString(),
Integer.parseInt(editTextPort.getText().toString()));
myClientTask.execute();
}};
public class MyClientTask extends AsyncTask<Void, Void, Void> {
String dstAddress;
int dstPort;
String response = "";
MyClientTask(String addr, int port){
dstAddress = addr;
dstPort = port;
}
private PrintWriter printwriter;
private InputStreamReader inputStreamReader;
private BufferedReader bufferedReader;
@Override
protected Void doInBackground(Void... arg0) {
Socket socket = null;
try {
socket = new Socket(dstAddress, dstPort);
DataInputStream din = new DataInputStream(socket.getInputStream());
DataOutputStream dout = new DataOutputStream(socket.getOutputStream());
dout.writeUTF("hello from client");
response="sent to serve;r";
String msg = din.readUTF();
response+=msg;
socket.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
response = e.toString();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
textResponse.setText(response);
super.onPostExecute(result);
}
}
}
为了 TCP connections
在真实设备上工作,您需要提供公共托管域的 URL
。 localhost
或本地网络 URL 仅适用于同一本地网络上 a PC
上的 an emulator
。
参考文献:
2.
3. Accessing localhost:port from Android emulator
4. Calling web service from android device
5. Connect an Android Device To a Web Service on Local Host
6. How can I access my localhost from my Android device?
7. How to browse localhost on android device?
我刚刚将端口号从 4444 更改为 6667,现在到处都可以使用了。我不认为端口 4444 在其他地方使用,因为该程序在本地网络上运行
我的工作地点有两个真实IP地址220.xx.xxx.xxx。我试图制作一个简单的 java tcp 服务器程序和 android tcp 客户端程序。 android 客户端在以下情况下工作正常:
- Server and emulator program in the same pc.
- Server in one pc having real IP address emulator is in another pc with a real ip address.
- Server and emulator both within under private network within same pc or different pc or device.
在以下情况下不起作用:
- client is a smart phone having wifi or 3g network, and server have a real IP address on different network.
- client is an emulator running on wifi connected device, server have a real ip address.
那么,如何从 Android Tcp 客户端套接字连接到具有 Public IP 的 Java TCP 服务器套接字?我不认为粘贴代码在这里真的是必要的。但是,如果有人问我可以提供。 根据我在互联网上的搜索,有人提供了在服务器端配置路由器的建议。这在我看来有点烦人。如果我 运行 一个网络服务器,它可以从任何地方访问。 此处包含的代码: 服务器端:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInput;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.*;
public class Server {
private static ServerSocket serverSocket;
private static Socket clientSocket;
private static InputStreamReader inputStreamReader;
private static BufferedReader bufferedReader;
private static OutputStreamWriter outputStreamWriter;
private static BufferedWriter bufferedWriter;
private static PrintWriter printWriter;
private static String message;
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
System.out.println(InetAddress.getLocalHost());
serverSocket = new ServerSocket(4444); // Server socket
} catch (IOException e) {
System.out.println("Could not listen on port: 4444");
}
System.out.println("Server started. Listening to the port 4444");
while (true) {
try {
clientSocket = serverSocket.accept(); // accept the client connection
System.out.println("connection initiated");
DataInputStream din = new DataInputStream(clientSocket.getInputStream());
DataOutputStream dout= new DataOutputStream(clientSocket.getOutputStream());
String msg = din.readUTF();
System.out.println(msg);
dout.writeUTF("hello from server");
System.out.println("sent to client");
clientSocket.close();
} catch (IOException ex) {
System.out.println("Problem in message reading "+ex);
}
}
}
}
客户端:
package com.example.client;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
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.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView textResponse;
EditText editTextAddress, editTextPort;
Button buttonConnect, buttonClear;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextAddress = (EditText)findViewById(R.id.address);
editTextPort = (EditText)findViewById(R.id.port);
buttonConnect = (Button)findViewById(R.id.connect);
buttonClear = (Button)findViewById(R.id.clear);
textResponse = (TextView)findViewById(R.id.response);
buttonConnect.setOnClickListener(buttonConnectOnClickListener);
buttonClear.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
textResponse.setText("");
}});
}
OnClickListener buttonConnectOnClickListener =
new OnClickListener(){
@Override
public void onClick(View arg0) {
MyClientTask myClientTask = new MyClientTask(
editTextAddress.getText().toString(),
Integer.parseInt(editTextPort.getText().toString()));
myClientTask.execute();
}};
public class MyClientTask extends AsyncTask<Void, Void, Void> {
String dstAddress;
int dstPort;
String response = "";
MyClientTask(String addr, int port){
dstAddress = addr;
dstPort = port;
}
private PrintWriter printwriter;
private InputStreamReader inputStreamReader;
private BufferedReader bufferedReader;
@Override
protected Void doInBackground(Void... arg0) {
Socket socket = null;
try {
socket = new Socket(dstAddress, dstPort);
DataInputStream din = new DataInputStream(socket.getInputStream());
DataOutputStream dout = new DataOutputStream(socket.getOutputStream());
dout.writeUTF("hello from client");
response="sent to serve;r";
String msg = din.readUTF();
response+=msg;
socket.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
response = e.toString();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
textResponse.setText(response);
super.onPostExecute(result);
}
}
}
为了 TCP connections
在真实设备上工作,您需要提供公共托管域的 URL
。 localhost
或本地网络 URL 仅适用于同一本地网络上 a PC
上的 an emulator
。
参考文献:
2.
3. Accessing localhost:port from Android emulator
4. Calling web service from android device
5. Connect an Android Device To a Web Service on Local Host
6. How can I access my localhost from my Android device?
7. How to browse localhost on android device?
我刚刚将端口号从 4444 更改为 6667,现在到处都可以使用了。我不认为端口 4444 在其他地方使用,因为该程序在本地网络上运行