kryonet ライブラリを使用して、コンピューターから Android アプリにデータを送信する簡単なプログラムを作成しようとしています。Android 用の動作するクライアントを作成する方法に関するドキュメントはほとんどありません。これまでのところ、グーグルの問題から十分な情報を集めて機能させることができたと思いますが、まだ接続できません。スタックトレースは「java.io.IOException: Unable to connect to:...」と言っていますが、ssh でテストしたので、IP とポートが機能することはわかっています。
クライアント側:
public class bclient extends AppCompatActivity {
Client client;
String ip = "(my ip)";
int tcpPort=2300, udpPort =2300;
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shell);
System.out.println("entering main activity");
client=new Client();
client.addListener(new Listener() {
public void received(Connection c, Object p) {
System.out.println("packets received!!");
//check if packetmessage class matches
if (p instanceof PacketMessage) {
//cast it, so we can access the message within
PacketMessage packet = (PacketMessage) p;
System.out.println("recieved a message from host: " + packet.message);
}
}
});
client.getKryo().register(PacketMessage.class);
client.start();
new AsyncTask<Integer, Void, Void>(){
@Override
protected Void doInBackground(Integer... params) {
// TODO Auto-generated method stub
try {
client.connect(5000, ip, tcpPort, udpPort);
System.out.println("connected!!!");
} catch(IOException e) {
System.out.println("not connected :(");
e.printStackTrace();
return null;
}
return null;
}
}.execute(1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_shell, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
パケット クラス:
public class PacketMessage {
public String message;
}
サーバ側:
public class aserver extends Listener{
//create server
static Server server;
//ports to listen on
static int udpPort = 2300;
static int tcpPort= 2300;
public static void main(String[]arg) throws Exception {
System.out.println("Creating the server...");
//create the server
server = new Server();
//register a packet
server.getKryo().register(PacketMessage.class);
//we can only send objects as packets if they are registered
//bind the server
server.bind(tcpPort, udpPort);
//start the server
server.start();
//add the listener
server.addListener(new aiserver());
System.out.println("Server is operational!");
}
//this is run when a connection is received!
public void connected(Connection c){
System.out.println("recieved connection fron"+c.getRemoteAddressTCP().getHostString());
//create a message packet.
PacketMessage packetMessage = new PacketMessage();
packetMessage.message= "Hello Frirend! The time is: "+new Date().toString();
//send message
//c.sendTCP(packetMessage);
//Alternatively we could do:
c.sendUDP(packetMessage);
//to send over UDP
}
//this is run when a client has received a packet
public void recieved(Connection c, Object p){
System.out.println("packet recieved by client!");
}
public void disconnected(Connection c){
System.out.println("A client disconnected!");
}
}