我是这个网站的新手,但很久以来一直在阅读。 我对这个IP东西感到困惑,我是合理的程序员,但在IP方面很差。
我想用Android Server实现C#客户端[Android设备遍布全球,所以首先要获取其IP地址]。
我已经在stackOverflow问题中编写了[复制]以下android代码并尝试使用它。
public static String getLocalIpAddress() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
String ip = Formatter.formatIpAddress(inetAddress.hashCode());
Log.i("IP", "***** IP="+ ip);
return ip;
}
}
}
} catch (SocketException ex) {
Log.e("EXCPTN", ex.toString());
}
return null;
}
问题1:它给了我一些Ip让我们说142.171.13.206,但是当我通过Google搜索检查我的IP时,我的ip&#39;从我的andoird决定,我完全得到新的IP [120.100.78.14]。我在做什么错?我在Wifi上运行,没有边缘没有3g或其他东西。
问题2:如果我有一个C#客户端,我应该尝试连接哪个Android IP?因为我无法连接到Android服务器。
我的C#客户端代码是这样的
try
{
const int Port = 4321;
TcpClient ourMagicClient = new TcpClient();
//Connect to the server - change this IP address to match your server's IP!
ourMagicClient.Connect("192.177.111.200", Port);
//Use a NetworkStream object to send and/or receive some data
NetworkStream ourStream = ourMagicClient.GetStream();
//Let's set up some data!
byte[] data = Encoding.ASCII.GetBytes("Plz recieve this msg!");
//Everyone ready? Send that bad-boy!
ourStream.Write(data, 0, data.Length); //Start at the 0'th position in our string and send until the end of the string, but we can stop there...
}
catch (Exception ex)
{
textBox1.Text = ex.ToString();
}
最后这是运行4.4 Kitkat
的Android监听器的代码private class SocketServerThread extends Thread {
static final int SocketServerPORT = 4321;
int count = 0;
@Override
public void run() {
try {
serverSocket = new ServerSocket(SocketServerPORT);
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
info.setText("I'm waiting here: "
+ serverSocket.getLocalPort());
;
}
});
while (true) {
Socket socket = serverSocket.accept();
count++;
message += "#" + count + " from " + socket.getInetAddress()
+ ":" + socket.getPort() + "\n";
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
msg.setText(message);
}
});
SocketServerReplyThread socketServerReplyThread = new SocketServerReplyThread(
socket, count);
socketServerReplyThread.run();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private class SocketServerReplyThread extends Thread {
private Socket hostThreadSocket;
int cnt;
SocketServerReplyThread(Socket socket, int c) {
hostThreadSocket = socket;
cnt = c;
}
@Override
public void run() {
OutputStream outputStream;
String msgReply = "Hello from Android, you are #" + cnt;
try {
outputStream = hostThreadSocket.getOutputStream();
PrintStream printStream = new PrintStream(outputStream);
printStream.print(msgReply);
printStream.close();
message += "replayed: " + msgReply + "\n";
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
msg.setText(message);
}
});
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
message += "Something wrong! " + e.toString() + "\n";
}
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
msg.setText(message);
}
});
}
}
我有很多googles,也搜索过这个网站,但是我无法用Andoird服务器连接C#客户端。 Plz帮助我解释代码在哪里修复。或者如果你有完全不同的工作代码,请与我分享。 注意:感谢我复制上面代码的所有人[不幸的是我没有参考他们的网站]。 THX!