サーバーソケット接続に問題がありました。この小さなゲーム用のプログラムがあり、ソケットを使用して接続しようとしていますが、接続するserverSocket = new ServerSocket(53000);
と停止してフリーズし、その後どこにも行かないようですそれまたは任意の接続を取ります。そして、kkSocket = new Socket(duke, 53000);
その直後にクラッシュし、接続していないようです。ファイアウォールがオンになっていないことを確認し、インターネットアクセス許可を追加し、デバイス名についてさまざまなことを試しました。私は自分のデバイスに名前を付けて、現在のIPアドレスを試してみましたが、何もうまくいきません.Galaxy nexusとnexus 7タブレットを持っていますが、お互いに通信しようとすると両方に問題があります.
package com.idlethought.handagedon;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.DragEvent;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnDragListener;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.Toast;
public class ImageSelectionFragment extends Fragment {
private static final String IMAGE_DATA_EXTRA = "resId";
private int mImageNum;
private ImageView mImageView;
private String myHand = null;
private String theirHand = null;
private ServerSocket serverSocket;
static ImageSelectionFragment newInstance(int imageNum) {
final ImageSelectionFragment f = new ImageSelectionFragment();
final Bundle args = new Bundle();
args.putInt(IMAGE_DATA_EXTRA, imageNum);
f.setArguments(args);
return f;
}
// Empty constructor, required as per Fragment docs
public ImageSelectionFragment() {}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mImageNum = getArguments() != null ? getArguments().getInt(IMAGE_DATA_EXTRA) : -1;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// image_detail_fragment.xml contains just an ImageView
final View v = inflater.inflate(R.layout.image_detail_fragment, container, false);
mImageView = (ImageView) v.findViewById(R.id.imageView);
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
final int resId = MainActivity.imageResIds[mImageNum];
mImageView.setImageResource(resId); // Load image into ImageView
addListenerOnImage();
}
public void addListenerOnImage() {
mImageView.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
//String id = String.valueOf(mImageNum);
String screen = null;
//final int resId = MainActivity.imageResIdsRed[mImageNum];
//mImageView.setImageResource(resId); // Load image into ImageView
LinearLayout layout = new LinearLayout(getActivity().getApplication());
getActivity();
LayoutInflater layoutInflater = (LayoutInflater) getActivity().getBaseContext().getSystemService(FragmentActivity.LAYOUT_INFLATER_SERVICE);
View popupView = null;
Button btnDismiss = null;
Button btnAccept = null;
switch (mImageNum) {
case 0: screen = "ROCK";
popupView = layoutInflater.inflate(R.layout.submit_rock_hand, null);
btnDismiss = (Button) popupView.findViewById(R.id.decline_rock_selection);
btnAccept = (Button) popupView.findViewById(R.id.accept_rock_selection);
try {
openServer(screen);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case 1: screen = "PAPER";
popupView = layoutInflater.inflate(R.layout.submit_paper_hand, null);
btnDismiss = (Button) popupView.findViewById(R.id.decline_paper_selection);
btnAccept = (Button) popupView.findViewById(R.id.accept_paper_selection);
try {
findServer(screen);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case 2: screen = "SCISSORS";
popupView = layoutInflater.inflate(R.layout.submit_scissors_hand, null);
btnDismiss = (Button) popupView.findViewById(R.id.decline_scissors_selection);
btnAccept = (Button) popupView.findViewById(R.id.accept_scissors_selection);
break;
case 3: screen = "LIZARD";
popupView = layoutInflater.inflate(R.layout.submit_lizard_hand, null);
btnDismiss = (Button) popupView.findViewById(R.id.decline_lizard_selection);
btnAccept = (Button) popupView.findViewById(R.id.accept_lizard_selection);
break;
case 4: screen = "SPOCK";
popupView = layoutInflater.inflate(R.layout.submit_spock_hand, null);
btnDismiss = (Button) popupView.findViewById(R.id.decline_spock_selection);
btnAccept = (Button) popupView.findViewById(R.id.accept_spock_selection);
break;
default: screen = "ERROR!";
break;
}
final PopupWindow popupWindow = new PopupWindow(popupView,LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
popupWindow.showAtLocation(layout, Gravity.CENTER, 0, 0);
btnDismiss.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
popupWindow.dismiss();
}
});
btnAccept.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
/*Insert what the accept button does here*/
}
});
//Toast.makeText(getActivity().getApplication(),
// "test goes here",
// Toast.LENGTH_SHORT).show();
}
});
}
public void openServer(String hand) throws IOException {
// YEAHHHH TOAST!!!
Toast.makeText(getActivity().getApplication(),"Open Serverbf",Toast.LENGTH_SHORT).show();
serverSocket = new ServerSocket(53000);
Socket clientSocket = null;
boolean end = false;
while (!end){
Toast.makeText(getActivity().getApplication(),"Open Serveraf",Toast.LENGTH_SHORT).show();
clientSocket = serverSocket.accept();
end = serverSocket.isBound();
}
Toast.makeText(getActivity().getApplication(),"Open Serveraftr",Toast.LENGTH_SHORT).show();
PrintWriter out = new PrintWriter(clientSocket.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
theirHand = in.readLine();
myHand = hand;
out.println(hand);
Toast.makeText(getActivity().getApplication(),"Their Hand:" + theirHand,Toast.LENGTH_SHORT).show();
Toast.makeText(getActivity().getApplication(),"My Hand:" + myHand,Toast.LENGTH_SHORT).show();
out.close();
in.close();
clientSocket.close();
serverSocket.close();
}
public void findServer(String hand) throws IOException {
// YEAHHHH TOAST!!!
Toast.makeText(getActivity().getApplication(), "Find Server", Toast.LENGTH_SHORT).show();
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
Socket kkSocket = null;
PrintWriter out = null;
BufferedReader in = null;
InetAddress duke = InetAddress.getByName("192.168.1.137");
try {
kkSocket = new Socket(duke, 53000);
out = new PrintWriter(kkSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("could not connect");
e.printStackTrace();
} catch (IOException e) {
System.err.println("other error");
e.printStackTrace();
}
//out = new PrintWriter(kkSocket.getOutputStream(), true);
//in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
//BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
//out.println(hand);
//myHand = hand;
//theirHand = in.readLine();
//Toast.makeText(getActivity().getApplication(),"Their Hand:" + theirHand,Toast.LENGTH_SHORT).show();
Toast.makeText(getActivity().getApplication(),"My Hand:",Toast.LENGTH_SHORT).show();
out.close();
in.close();
//stdIn.close();
kkSocket.close();
}
}