Wi-Fi direct
を使用してAndroidのマルチプレイヤーゲームを開発しています。 Wi-Fiダイレクトを使用して、ピアデバイス間の接続を確立できました。また、コードは、クライアント装置からサーバ装置にデータを送信することができる。私は直接のWi-Fiを使用して、クライアントデバイスにサーバ装置からのデータを共有することはできませんよWi-Fiダイレクトによるピアツーピアデータ共有
:
問題(Androidのドキュメントによると、のWi-Fiダイレクトは、クライアントサーバモデルを使用しています)。
私が質問に次き:
- は、Wi-Fiダイレクトを介して接続されている2つの Androidデバイス間でデータ
(bi-directional)
を転送する他の方法はありますか? - オンライン調査中に、サーバ からクライアントデバイスにデータを送信するには、サーバがクライアントのIPアドレス を知る必要があることを理解しました。このクライアントのIPアドレスを使用して サーバーデバイスからクライアントデバイスにデータを送信する方法(クライアントのIP アドレスを取得できます)
これらのクエリに関するご意見をお寄せいただきありがとうございます。前もって感謝します。
コード: サーバー側
public class DataTransferAsyncTask extends AsyncTask<Void,Void,String>{
ServerSocket serverSocket;
Socket client;
InputStream inputstream;
Context context = mActivity.getApplicationContext();
String s;
InetAddress client_add;
@Override
protected String doInBackground(Void... voids) {
try{
serverSocket = new ServerSocket(9999);
Log.e("hello", "Server: Socket opened");
client = serverSocket.accept();
Log.e("hello", "Server: connection done");
inputstream = client.getInputStream();
// OutputStream outputStream = serverSocket.getO
//getting data from client
byte[] address = new byte[12];
if(client.isConnected())
inputstream.read(address);
s = new String(address);
String only_add = new String();
only_add = s.substring(0,12);
client_add = InetAddress.getByName(only_add);
Log.e("hello", "Server: clients ip 1 " + only_add);
Log.e("hello", "Server: converted address 1 " + client_add + " \n is connected"+
client.isConnected());
//send data to client
OutputStream stream = client.getOutputStream();
stream.write(s.getBytes());
Log.e("hello","context value "+context);
// cancel(true);
}catch (IOException e){
}
return null;
}
}
クライアント側:
@override
protected void onHandleIntent(Intent intent) {
Log.e("hello","client socket");
Toast.makeText(this,"client socket",Toast.LENGTH_LONG).show();
Context context = getApplicationContext();
if(intent.getAction().equals(action_send_data)){
String host = intent.getStringExtra(group_owner_address);
Socket socket = new Socket();
int port = intent.getIntExtra(group_owner_port,9999);
//binding connection
try{
String x="hello";
Log.e("hello","opening client socket");
byte[] address = getLocalAddress();
String ipAdd = getDottedDecimalIP(address);
socket.bind(null);
socket.connect(new InetSocketAddress(host,port),socket_timeout);
Log.e("hello","device socket address "+ socket.getInetAddress());
Log.e("hello","client socket is connected"+socket.isConnected());
Log.e("hello","device address :"+ipAdd + " byte "+ address);
//sending data to server
OutputStream stream = socket.getOutputStream();
stream.write(ipAdd.getBytes());
//getting data from the server(supposed to)
InputStream inputstream = socket.getInputStream();
byte[] address_to_sto_fr_ser = new byte[15] ;
inputstream.read(address_to_sto_fr_ser);
String s = new String(address_to_sto_fr_ser);
Log.e("msg from server","msg from server "+s);
// stream.close();
// is.close();
}catch (IOException e){
}
}
}
サーバーは、クライアントがサーバーに接続されている場合にのみ、クライアントに情報を送信できます。サーバーは、クライアントが接続するのを待つだけです。 – greenapps
いいえ。サーバーはクライアントのIPを知る必要はありません。IPがわかっていて、クライアントが接続されていない場合、サーバーはそのIPを使用してそのクライアントに接続することはできません。サーバが接続できなくなるため。クライアントのみがサーバーに接続できます。 – greenapps
しかし問題はありましたか?クライアントが接続するとすぐにデータを2方向に送信できます。 – greenapps