Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 服务器套接字连接问题_Java_Android_Sockets_Wireless_Serversocket - Fatal编程技术网

Java 服务器套接字连接问题

Java 服务器套接字连接问题,java,android,sockets,wireless,serversocket,Java,Android,Sockets,Wireless,Serversocket,我在服务器套接字连接方面遇到了一个问题,我有一个用于这个小游戏的程序,我正在尝试使用套接字来连接它,但是当我到达serverSocket=newserversocket(53000)它只是停止和冻结,似乎没有去任何地方之后,或采取任何连接。和kkSocket=新插座(杜克,53000)然后它就崩溃了,似乎没有连接,我已经确保我的防火墙没有打开,我添加了互联网权限,我尝试了很多不同的方法来获取设备名称,我输入了我的设备名称,尝试了当前的ip地址,什么都不会通过,我有一个galaxy nexus和一

我在服务器套接字连接方面遇到了一个问题,我有一个用于这个小游戏的程序,我正在尝试使用套接字来连接它,但是当我到达
serverSocket=newserversocket(53000)它只是停止和冻结,似乎没有去任何地方之后,或采取任何连接。和
kkSocket=新插座(杜克,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();
    }

}

你能给我们看看日志吗?怎么了

第二:当你这样做的时候:

 while (!end){
        Toast.makeText(getActivity().getApplication(),"Open Serveraf",Toast.LENGTH_SHORT).show();
        clientSocket = serverSocket.accept();
        end = serverSocket.isBound();   
    }    

接受第一次连接后,服务器套接字将不再侦听,因为while条件为false。

使用try-catch语句捕获并显示正在发生的任何错误,并缩小问题范围。定义“it just crash”。当你遇到异常时,不要编造你自己的消息,尤其是像“其他错误”一样平庸、内容自由的消息。打印异常附带的消息,最好还打印堆栈跟踪。否则你就没有希望知道出了什么问题,因为你不知道。