2台の電話機がデータや携帯電話の接続なしでお互いにテキストメッセージを送信できる代わりにWiFi接続を使用するAndroidアプリを構築しようとしています。私はを見つけたので、最初にp2p(wifidirect)接続を作成してから、クライアントが接続してデータを送信するのを待つためにサーバーソケットを使用する必要があります。今、私はp2p接続を確立することができますが、私はソケットを使用してデータを転送することはできません。私はonPostExecute()
メソッドでヌルの結果をクライアント側とサーバー側の両方で取得しています。サーバーとクライアントをどのように確立する必要がありますか?はサーバーソケットに接続してアンドロイドでデータを転送できません
MainActivityで各クラスのオブジェクトを作成して、クライアント側とサーバー側を開始しました。両方ともバックグラウンドで実行されます。ここで
Client task = new Client(MainActivity.this,d.deviceAddress);
task.execute();
sever task = new server(MainActivity.this);
task.execute();
は私のサーバー側である:ここで
public class server extends AsyncTask<String, String, String>{
InputStream inputstream ;
Socket client;
ByteArrayOutputStream result;
byte[] buffer;
private Context context;
public static InetAddress address;
public static boolean connected;
public server(Context context) {
this.context = context;
}
@Override
protected String doInBackground(String... arg0) {
ServerSocket serverSocket;
//byte[] ipAddress={'5','4','3','1'};
try {
address = InetAddress.getByName("hazel");// i tried to use getByAddress(ipAddress) as well ,no change
serverSocket = new ServerSocket(8888,10,address);
// serverSocket.setSoTimeout(10000);
client = serverSocket.accept();
connected =true;
} catch (IOException e) {
return null;
}
return null;
}
@Override
protected void onPostExecute(String result) {
if (result != null) {
super.onPostExecute(result);
Toast.makeText(context, "result is "+result,Toast.LENGTH_LONG).show();
}
Toast.makeText(context, " is someone connected "+ connected ,Toast.LENGTH_LONG).show();
}
}
は、クライアント側である:
public class Client extends AsyncTask<String, String, String> {
Context ctx;
String deviceAddress ;
String sek;
boolean isresolved = false;
InetAddress inetaddress;
Client task = new Client(MainActivity.this,d.deviceAddress);
task.execute();
public Client(MainActivity ctx, String deviceAddress) {
this.ctx = ctx;
this.deviceAddress = deviceAddress;
}
@Override
protected String doInBackground(String... arg0) {
String host="hazel";
int port=8888;
int len;
Socket socket = new Socket();
byte buf[] = new byte[1024];
try {
socket.bind(null);
// byte[] ipAddress={'5','4','3','1'};
inetaddress = InetAddress.getByName("hazel");
InetSocketAddress address = new InetSocketAddress(inetaddress,8888);
isresolved = address.isUnresolved();
// the isresolved will get false value after executing the code
// i think it means that adress has no problem
socket.connect(address,1000);
} catch (SecurityException e) {
Log.v("Exception" , "is " + e);
} catch (Exception e) {
Log.v("Exception" , "is " + e);
}
finally {
if (socket != null) {
if (socket.isConnected()) {
try {
socket.close();
} catch (IOException e) {
Log.v("Exception " , "is " + e);
}
}
}
}
return null;
}
@Override
protected void onPostExecute(String result) {
if (result != null) {
super.onPostExecute(result);
Toast.makeText(ctx, "clientside , result : "+result ,Toast.LENGTH_LONG).show();
}else Toast.makeText(ctx, "result is null :(" , result : "+result ,Toast.LENGTH_LONG).show();
}
}
私は、サーバー側を実行するとonPostExecute
がnullの結果と呼ばれます。私はonPostExecuteがサーバーに接続された人の後に呼び出されることを期待しています。
クライアント側では、onPostExecute
メソッドでnullの結果が返されていますが、解決されていないメソッドもfalseを返します。
サーバーに接続してデータを送信する必要があります。そのため、誰かが私のロジックに間違いがあると指摘できますか?私はさまざまな方法(ポート、ホスト名とポート、IPアドレスのポートなど)でソケットを作成しようとしましたが、serverSocket.accept()
を入れましたが、うまくいきませんでした。