我已经在我的多人游戏代码中实现了Wi-Fi direct
。我可以在Google文档中提到的两个Android设备之间建立连接。
然后我创建了2个方法,用于发送和接收客户端和服务器端的数据。现在,我想从我的Renderer类中调用这些方法,当我调用这些函数时,它为套接字和流提供了NPE(空指针异常),但是使用相同的套接字和流来建立两个设备之间的连接。
问题:
代码:
服务器端:
public class DataTransferAsyncTask extends AsyncTask<Void,Void,String> {
ServerSocket serverSocket;
Context context = mActivity.getApplicationContext();
String s;
InetAddress client_add;
@Override
protected String doInBackground(Void... voids) {
if(first_call== 0) {
try {
first_call=1;
serverSocket = new ServerSocket(9999);
Log.e("hello", "Server: Socket opened");
socket_server = serverSocket.accept();
Log.e("hello", "Server: connection done");
inputstream_server = new DataInputStream(socket_server.getInputStream());
// OutputStream outputStream = serverSocket.getO
//getting data from client
byte[] address = new byte[12];
if (socket_server.isConnected())
inputstream_server.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" +
socket_server.isConnected());
Log.e("hello", "Server: clients ip " + socket_server.getInetAddress());
Log.e("hello", "Server: client msg as address " + s);
//client.connect(new InetSocketAddress(client.getInetAddress().getHostAddress(),9999),5000);
dataOutputStream_server = new DataOutputStream(socket_server.getOutputStream());
dataOutputStream_server.writeUTF("I am server");
//dataOutputStream.flush();
// cancel(true);
run_send_receive_server();
} catch (IOException e) {
}
}
return null;
}
//to transfer data to the another device
public void send_data_server() {
try {
if (player_tap == 0)
dataOutputStream_server.writeUTF("I am server");
} catch (IOException e) {
}
}
//to receive data from the another device
public void received_data_server() {
try {
byte[] address = new byte[12];
inputstream_server.read(address);
String s = new String(address);
Log.e("in server", " in server received : " + s);
}
} catch (IOException e) {
}
}
public void run_send_receive_server() {
send_data_server();
received_data_server();
}
客户端:
public class DataTransfer extends IntentService {
int socket_timeout = 5000;
public static String group_owner_address = "go_owner";
public static String group_owner_port = "go_port";
public static String action_send_data = "com";
DataOutputStream outputstream_client;
DataInputStream inputstream_client;
ModelRenderer modelRenderer;
Socket socket_client;
public DataTransfer(String name){
super(name);
}
public DataTransfer(){
super("hello");
}
@Override
protected void onHandleIntent(Intent intent) {
Log.e("hello", "client socket");
Toast.makeText(this, "client socket", Toast.LENGTH_LONG).show();
if (intent.getAction().equals(action_send_data)) {
String host = intent.getStringExtra(group_owner_address);
socket_client = new Socket();
int port = intent.getIntExtra(group_owner_port, 9999);
//binding connection
try {
Log.e("hello", "opening client socket");
byte[] address = getLocalAddress();
String ipAdd = getDottedDecimalIP(address);
socket_client.bind(null);
socket_client.connect(new InetSocketAddress(host, port), socket_timeout);
Log.e("hello", "device socket address " + socket_client.getInetAddress());
Log.e("hello", "client socket is connected" + socket_client.isConnected());
Log.e("hello", "device address :" + ipAdd + " byte " + address);
//sending data to server
outputstream_client = new DataOutputStream(socket_client.getOutputStream());
outputstream_client.write(ipAdd.getBytes());
//getting data from the server(supposed to)
inputstream_client = new DataInputStream(socket_client.getInputStream());
byte[] address_to_sto_fr_ser = new byte[13];
inputstream_client.read(address_to_sto_fr_ser);
String s = new String(address_to_sto_fr_ser);
Log.e("msg from server", "msg from server " + s);
outputstream_client.flush();
run_send_receive_client();
// stream.close();
// is.close();
} catch (IOException e) {
}
}
}
public void run_send_receive_client(){
send_data_client();
receive_data_client();
}
public void send_data_client(){
try {
String s = "hello";
String value = "True";
outputstream_client.write(s.getBytes());
}
}
catch(IOException e){
}
}
public void receive_data_client() {
byte[] address_to_sto_fr_ser = new byte[13];
try {
if(inputstream_client!=null) {
inputstream_client.read(address_to_sto_fr_ser);
} String s = new String(address_to_sto_fr_ser);
Log.e("in"," in receive data: " + s);
}catch (IOException e){
}
}
private byte[] getLocalAddress() {
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()) {
if (inetAddress instanceof Inet4Address) {
// fix for Galaxy Nexus. IPv4 is easy to use :-)
// Log.e("hello","hello : "+ Arrays.toString(inetAddress.getAddress()));
return inetAddress.getAddress();
}
//return inetAddress.getHostAddress().toString(); // Galaxy Nexus returns IPv6
}
}
}
} catch (SocketException ex) {
//Log.e("AndroidNetworkAddressFactory", "getLocalIPAddress()", ex);
} catch (NullPointerException ex) {
//Log.e("AndroidNetworkAddressFactory", "getLocalIPAddress()", ex);
}
return null;
}
private String getDottedDecimalIP(byte[] ipAddr) {
//convert to dotted decimal notation:
String ipAddrStr = "";
for (int i=0; i<ipAddr.length; i++) {
if (i > 0) {
ipAddrStr += ".";
}
ipAddrStr += ipAddr[i]&0xFF;
}
return ipAddrStr;
}
}
记录猫:
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at com.octane.pingpong.DataTransfer.send_data_client (DataTransfer.java:110)
at com.octane.pingpong.DataTransfer.run_send_receive_client(DataTransfer.java:95)
at com.octane.pingpong.ModelRenderer.Tap_To_Cont_Ingame_Logic_Multiplayer(ModelRenderer.java:3929)
at com.octane.pingpong.ModelRenderer$SampleOnClickListener.onClick(ModelRenderer.java:4157)
at android.view.View.performClick(View.java:4222)
at android.view.View$PerformClick.run(View.java:17620)
at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5391)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)