[英]Android Kryonet client wont connect to my PC server?
I made a basic server in eclipse: 我在Eclipse中制作了一个基本服务器:
Server server;
int tcp = 8885, udp = 8885;
public ServerMain() {
server = new Server();
server.addListener(new Listener() {
public void connected(Connection c) {
System.out.println("Connection made");
}
public void received(Connection c, Object p) {
}
public void disconnected(Connection c) {
System.out.println("Disconnection made");
}
});
server.start();
try {
server.bind(tcp, udp);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new ServerMain();
}
Then I created my client in android studio. 然后,我在android studio中创建了客户端。 I installed the library correctly.
我正确安装了库。 (According to google) I put the Kryonet jar into the libs folder and then rebuilt the project.
(根据google)我将Kryonet jar放入libs文件夹,然后重新构建了项目。 I could then use Kryonet classes.
然后,我可以使用Kryonet类。 Here is my code for my client:
这是我给客户的代码:
Client client;
String ip = "MY.IP.ADDRESS";
int tcp = 8885, udp = 8885;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG, "Start");
client = new Client();
client.addListener(new Listener() {
public void received(Connection c, Object p) {
}
});
client.start();
try {
client.connect(5000, ip, tcp, udp);
Log.i(TAG, "conected");
} catch(IOException e) {
Log.i(TAG, "Error: " + e.getMessage());
}
}
It's the exact same code I would use in a normal Java client except now it's running directly from my android device. 除了现在直接从我的android设备运行之外,它与在普通Java客户端中使用的代码完全相同。 I get this error:
我收到此错误:
Error: Unable to connect to: /MY.IP.ADDRESS:8885
It cannot connect to my IP even though I port forwarded it and all. 即使我端口转发了它,它也无法连接到我的IP。 Why won't it connect?
为什么不连接? I have read online to add in this to my manifest file:
我已经在线阅读将其添加到清单文件中:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
So, I did: 所以我做了:
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Still to absolutely no avail. 仍然绝对无济于事。 What am I doing wrong?
我究竟做错了什么? (PS I have tried with both my network IP, and my public IP. I have no idea what I'm doing wrong, everything works fine with a normal Java program, but with an android app it bugs out.)
(PS我已经尝试过使用我的网络IP和我的公共IP。我不知道我在做什么错,在正常的Java程序中一切正常,但是在android应用中它会出错。)
Try this code, it's working: 试试下面的代码,它可以正常工作:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG, "Start");
client = new Client();
client.addListener(new Listener() {
public void received(Connection c, Object p) {
}
});
client.start();
new ConnectToServer.execute();
}
public class ConnectToServer extends AsyncTask<String, int[], String>
{
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
try {
client.connect(5000, ip, tcp, udp);
Log.i(TAG, "conected");
} catch(IOException e) {
Log.i(TAG, "Error: " + e.getMessage());
return null;
}
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.