我一直有服务器套接字连接的问题,我有一个用于这个小游戏的程序,并试图使用套接字来连接它,但是当我到达它时,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();
}
}